mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 19:46:31 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
92 lines
3.2 KiB
PHP
92 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Color;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ConditionalStyleTest extends TestCase
|
|
{
|
|
/**
|
|
* @var Spreadsheet
|
|
*/
|
|
protected $spreadsheet;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->spreadsheet = new Spreadsheet();
|
|
|
|
$conditional1 = new Conditional();
|
|
$conditional1->setConditionType(Conditional::CONDITION_CELLIS);
|
|
$conditional1->setOperatorType(Conditional::OPERATOR_LESSTHAN);
|
|
$conditional1->addCondition('0');
|
|
$conditional1->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_RED);
|
|
$conditional1->getStyle()->getFont()->setBold(true);
|
|
|
|
$conditional2 = new Conditional();
|
|
$conditional2->setConditionType(Conditional::CONDITION_CELLIS);
|
|
$conditional2->setOperatorType(Conditional::OPERATOR_EQUAL);
|
|
$conditional2->addCondition('0');
|
|
$conditional2->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_YELLOW);
|
|
$conditional2->getStyle()->getFont()->setBold(true);
|
|
|
|
$conditional3 = new Conditional();
|
|
$conditional3->setConditionType(Conditional::CONDITION_CELLIS);
|
|
$conditional3->setOperatorType(Conditional::OPERATOR_GREATERTHAN);
|
|
$conditional3->addCondition('0');
|
|
$conditional3->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_GREEN);
|
|
$conditional3->getStyle()->getFont()->setBold(true);
|
|
|
|
$conditionalStyles = $this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->getConditionalStyles();
|
|
$conditionalStyles[] = $conditional1;
|
|
$conditionalStyles[] = $conditional2;
|
|
$conditionalStyles[] = $conditional3;
|
|
|
|
$this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->setConditionalStyles($conditionalStyles);
|
|
|
|
$this->spreadsheet->getActiveSheet()
|
|
->duplicateConditionalStyle(
|
|
$this->spreadsheet->getActiveSheet()->getConditionalStyles('A1:C3'),
|
|
'F1'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider cellConditionalStylesProvider
|
|
*/
|
|
public function testCellHasConditionalStyles(string $cellReference, bool $expectedHasConditionalStyles): void
|
|
{
|
|
$cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->conditionalStylesExists($cellReference);
|
|
|
|
self::assertSame($expectedHasConditionalStyles, $cellHasConditionalStyles);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider cellConditionalStylesProvider
|
|
*/
|
|
public function testCellGetConditionalStyles(string $cellReference, bool $expectedGetConditionalStyles): void
|
|
{
|
|
$cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->getConditionalStyles($cellReference);
|
|
|
|
self::assertSame($expectedGetConditionalStyles, !empty($cellHasConditionalStyles));
|
|
}
|
|
|
|
public static function cellConditionalStylesProvider(): array
|
|
{
|
|
return [
|
|
['A1', true],
|
|
['B2', true],
|
|
['B4', false],
|
|
['A1:C3', true],
|
|
['A1:B2', false],
|
|
['F1', true],
|
|
['F2', false],
|
|
['A1:F1', false],
|
|
];
|
|
}
|
|
}
|