|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; |
| 8 | +use PhpOffice\PhpSpreadsheet\Shared\File; |
| 9 | +use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; |
| 10 | +use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
| 11 | + |
| 12 | +class Issue3767Test extends AbstractFunctional |
| 13 | +{ |
| 14 | + // some weirdness with this file, including the fact that it has a |
| 15 | + // title ('Chart Title') which I cannot find anywhere in the xml. |
| 16 | + private static string $testbook = 'tests/data/Reader/XLSX/issue.3767.xlsx'; |
| 17 | + |
| 18 | + private string $tempfile = ''; |
| 19 | + |
| 20 | + protected function tearDown(): void |
| 21 | + { |
| 22 | + if ($this->tempfile !== '') { |
| 23 | + unlink($this->tempfile); |
| 24 | + $this->tempfile = ''; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public function readCharts(XlsxReader $reader): void |
| 29 | + { |
| 30 | + $reader->setIncludeCharts(true); |
| 31 | + } |
| 32 | + |
| 33 | + public function writeCharts(XlsxWriter $writer): void |
| 34 | + { |
| 35 | + $writer->setIncludeCharts(true); |
| 36 | + } |
| 37 | + |
| 38 | + public function testReadWithoutCharts(): void |
| 39 | + { |
| 40 | + $reader = new XlsxReader(); |
| 41 | + //$this->readCharts($reader); // Commented out - don't want to read charts. |
| 42 | + $spreadsheet = $reader->load(self::$testbook); |
| 43 | + $sheet = $spreadsheet->getActiveSheet(); |
| 44 | + $charts = $sheet->getChartCollection(); |
| 45 | + self::assertCount(0, $charts); |
| 46 | + $this->tempfile = File::temporaryFileName(); |
| 47 | + $writer = new XlsxWriter($spreadsheet); |
| 48 | + $this->writeCharts($writer); |
| 49 | + $writer->save($this->tempfile); |
| 50 | + $spreadsheet->disconnectWorksheets(); |
| 51 | + $file = 'zip://'; |
| 52 | + $file .= $this->tempfile; |
| 53 | + $file .= '#xl/worksheets/_rels/sheet1.xml.rels'; |
| 54 | + $data = (string) file_get_contents($file); |
| 55 | + // PhpSpreadsheet still generates this target even though charts aren't included |
| 56 | + self::assertStringContainsString('Target="../drawings/drawing1.xml"', $data); |
| 57 | + $file = 'zip://'; |
| 58 | + $file .= $this->tempfile; |
| 59 | + $file .= '#xl/drawings/drawing1.xml'; |
| 60 | + $data = file_get_contents($file); |
| 61 | + self::assertSame('<xml></xml>', $data); // fake file because rels needs it |
| 62 | + } |
| 63 | + |
| 64 | + public function testReadWithCharts(): void |
| 65 | + { |
| 66 | + $reader = new XlsxReader(); |
| 67 | + $this->readCharts($reader); |
| 68 | + $spreadsheet = $reader->load(self::$testbook); |
| 69 | + $xsheet = $spreadsheet->getActiveSheet(); |
| 70 | + $xcharts = $xsheet->getChartCollection(); |
| 71 | + self::assertCount(1, $xcharts); |
| 72 | + /** @var callable */ |
| 73 | + $callableReader = [$this, 'readCharts']; |
| 74 | + /** @var callable */ |
| 75 | + $callableWriter = [$this, 'writeCharts']; |
| 76 | + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter); |
| 77 | + $spreadsheet->disconnectWorksheets(); |
| 78 | + $sheet = $reloadedSpreadsheet->getActiveSheet(); |
| 79 | + $charts = $xsheet->getChartCollection(); |
| 80 | + self::assertCount(1, $charts); |
| 81 | + // In Excel, a default title ('Chart Title') is shown. |
| 82 | + // I can't find that anywhere in the Xml. |
| 83 | + self::assertSame('', $charts[0]?->getTitle()?->getCaptionText()); |
| 84 | + // Just test anything on the chart. |
| 85 | + self::assertSame($sheet->getCell('B2')->getValue(), $charts[0]->getPlotArea()?->getPlotGroup()[0]->getPlotValues()[0]->getDataValues()[0]); |
| 86 | + $reloadedSpreadsheet->disconnectWorksheets(); |
| 87 | + } |
| 88 | +} |
0 commit comments