mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-01 13:08:50 +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+.
73 lines
3.1 KiB
PHP
73 lines
3.1 KiB
PHP
<?php
|
|
|
|
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'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerFormats
|
|
*
|
|
* @param string $format
|
|
*/
|
|
public function testConditionalStopIfTrue($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)
|
|
->getEndColor()->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)
|
|
->getEndColor()->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');
|
|
}
|
|
}
|