mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-22 15:13:41 +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.
59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Chart;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Chart\Title;
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class TitleTest extends TestCase
|
|
{
|
|
public function testString(): void
|
|
{
|
|
$title = new Title('hello');
|
|
self::assertSame('hello', $title->getCaption());
|
|
self::assertSame('hello', $title->getCaptionText());
|
|
}
|
|
|
|
public function testStringArray(): void
|
|
{
|
|
$title = new Title();
|
|
$title->setCaption(['Hello', ', ', 'world.']);
|
|
self::assertSame('Hello, world.', $title->getCaptionText());
|
|
}
|
|
|
|
public function testRichText(): void
|
|
{
|
|
$title = new Title();
|
|
$richText = new RichText();
|
|
$part = $richText->createTextRun('Hello');
|
|
$font = $part->getFont();
|
|
if ($font === null) {
|
|
self::fail('Unable to retrieve font');
|
|
} else {
|
|
$font->setBold(true);
|
|
$title->setCaption($richText);
|
|
self::assertSame('Hello', $title->getCaptionText());
|
|
}
|
|
}
|
|
|
|
public function testMixedArray(): void
|
|
{
|
|
$title = new Title();
|
|
$richText1 = new RichText();
|
|
$part1 = $richText1->createTextRun('Hello');
|
|
$font1 = $part1->getFont();
|
|
$richText2 = new RichText();
|
|
$part2 = $richText2->createTextRun('world');
|
|
$font2 = $part2->getFont();
|
|
if ($font1 === null || $font2 === null) {
|
|
self::fail('Unable to retrieve font');
|
|
} else {
|
|
$font1->setBold(true);
|
|
$font2->setItalic(true);
|
|
$title->setCaption([$richText1, ', ', $richText2, '.']);
|
|
self::assertSame('Hello, world.', $title->getCaptionText());
|
|
}
|
|
}
|
|
}
|