mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-28 19:19:27 +00:00
ecb4a7fe27
This is a leftover Scrutinizer change, but it needed more attention than most others. Chart/Title DocBlocks define caption as `null|string`. However, in the wild, Excel usually presents the caption as an array, and not an array of strings but rather of RichText items. I am not sure why an array is needed since a RichText item can contain many text runs, but things are what they are. Reader/Xlsx/ChartTitleTest reads a spreadsheet with the captions stored as a RichText array. Since it performs array operations on something the DocBlock says cannot be an array, Scrutinizer objects, although not seriously enough to fail the module. Phpstan also objects; its objection is silenced with an annotation. Aside from this test, there are other tests which do set the caption to a string, and Excel seems to handle that without a problem. So, I have changed the DocBlock to specify `array|RichText|String`. I have dropped null as a possibility; nullstring will do equally well. Because getCaption can now return multiple datatypes, I think a new function which can return the text portion of the entire caption as a single string is needed. I have added it. This simplifies the test named above, and some code in Writer/Html. The latter is not part of unit testing because the version of JpGraph found in Composer is too antiquated. I verified the Html change manually by running samples/Chart/32_Chart_read_write_HTML.php using a recent version of JpGraph. It was as a result of this test that I uncovered issue #2203. I did not see anything about Charts in docs, so did not add a description of the new function there. Phpstan is happy with the changes. We'll see how Scrutinizer feels when I push it.
60 lines
2.4 KiB
PHP
60 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Chart\Title;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ChartsTitleTest extends TestCase
|
|
{
|
|
private static function getTitleText(?Title $title): string
|
|
{
|
|
return ($title === null) ? '' : $title->getCaptionText();
|
|
}
|
|
|
|
public function testChartTitles(): void
|
|
{
|
|
$filename = 'tests/data/Reader/XLSX/excelChartsTest.xlsx';
|
|
$reader = IOFactory::createReader('Xlsx')->setIncludeCharts(true);
|
|
$spreadsheet = $reader->load($filename);
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$charts = $worksheet->getChartCollection();
|
|
self::assertEquals(5, $worksheet->getChartCount());
|
|
self::assertCount(5, $charts);
|
|
|
|
// No title or axis labels
|
|
$chart1 = $charts[0];
|
|
$title = self::getTitleText($chart1->getTitle());
|
|
self::assertEmpty($title);
|
|
self::assertEmpty(self::getTitleText($chart1->getXAxisLabel()));
|
|
self::assertEmpty(self::getTitleText($chart1->getYAxisLabel()));
|
|
|
|
// Title, no axis labels
|
|
$chart2 = $charts[1];
|
|
|
|
self::assertEquals('Chart with Title and no Axis Labels', self::getTitleText($chart2->getTitle()));
|
|
self::assertEmpty(self::getTitleText($chart2->getXAxisLabel()));
|
|
self::assertEmpty(self::getTitleText($chart2->getYAxisLabel()));
|
|
|
|
// No title, only horizontal axis label
|
|
$chart3 = $charts[2];
|
|
self::assertEmpty(self::getTitleText($chart3->getTitle()));
|
|
self::assertEquals('Horizontal Axis Title Only', self::getTitleText($chart3->getXAxisLabel()));
|
|
self::assertEmpty(self::getTitleText($chart3->getYAxisLabel()));
|
|
|
|
// No title, only vertical axis label
|
|
$chart4 = $charts[3];
|
|
self::assertEmpty(self::getTitleText($chart4->getTitle()));
|
|
self::assertEquals('Vertical Axis Title Only', self::getTitleText($chart4->getYAxisLabel()));
|
|
self::assertEmpty(self::getTitleText($chart4->getXAxisLabel()));
|
|
|
|
// Title and both axis labels
|
|
$chart5 = $charts[4];
|
|
self::assertEquals('Complete Annotations', self::getTitleText($chart5->getTitle()));
|
|
self::assertEquals('Horizontal Axis Title', self::getTitleText($chart5->getXAxisLabel()));
|
|
self::assertEquals('Vertical Axis Title', self::getTitleText($chart5->getYAxisLabel()));
|
|
}
|
|
}
|