Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xls/ConditionalFormattingBasicTest.php
oleibman 5c94de34a9 More Phpstan Level 10 Prep - Readers
Also upgrade Phpstan.
2025-05-03 21:18:55 -07:00

208 lines
8.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class ConditionalFormattingBasicTest extends TestCase
{
/** @param mixed[][] $expectedRules */
#[DataProvider('conditionalFormattingProvider')]
public function testReadConditionalFormatting(string $expectedRange, array $expectedRules): void
{
$filename = 'tests/data/Reader/XLS/CF_Basic_Comparisons.xls';
$reader = new Xls();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$hasConditionalStyles = $sheet->conditionalStylesExists($expectedRange);
self::assertTrue($hasConditionalStyles);
$conditionalStyles = $sheet->getConditionalStyles($expectedRange);
foreach ($conditionalStyles as $index => $conditionalStyle) {
self::assertSame($expectedRules[$index]['type'], $conditionalStyle->getConditionType());
self::assertSame($expectedRules[$index]['operator'], $conditionalStyle->getOperatorType());
self::assertSame($expectedRules[$index]['conditions'], $conditionalStyle->getConditions());
}
$spreadsheet->disconnectWorksheets();
}
public static function conditionalFormattingProvider(): array
{
return [
[
'A2:E5',
[
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_EQUAL,
'conditions' => [
0,
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_GREATERTHAN,
'conditions' => [
0,
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_LESSTHAN,
'conditions' => [
0,
],
],
],
],
[
'A10:E13',
[
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_EQUAL,
'conditions' => [
'$H$9',
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_GREATERTHAN,
'conditions' => [
'$H$9',
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_LESSTHAN,
'conditions' => [
'$H$9',
],
],
],
],
[
'A18:A20',
[
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_BETWEEN,
'conditions' => [
'$B1',
'$C1',
],
],
],
],
[
'A24:E27',
[
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_BETWEEN,
'conditions' => [
'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_GREATERTHAN,
'conditions' => [
'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_LESSTHAN,
'conditions' => [
'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
],
],
],
],
[
'A31:A33',
[
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_EQUAL,
'conditions' => [
'"LOVE"',
],
],
[
'type' => Conditional::CONDITION_CELLIS,
'operator' => Conditional::OPERATOR_EQUAL,
'conditions' => [
'"PHP"',
],
],
],
],
];
}
public function testReadConditionalFormattingStyles(): void
{
$filename = 'tests/data/Reader/XLS/CF_Basic_Comparisons.xls';
$reader = new Xls();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$expectedRange = 'A2:E5';
$hasConditionalStyles = $sheet->conditionalStylesExists($expectedRange);
self::assertTrue($hasConditionalStyles);
$conditionalStyles = $sheet->getConditionalStyles($expectedRange);
self::assertCount(3, $conditionalStyles);
$style = $conditionalStyles[0]->getStyle();
$font = $style->getFont();
self::assertSame('FF0000FF', $font->getColor()->getArgb());
self::assertNull($font->getItalic());
self::assertNull($font->getStrikethrough());
// Fill not handled correctly - forget for now
$borders = $style->getBorders();
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
self::assertNull($style->getNumberFormat()->getFormatCode());
$style = $conditionalStyles[1]->getStyle();
$font = $style->getFont();
self::assertSame('FF800000', $font->getColor()->getArgb());
self::assertNull($font->getItalic());
self::assertNull($font->getStrikethrough());
// Fill not handled correctly - forget for now
$borders = $style->getBorders();
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
self::assertNull($style->getNumberFormat()->getFormatCode());
$style = $conditionalStyles[2]->getStyle();
$font = $style->getFont();
self::assertSame('FF00FF00', $font->getColor()->getArgb());
self::assertNull($font->getItalic());
self::assertNull($font->getStrikethrough());
// Fill not handled correctly - forget for now
$borders = $style->getBorders();
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
self::assertNull($style->getNumberFormat()->getFormatCode());
$spreadsheet->disconnectWorksheets();
}
}