Files
oleibman 29c0162e2a Let Phpstan Run on Samples (#3808)
* Let Phpstan Run on Samples

Phpstan currently analyzes all source and test members. We already run phpcs and php-cs-fixer on samples as well. I would expect that samples are often used as templates for code in userland; it behooves us to be at least as careful with those members as for the others which are already being analyzed.  Aside from 1300+ messages `Variable $helper might not be defined.`, which will be suppressed in phpstan.neon.dist, there are really only a few changes needed for sample members, so that part of the code base was already in good shape, and is now even better. No annotations were needed.

* Scrutinizer 2 out of 3

1 false positive, now suppressed; fix other 2.

* Remove Dead Code

* Very Minor Changes

* Add infra
2023-12-06 09:40:27 -08:00

69 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\RichText\TextElement;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class RichTextTest extends TestCase
{
public function testConstructorSpecifyingCell(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell = $sheet->getCell('A1');
$cell->setValue(2);
self::assertSame(2, $cell->getCalculatedValue());
$cell->getStyle()->getFont()->setName('whatever');
$richText = new RichText($cell);
self::assertSame('whatever', $sheet->getCell('A1')->getStyle()->getFont()->getName());
self::assertEquals($richText, $cell->getValue());
self::assertSame('2', $cell->getCalculatedValue());
$spreadsheet->disconnectWorksheets();
}
public function testTextElements(): void
{
$element1 = new TextElement('A');
$element2 = new TextElement('B');
$element3 = new TextElement('C');
$richText = new RichText();
$richText->setRichTextElements([$element1, $element2, $element3]);
self::assertSame('ABC', $richText->getPlainText());
$cloneText = clone $richText;
self::assertEquals($richText, $cloneText);
self::assertNotSame($richText, $cloneText);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($richText);
self::assertInstanceOf(RichText::class, $sheet->getCell('A1')->getValue());
self::assertSame('ABC', $sheet->getCell('A1')->getFormattedValue());
$sheet->getCell('B1')->setValue(-3.5);
self::assertSame([['ABC', '-3.5']], $sheet->toArray());
$spreadsheet->disconnectWorksheets();
}
public function testNullFont(): void
{
$richText = new RichText();
$textRun = $richText->createTextRun('hello');
self::assertNotNull($textRun->getFontOrThrow());
$textRun->setFont(null);
try {
$textRun->getFontOrThrow();
$foundFont = true;
} catch (SpreadsheetException $e) {
$foundFont = false;
}
self::assertFalse($foundFont, 'expected exception not received');
}
}