Skip to content

Commit dde7fe1

Browse files
authored
Merge pull request #4734 from zhukoff74/refactor_CellTest
Simplified CellTest and added a helper method for styles
2 parents a56cbac + c1be7a5 commit dde7fe1

File tree

2 files changed

+41
-54
lines changed

2 files changed

+41
-54
lines changed

tests/PhpSpreadsheetTests/Cell/CellTest.php

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ protected function tearDown(): void
3636
}
3737
}
3838

39+
private function createSolidFillStyle(string $color): Style
40+
{
41+
$style = new Style(false, true);
42+
$style->getFill()
43+
->setFillType(Fill::FILL_SOLID)
44+
->getStartColor()->setARGB($color);
45+
46+
return $style;
47+
}
48+
3949
public function testSetValueBinderOverride(): void
4050
{
4151
$value = '12.5%';
@@ -105,9 +115,10 @@ public function testInvalidIsoDateSetValueExplicit(): void
105115
}
106116

107117
#[DataProvider('providerSetValueExplicitException')]
108-
public function testSetValueExplicitException(mixed $value, string $dataType): void
118+
public function testSetValueExplicitException(mixed $value, string $dataType, string $message): void
109119
{
110120
$this->expectException(Exception::class);
121+
$this->expectExceptionMessage($message);
111122

112123
$this->spreadsheet = new Spreadsheet();
113124
$cell = $this->spreadsheet->getActiveSheet()->getCell('A1');
@@ -130,12 +141,11 @@ public function testNoChangeToActiveSheet(): void
130141
$sheet1->setCellValue('D1', 124);
131142
$sheet3->setCellValue('A1', "='Sheet 1'!C1+'Sheet 1'!D1");
132143
$sheet1->setCellValue('A1', "='Sheet 3'!A1");
133-
$cell = 'A1';
134144
$spreadsheet->setActiveSheetIndex(0);
135-
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
136-
$value = $spreadsheet->getActiveSheet()->getCell($cell)->getCalculatedValue();
137-
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
138-
self::assertEquals(247, $value);
145+
self::assertSame(0, $spreadsheet->getActiveSheetIndex());
146+
$value = $spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
147+
self::assertSame(0, $spreadsheet->getActiveSheetIndex());
148+
self::assertSame(247, $value);
139149
$spreadsheet->disconnectWorksheets();
140150
}
141151

@@ -172,12 +182,9 @@ public function testDestroyCell2(): void
172182
$this->expectException(Exception::class);
173183
$this->expectExceptionMessage('Coordinate no longer exists');
174184
$parent = $cell->getParent();
175-
if ($parent === null) {
176-
self::fail('Unexpected null parent');
177-
} else {
178-
$parent->delete('A1');
179-
$cell->getCoordinate();
180-
}
185+
self::assertNotNull($parent, 'Parent should not be null');
186+
$parent->delete('A1');
187+
$cell->getCoordinate();
181188
}
182189

183190
public function testAppliedStyleWithRange(): void
@@ -191,56 +198,44 @@ public function testAppliedStyleWithRange(): void
191198
$cellRange = 'A1:A3';
192199
$sheet->getStyle($cellRange)->getFont()->setBold(true);
193200

194-
$yellowStyle = new Style(false, true);
195-
$yellowStyle->getFill()
196-
->setFillType(Fill::FILL_SOLID)
197-
->getStartColor()->setARGB(Color::COLOR_YELLOW);
198-
$greenStyle = new Style(false, true);
199-
$greenStyle->getFill()
200-
->setFillType(Fill::FILL_SOLID)
201-
->getStartColor()->setARGB(Color::COLOR_GREEN);
202-
$redStyle = new Style(false, true);
203-
$redStyle->getFill()
204-
->setFillType(Fill::FILL_SOLID)
205-
->getStartColor()->setARGB(Color::COLOR_RED);
201+
$yellowStyle = $this->createSolidFillStyle(Color::COLOR_YELLOW);
202+
$greenStyle = $this->createSolidFillStyle(Color::COLOR_GREEN);
203+
$redStyle = $this->createSolidFillStyle(Color::COLOR_RED);
206204

207-
$conditionalStyles = [];
208205
$wizardFactory = new Wizard($cellRange);
209206
/** @var Wizard\CellValue $cellWizard */
210207
$cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
211208

212-
$cellWizard->equals(0)
213-
->setStyle($yellowStyle);
209+
$conditionalStyles = [];
210+
$cellWizard->equals(0)->setStyle($yellowStyle);
214211
$conditionalStyles[] = $cellWizard->getConditional();
215212

216-
$cellWizard->greaterThan(0)
217-
->setStyle($greenStyle);
213+
$cellWizard->greaterThan(0)->setStyle($greenStyle);
218214
$conditionalStyles[] = $cellWizard->getConditional();
219215

