Simplified CellTest and added a helper method for styles

This commit is contained in:
zhukoff74
2025-12-03 22:54:20 +03:00
parent a56cbaccb1
commit 873aa649fc
+25 -39
View File
@@ -36,6 +36,16 @@ class CellTest extends TestCase
}
}
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 @@ class CellTest extends TestCase
$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 @@ class CellTest extends TestCase
$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 @@ class CellTest extends TestCase
$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 @@ class CellTest extends TestCase
$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 @@ class CellTest extends TestCase
/** @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())