From 873aa649fc0992630609ac1691d8b68dbf72bc81 Mon Sep 17 00:00:00 2001 From: zhukoff74 Date: Wed, 3 Dec 2025 22:54:20 +0300 Subject: [PATCH 1/3] Simplified CellTest and added a helper method for styles --- tests/PhpSpreadsheetTests/Cell/CellTest.php | 64 ++++++++------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Cell/CellTest.php b/tests/PhpSpreadsheetTests/Cell/CellTest.php index 9b0ba5572f..7d6441b850 100644 --- a/tests/PhpSpreadsheetTests/Cell/CellTest.php +++ b/tests/PhpSpreadsheetTests/Cell/CellTest.php @@ -36,6 +36,16 @@ protected function tearDown(): void } } + private function createSolidFillStyle(string $color): Style + { + $style = new Style(false, true); + $style->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getStartColor()->setARGB($color); + + return $style; + } + public function testSetValueBinderOverride(): void { $value = '12.5%'; @@ -130,10 +140,9 @@ public function testNoChangeToActiveSheet(): void $sheet1->setCellValue('D1', 124); $sheet3->setCellValue('A1', "='Sheet 1'!C1+'Sheet 1'!D1"); $sheet1->setCellValue('A1', "='Sheet 3'!A1"); - $cell = 'A1'; $spreadsheet->setActiveSheetIndex(0); self::assertEquals(0, $spreadsheet->getActiveSheetIndex()); - $value = $spreadsheet->getActiveSheet()->getCell($cell)->getCalculatedValue(); + $value = $spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(); self::assertEquals(0, $spreadsheet->getActiveSheetIndex()); self::assertEquals(247, $value); $spreadsheet->disconnectWorksheets(); @@ -172,12 +181,9 @@ public function testDestroyCell2(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Coordinate no longer exists'); $parent = $cell->getParent(); - if ($parent === null) { - self::fail('Unexpected null parent'); - } else { - $parent->delete('A1'); - $cell->getCoordinate(); - } + self::assertNotNull($parent, 'Parent should not be null'); + $parent->delete('A1'); + $cell->getCoordinate(); } public function testAppliedStyleWithRange(): void @@ -191,34 +197,22 @@ public function testAppliedStyleWithRange(): void $cellRange = 'A1:A3'; $sheet->getStyle($cellRange)->getFont()->setBold(true); - $yellowStyle = new Style(false, true); - $yellowStyle->getFill() - ->setFillType(Fill::FILL_SOLID) - ->getStartColor()->setARGB(Color::COLOR_YELLOW); - $greenStyle = new Style(false, true); - $greenStyle->getFill() - ->setFillType(Fill::FILL_SOLID) - ->getStartColor()->setARGB(Color::COLOR_GREEN); - $redStyle = new Style(false, true); - $redStyle->getFill() - ->setFillType(Fill::FILL_SOLID) - ->getStartColor()->setARGB(Color::COLOR_RED); + $yellowStyle = $this->createSolidFillStyle(Color::COLOR_YELLOW); + $greenStyle = $this->createSolidFillStyle(Color::COLOR_GREEN); + $redStyle = $this->createSolidFillStyle(Color::COLOR_RED); - $conditionalStyles = []; $wizardFactory = new Wizard($cellRange); /** @var Wizard\CellValue $cellWizard */ $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE); - $cellWizard->equals(0) - ->setStyle($yellowStyle); + $conditionalStyles = []; + $cellWizard->equals(0)->setStyle($yellowStyle); $conditionalStyles[] = $cellWizard->getConditional(); - $cellWizard->greaterThan(0) - ->setStyle($greenStyle); + $cellWizard->greaterThan(0)->setStyle($greenStyle); $conditionalStyles[] = $cellWizard->getConditional(); - $cellWizard->lessThan(0) - ->setStyle($redStyle); + $cellWizard->lessThan(0)->setStyle($redStyle); $conditionalStyles[] = $cellWizard->getConditional(); $sheet->getStyle($cellWizard->getCellRange()) @@ -261,14 +255,8 @@ public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyl $cellRange = 'A1:C2'; $sheet->getStyle($cellRange)->getFont()->setBold(true); - $yellowStyle = new Style(false, true); - $yellowStyle->getFill() - ->setFillType(Fill::FILL_SOLID) - ->getStartColor()->setARGB(Color::COLOR_YELLOW); - $redStyle = new Style(false, true); - $redStyle->getFill() - ->setFillType(Fill::FILL_SOLID) - ->getStartColor()->setARGB(Color::COLOR_RED); + $yellowStyle = $this->createSolidFillStyle(Color::COLOR_YELLOW); + $redStyle = $this->createSolidFillStyle(Color::COLOR_RED); $conditionalCellRange = 'A1:C1'; $conditionalStyles = []; @@ -276,12 +264,10 @@ public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyl /** @var Wizard\CellValue $cellWizard */ $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE); - $cellWizard->equals(0) - ->setStyle($yellowStyle); + $cellWizard->equals(0)->setStyle($yellowStyle); $conditionalStyles[] = $cellWizard->getConditional(); - $cellWizard->lessThan(0) - ->setStyle($redStyle); + $cellWizard->lessThan(0)->setStyle($redStyle); $conditionalStyles[] = $cellWizard->getConditional(); $sheet->getStyle($cellWizard->getCellRange()) From 49479a01b2d5d5af2997830642800b24dc7d87e0 Mon Sep 17 00:00:00 2001 From: zhukoff74 Date: Wed, 3 Dec 2025 23:37:01 +0300 Subject: [PATCH 2/3] replace with --- tests/PhpSpreadsheetTests/Cell/CellTest.php | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Cell/CellTest.php b/tests/PhpSpreadsheetTests/Cell/CellTest.php index 7d6441b850..38c5f37afd 100644 --- a/tests/PhpSpreadsheetTests/Cell/CellTest.php +++ b/tests/PhpSpreadsheetTests/Cell/CellTest.php @@ -141,10 +141,10 @@ public function testNoChangeToActiveSheet(): void $sheet3->setCellValue('A1', "='Sheet 1'!C1+'Sheet 1'!D1"); $sheet1->setCellValue('A1', "='Sheet 3'!A1"); $spreadsheet->setActiveSheetIndex(0); - self::assertEquals(0, $spreadsheet->getActiveSheetIndex()); + self::assertSame(0, $spreadsheet->getActiveSheetIndex()); $value = $spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(); - self::assertEquals(0, $spreadsheet->getActiveSheetIndex()); - self::assertEquals(247, $value); + self::assertSame(0, $spreadsheet->getActiveSheetIndex()); + self::assertSame(247, $value); $spreadsheet->disconnectWorksheets(); } @@ -220,21 +220,21 @@ public function testAppliedStyleWithRange(): void $style = $sheet->getCell('A1')->getAppliedStyle(); self::assertTrue($style->getFont()->getBold()); - self::assertEquals($redStyle->getFill()->getFillType(), $style->getFill()->getFillType()); - self::assertEquals($redStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB()); + self::assertSame($redStyle->getFill()->getFillType(), $style->getFill()->getFillType()); + self::assertSame($redStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB()); $style = $sheet->getCell('A2')->getAppliedStyle(); self::assertTrue($style->getFont()->getBold()); - self::assertEquals($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType()); - self::assertEquals( + self::assertSame($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType()); + self::assertSame( $yellowStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB() ); $style = $sheet->getCell('A3')->getAppliedStyle(); self::assertTrue($style->getFont()->getBold()); - self::assertEquals($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType()); - self::assertEquals( + self::assertSame($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType()); + self::assertSame( $greenStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB() ); @@ -276,9 +276,9 @@ public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyl $style = $sheet->getCell($cellAddress)->getAppliedStyle(); self::assertTrue($style->getFont()->getBold()); - self::assertEquals($fillStyle, $style->getFill()->getFillType()); + self::assertSame($fillStyle, $style->getFill()->getFillType()); if ($fillStyle === Fill::FILL_SOLID) { - self::assertEquals($fillColor, $style->getFill()->getStartColor()->getARGB()); + self::assertSame($fillColor, $style->getFill()->getStartColor()->getARGB()); } $spreadsheet->disconnectWorksheets(); } From c1be7a5bb594948543f04551044a4c6c22c4b6fb Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Wed, 3 Dec 2025 19:33:07 -0800 Subject: [PATCH 3/3] Strengthen One Test --- tests/PhpSpreadsheetTests/Cell/CellTest.php | 3 ++- tests/data/Cell/SetValueExplicitException.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Cell/CellTest.php b/tests/PhpSpreadsheetTests/Cell/CellTest.php index 38c5f37afd..769ea78324 100644 --- a/tests/PhpSpreadsheetTests/Cell/CellTest.php +++ b/tests/PhpSpreadsheetTests/Cell/CellTest.php @@ -115,9 +115,10 @@ public function testInvalidIsoDateSetValueExplicit(): void } #[DataProvider('providerSetValueExplicitException')] - public function testSetValueExplicitException(mixed $value, string $dataType): void + public function testSetValueExplicitException(mixed $value, string $dataType, string $message): void { $this->expectException(Exception::class); + $this->expectExceptionMessage($message); $this->spreadsheet = new Spreadsheet(); $cell = $this->spreadsheet->getActiveSheet()->getCell('A1'); diff --git a/tests/data/Cell/SetValueExplicitException.php b/tests/data/Cell/SetValueExplicitException.php index d21372bbd5..1cd1fd7c14 100644 --- a/tests/data/Cell/SetValueExplicitException.php +++ b/tests/data/Cell/SetValueExplicitException.php @@ -5,7 +5,7 @@ use PhpOffice\PhpSpreadsheet\Cell\DataType; return [ - 'invalid numeric' => ['XYZ', DataType::TYPE_NUMERIC], - 'invalid array' => [[], DataType::TYPE_STRING], - 'invalid unstringable object' => [new DateTime(), DataType::TYPE_INLINE], + 'invalid numeric' => ['XYZ', DataType::TYPE_NUMERIC, 'Invalid numeric value'], + 'invalid array' => [[], DataType::TYPE_STRING, 'Unable to convert to string'], + 'invalid unstringable object' => [new DateTime(), DataType::TYPE_INLINE, 'Unable to convert to string'], ];