mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 20:18:00 +00:00
d647fe7ee7
With PhpUnit 10 came the ability to use Php attributes rather than doc-block annotations for things like "data provider". PhpUnit 11 deprecates the use of annotations, and PhpUnit 12 will not not permit their use. Since PhpUnit 11 requires Php8.2+, we cannot adopt it as long as we support Php8.1, which will continue to be the case for some time. However, there is no penalty for early adoption. Php-cs-fixer can use: ``` 'php_unit_attributes' => ['keep_annotations' => false], ``` This allows us to run `composer fix` to automate all the needed changes. No manual changes were needed for any of the test members. With this change, PhpUnit 9 can no longer be used with the test suite. File composer.json is updated to reflect that reality, and phpunit9.xml.dist, which has been supplied in case anyone needed to use PhpUnit 9, is no longer required, and is thus deleted. For now, PhpUnit 11 is not being added as a possibility. No source code is changed in this PR.
71 lines
3.1 KiB
PHP
71 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Functional;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
class ConditionalStopIfTrueTest extends AbstractFunctional
|
|
{
|
|
const COLOR_GREEN = 'FF99FF66';
|
|
const COLOR_RED = 'FFFF5050';
|
|
|
|
public static function providerFormats(): array
|
|
{
|
|
return [
|
|
['Xlsx'],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerFormats')]
|
|
public function testConditionalStopIfTrue(string $format): void
|
|
{
|
|
$pCoordinate = 'A1:A3';
|
|
|
|
// if blank cell -> no styling
|
|
$condition0 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
|
|
$condition0->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION);
|
|
$condition0->addCondition('LEN(TRIM(A1))=0');
|
|
$condition0->setStopIfTrue(true); // ! stop here
|
|
|
|
// if value below 0.6 (matches also blank cells!) -> red background
|
|
$condition1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
|
|
$condition1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
|
|
$condition1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN);
|
|
$condition1->addCondition(0.6);
|
|
$condition1->getStyle()->getFill()
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
|
->getStartColor()->setARGB(self::COLOR_RED);
|
|
|
|
// if value above 0.6 -> green background
|
|
$condition2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
|
|
$condition2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
|
|
$condition2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_GREATERTHAN);
|
|
$condition2->addCondition(0.6);
|
|
$condition2->getStyle()->getFill()
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
|
->getStartColor()->setARGB(self::COLOR_GREEN);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->getCell('A1')->setValue(0.7);
|
|
$spreadsheet->getActiveSheet()->getCell('A2')->setValue('');
|
|
$spreadsheet->getActiveSheet()->getCell('A3')->setValue(0.4);
|
|
|
|
// put all three conditions in sheet
|
|
$conditionalStyles = [];
|
|
array_push($conditionalStyles, $condition0);
|
|
array_push($conditionalStyles, $condition1);
|
|
array_push($conditionalStyles, $condition2);
|
|
$spreadsheet->getActiveSheet()->setConditionalStyles($pCoordinate, $conditionalStyles);
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
|
|
|
|
// see if we successfully written "StopIfTrue"
|
|
$newConditionalStyles = $reloadedSpreadsheet->getActiveSheet()->getConditionalStyles($pCoordinate);
|
|
self::assertTrue($newConditionalStyles[0]->getStopIfTrue(), 'StopIfTrue should be set (=true) on first condition');
|
|
self::assertFalse($newConditionalStyles[1]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on second condition');
|
|
self::assertFalse($newConditionalStyles[2]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on third condition');
|
|
}
|
|
}
|