220-
$cellWizard->lessThan(0)
221-
->setStyle($redStyle);
216+
$cellWizard->lessThan(0)->setStyle($redStyle);
222217
$conditionalStyles[] = $cellWizard->getConditional();
223218

224219
$sheet->getStyle($cellWizard->getCellRange())
225220
->setConditionalStyles($conditionalStyles);
226221

227222
$style = $sheet->getCell('A1')->getAppliedStyle();
228223
self::assertTrue($style->getFont()->getBold());
229-
self::assertEquals($redStyle->getFill()->getFillType(), $style->getFill()->getFillType());
230-
self::assertEquals($redStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB());
224+
self::assertSame($redStyle->getFill()->getFillType(), $style->getFill()->getFillType());
225+
self::assertSame($redStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB());
231226

232227
$style = $sheet->getCell('A2')->getAppliedStyle();
233228
self::assertTrue($style->getFont()->getBold());
234-
self::assertEquals($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType());
235-
self::assertEquals(
229+
self::assertSame($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType());
230+
self::assertSame(
236231
$yellowStyle->getFill()->getStartColor()->getARGB(),
237232
$style->getFill()->getStartColor()->getARGB()
238233
);
239234

240235
$style = $sheet->getCell('A3')->getAppliedStyle();
241236
self::assertTrue($style->getFont()->getBold());
242-
self::assertEquals($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType());
243-
self::assertEquals(
237+
self::assertSame($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType());
238+
self::assertSame(
244239
$greenStyle->getFill()->getStartColor()->getARGB(),
245240
$style->getFill()->getStartColor()->getARGB()
246241
);
@@ -261,27 +256,19 @@ public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyl
261256
$cellRange = 'A1:C2';
262257
$sheet->getStyle($cellRange)->getFont()->setBold(true);
263258

264-
$yellowStyle = new Style(false, true);
265-
$yellowStyle->getFill()
266-
->setFillType(Fill::FILL_SOLID)
267-
->getStartColor()->setARGB(Color::COLOR_YELLOW);
268-
$redStyle = new Style(false, true);
269-
$redStyle->getFill()
270-
->setFillType(Fill::FILL_SOLID)
271-
->getStartColor()->setARGB(Color::COLOR_RED);
259+
$yellowStyle = $this->createSolidFillStyle(Color::COLOR_YELLOW);
260+
$redStyle = $this->createSolidFillStyle(Color::COLOR_RED);
272261

273262
$conditionalCellRange = 'A1:C1';
274263
$conditionalStyles = [];
275264
$wizardFactory = new Wizard($conditionalCellRange);
276265
/** @var Wizard\CellValue $cellWizard */
277266
$cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
278267

279-
$cellWizard->equals(0)
280-
->setStyle($yellowStyle);
268+
$cellWizard->equals(0)->setStyle($yellowStyle);
281269
$conditionalStyles[] = $cellWizard->getConditional();
282270

283-
$cellWizard->lessThan(0)
284-
->setStyle($redStyle);
271+
$cellWizard->lessThan(0)->setStyle($redStyle);
285272
$conditionalStyles[] = $cellWizard->getConditional();
286273

287274
$sheet->getStyle($cellWizard->getCellRange())
@@ -290,9 +277,9 @@ public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyl
290277
$style = $sheet->getCell($cellAddress)->getAppliedStyle();
291278

292279
self::assertTrue($style->getFont()->getBold());
293-
self::assertEquals($fillStyle, $style->getFill()->getFillType());
280+
self::assertSame($fillStyle, $style->getFill()->getFillType());
294281
if ($fillStyle === Fill::FILL_SOLID) {
295-
self::assertEquals($fillColor, $style->getFill()->getStartColor()->getARGB());
282+
self::assertSame($fillColor, $style->getFill()->getStartColor()->getARGB());
296283
}
297284
$spreadsheet->disconnectWorksheets();
298285
}

tests/data/Cell/SetValueExplicitException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PhpOffice\PhpSpreadsheet\Cell\DataType;
66

77
return [
8-
'invalid numeric' => ['XYZ', DataType::TYPE_NUMERIC],
9-
'invalid array' => [[], DataType::TYPE_STRING],
10-
'invalid unstringable object' => [new DateTime(), DataType::TYPE_INLINE],
8+
'invalid numeric' => ['XYZ', DataType::TYPE_NUMERIC, 'Invalid numeric value'],
9+
'invalid array' => [[], DataType::TYPE_STRING, 'Unable to convert to string'],
10+
'invalid unstringable object' => [new DateTime(), DataType::TYPE_INLINE, 'Unable to convert to string'],
1111
];

0 commit comments

Comments
 (0)