From 1a0aab1a4ffc1f027f4e78b2d3c0050b1de4a4ce Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Fri, 27 Nov 2020 06:50:01 -0800 Subject: [PATCH] Improve Coverage in src/PhpSpreadsheet There are no changes to code. Additional tests are added, so that the following 6 items now have 100% test coverage: - Comment - DefinedName - DocumentGenerator - IOFactory - NamedFormula - NamedRange --- samples/Reader/sampleData/example1xls | Bin 0 -> 22528 bytes .../DefinedNameFormulaTest.php | 21 +++++ tests/PhpSpreadsheetTests/DefinedNameTest.php | 82 ++++++++++++++++++ .../DocumentGeneratorTest.php | 10 +++ .../Functional/CommentsTest.php | 15 +++- tests/PhpSpreadsheetTests/IOFactoryTest.php | 41 +++++++++ 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 samples/Reader/sampleData/example1xls diff --git a/samples/Reader/sampleData/example1xls b/samples/Reader/sampleData/example1xls new file mode 100644 index 0000000000000000000000000000000000000000..bd9bb110b9a41b0aab816ba9e20682ee5bb43aea GIT binary patch literal 22528 zcmeHPd2AfldH-g)%R?l!yhO^f#EePVk|=F`4@lQe*F_)S@#2pguA6q-dJdo`YODG=K%}1 z3Fr$81Z}D;$orXW1$77wy4q{--(IMSdscj7mxJl9htIz0_w= z-J8|xfciBWBvDH%M_NRwHFb8Ry(YCcxmHrrDbn}JzW#0f{X=)2AGD9X$qktQYpZAv zo%C`KS2OfV1G*wCynU#;JfPlWzcfnkVvdLuWxE|K%aF(gRzBdWCKm>cE)_7K_6_!d zc407cVX!y3O}5A;7Ob$HuX-ka5&vDXO)w;?D}n-%x(udL-N;C#!d_C%L#@(Y|*@B6#lU%HJ5}cb1XERdT5u!HBMn|8z}! zgROTW(YH!gr6jpd$7coa_DQOOr+T&Z{7g;xrd@r)CCh|)_rr;#w9VBmFmIo$*Supc zsd?vIgXS_Pns?2m79P>P;0esiS1?&cT4i6hoOMr4cwX7C|5xH6GNzPFTp)Q39vOk_)iyhnLnp`69w**`u(!U6wUx#X9D zxYsXS0{x(0_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ulaTest.php +++ b/tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheetTests; use PhpOffice\PhpSpreadsheet\DefinedName; +use PhpOffice\PhpSpreadsheet\NamedFormula; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; @@ -164,4 +165,24 @@ class DefinedNameFormulaTest extends TestCase 'utf-8 named ranges in a formula' => ['Здравствуй+мир', true], ]; } + + public function testEmptyNamedFormula(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); + $spreadSheet = new Spreadsheet(); + $workSheet1 = $spreadSheet->getActiveSheet(); + new NamedFormula('namedformula', $workSheet1); + } + + public function testChangeFormula(): void + { + $spreadSheet = new Spreadsheet(); + $workSheet1 = $spreadSheet->getActiveSheet(); + $namedFormula = new NamedFormula('namedformula', $workSheet1, '=1'); + self::assertEquals('=1', $namedFormula->getFormula()); + $namedFormula->setFormula('=2'); + self::assertEquals('=2', $namedFormula->getFormula()); + $namedFormula->setFormula(''); + self::assertEquals('=2', $namedFormula->getFormula()); + } } diff --git a/tests/PhpSpreadsheetTests/DefinedNameTest.php b/tests/PhpSpreadsheetTests/DefinedNameTest.php index 3f5b75a0..c199e7f2 100644 --- a/tests/PhpSpreadsheetTests/DefinedNameTest.php +++ b/tests/PhpSpreadsheetTests/DefinedNameTest.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheetTests; use PhpOffice\PhpSpreadsheet\DefinedName; +use PhpOffice\PhpSpreadsheet\NamedRange; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; @@ -121,4 +122,85 @@ class DefinedNameTest extends TestCase $this->spreadsheet->getDefinedName('foo')->getValue() ); } + + public function testDefinedNameNoWorksheetNoScope(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); + new NamedRange('xyz'); + } + + public function testSetAndGetRange(): void + { + $this->spreadsheet->addDefinedName( + DefinedName::createInstance('xyz', $this->spreadsheet->getActiveSheet(), 'A1') + ); + + $namedRange = $this->spreadsheet->getDefinedName('XYZ'); + self::assertInstanceOf(NamedRange::class, $namedRange); + if ($namedRange instanceof NamedRange) { + self::assertEquals('A1', $namedRange->getRange()); + self::assertEquals('A1', $namedRange->getValue()); + $namedRange->setRange('A2'); + self::assertEquals('A2', $namedRange->getValue()); + } + } + + public function testChangeWorksheet(): void + { + $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); + $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); + $sheet1->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->setValue(2); + $namedRange = new NamedRange('xyz', $sheet2, '$A$1'); + $namedRange->setWorksheet($sheet1); + $this->spreadsheet->addNamedRange($namedRange); + $sheet1->getCell('B2')->setValue('=XYZ'); + self::assertEquals(1, $sheet1->getCell('B2')->getCalculatedValue()); + $sheet2->getCell('B2')->setValue('=XYZ'); + self::assertEquals(1, $sheet2->getCell('B2')->getCalculatedValue()); + } + + public function testLocalOnly(): void + { + $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); + $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); + $sheet1->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->setValue(2); + $namedRange = new NamedRange('abc', $sheet2, '$A$1'); + $namedRange->setWorksheet($sheet1)->setLocalOnly(true); + $this->spreadsheet->addNamedRange($namedRange); + $sheet1->getCell('C2')->setValue('=ABC'); + self::assertEquals(1, $sheet1->getCell('C2')->getCalculatedValue()); + $sheet2->getCell('C2')->setValue('=ABC'); + self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue()); + } + + public function testScope(): void + { + $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); + $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); + $sheet1->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->setValue(2); + $namedRange = new NamedRange('abc', $sheet2, '$A$1'); + $namedRange->setScope($sheet1); + $this->spreadsheet->addNamedRange($namedRange); + $sheet1->getCell('C2')->setValue('=ABC'); + self::assertEquals(2, $sheet1->getCell('C2')->getCalculatedValue()); + $sheet2->getCell('C2')->setValue('=ABC'); + self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue()); + } + + public function testClone(): void + { + $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); + $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); + $sheet1->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->setValue(2); + $namedRange = new NamedRange('abc', $sheet2, '$A$1'); + $namedRangeClone = clone $namedRange; + $ss1 = $namedRange->getWorksheet(); + $ss2 = $namedRangeClone->getWorksheet(); + self::assertNotSame($ss1, $ss2); + self::assertEquals($ss1->getTitle(), $ss2->getTitle()); + } } diff --git a/tests/PhpSpreadsheetTests/DocumentGeneratorTest.php b/tests/PhpSpreadsheetTests/DocumentGeneratorTest.php index 8a34f1c0..45000f8e 100644 --- a/tests/PhpSpreadsheetTests/DocumentGeneratorTest.php +++ b/tests/PhpSpreadsheetTests/DocumentGeneratorTest.php @@ -7,6 +7,7 @@ use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Logical; use PhpOffice\PhpSpreadsheet\DocumentGenerator; use PHPUnit\Framework\TestCase; +use UnexpectedValueException; class DocumentGeneratorTest extends TestCase { @@ -137,4 +138,13 @@ EXPECTED ], ]; } + + public function testGenerateFunctionBadArray(): void + { + $this->expectException(UnexpectedValueException::class); + $phpSpreadsheetFunctions = [ + 'ABS' => ['category' => Cat::CATEGORY_MATH_AND_TRIG, 'functionCall' => 1], + ]; + DocumentGenerator::generateFunctionListByName($phpSpreadsheetFunctions); + } } diff --git a/tests/PhpSpreadsheetTests/Functional/CommentsTest.php b/tests/PhpSpreadsheetTests/Functional/CommentsTest.php index 6f1c5340..5ba4e7c8 100644 --- a/tests/PhpSpreadsheetTests/Functional/CommentsTest.php +++ b/tests/PhpSpreadsheetTests/Functional/CommentsTest.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Functional; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Style\Alignment; class CommentsTest extends AbstractFunctional { @@ -35,10 +36,22 @@ class CommentsTest extends AbstractFunctional $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); - $commentsLoaded = $reloadedSpreadsheet->getSheet(0)->getComments(); + $sheet = $reloadedSpreadsheet->getSheet(0); + $commentsLoaded = $sheet->getComments(); self::assertCount(1, $commentsLoaded); $commentCoordinate = key($commentsLoaded); self::assertSame('E10', $commentCoordinate); + $comment = $commentsLoaded[$commentCoordinate]; + self::assertEquals('Comment to test', (string) $comment); + $commentClone = clone $comment; + self::assertEquals($comment, $commentClone); + self::assertNotSame($comment, $commentClone); + if ($format === 'Xlsx') { + self::assertEquals('feb0c24b880a8130262dadf801f85e94', $comment->getHashCode()); + self::assertEquals(Alignment::HORIZONTAL_GENERAL, $comment->getAlignment()); + $comment->setAlignment(Alignment::HORIZONTAL_RIGHT); + self::assertEquals(Alignment::HORIZONTAL_RIGHT, $comment->getAlignment()); + } } } diff --git a/tests/PhpSpreadsheetTests/IOFactoryTest.php b/tests/PhpSpreadsheetTests/IOFactoryTest.php index f9512a34..9b07ad33 100644 --- a/tests/PhpSpreadsheetTests/IOFactoryTest.php +++ b/tests/PhpSpreadsheetTests/IOFactoryTest.php @@ -161,4 +161,45 @@ class IOFactoryTest extends TestCase IOFactory::registerReader('foo', 'bar'); } + + public function testCreateInvalidWriter(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class); + $spreadsheet = new Spreadsheet(); + IOFactory::createWriter($spreadsheet, 'bad'); + } + + public function testCreateInvalidReader(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); + IOFactory::createReader('bad'); + } + + public function testCreateReaderUnknownExtension(): void + { + $filename = 'samples/Reader/sampleData/example1.tsv'; + $reader = IOFactory::createReaderForFile($filename); + self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader)); + } + + public function testCreateReaderCsvExtension(): void + { + $filename = 'samples/Reader/sampleData/example1.csv'; + $reader = IOFactory::createReaderForFile($filename); + self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader)); + } + + public function testCreateReaderNoExtension(): void + { + $filename = 'samples/Reader/sampleData/example1xls'; + $reader = IOFactory::createReaderForFile($filename); + self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Xls', get_class($reader)); + } + + public function testCreateReaderNotSpreadsheet(): void + { + $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); + $filename = __FILE__; + $reader = IOFactory::createReaderForFile($filename); + } }