mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 03:26:26 +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.
550 lines
23 KiB
PHP
550 lines
23 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Style\Style;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
|
|
|
class ConditionalTest extends AbstractFunctional
|
|
{
|
|
protected string $cellRange;
|
|
|
|
protected Style $style;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->cellRange = 'C3:E5';
|
|
$this->style = new Style();
|
|
$this->style->applyFromArray([
|
|
'fill' => [
|
|
'color' => ['argb' => 'FFFFC000'],
|
|
'fillType' => Fill::FILL_SOLID,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function testWriteSimpleCellConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\CellValue($this->cellRange);
|
|
$wizard->greaterThan(5);
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="cellIs" dxfId="" priority="1" operator="greaterThan"><formula>5</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public function testWriteBetweenCellConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\CellValue($this->cellRange);
|
|
$wizard->between(-5)->and(5);
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="cellIs" dxfId="" priority="1" operator="between"><formula>-5</formula><formula>5</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public function testWriteTextConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\TextValue($this->cellRange);
|
|
$wizard->contains('PHP');
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsText" dxfId="" priority="1" operator="containsText" text="PHP"><formula>NOT(ISERROR(SEARCH("PHP",C3)))</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('textConditionalsProvider')]
|
|
public function testWriteTextConditionals(string $conditionType, string $operatorType, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType($conditionType);
|
|
$condition->setOperatorType($operatorType);
|
|
$condition->setText('PHP');
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function textConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Contains' => [
|
|
Conditional::CONDITION_CONTAINSTEXT,
|
|
Conditional::OPERATOR_CONTAINSTEXT,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsText" dxfId="" priority="1" operator="containsText" text="PHP"><formula>NOT(ISERROR(SEARCH("PHP",C3)))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Not Contains' => [
|
|
Conditional::CONDITION_NOTCONTAINSTEXT,
|
|
Conditional::OPERATOR_NOTCONTAINS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="notContainsText" dxfId="" priority="1" operator="notContains" text="PHP"><formula>ISERROR(SEARCH("PHP",C3))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Begins With' => [
|
|
Conditional::CONDITION_BEGINSWITH,
|
|
Conditional::OPERATOR_BEGINSWITH,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="beginsWith" dxfId="" priority="1" operator="beginsWith" text="PHP"><formula>LEFT(C3,LEN("PHP"))="PHP"</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Ends With' => [
|
|
Conditional::CONDITION_ENDSWITH,
|
|
Conditional::OPERATOR_ENDSWITH,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="endsWith" dxfId="" priority="1" operator="endsWith" text="PHP"><formula>RIGHT(C3,LEN("PHP"))="PHP"</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testWriteDateConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\DateValue($this->cellRange);
|
|
$wizard->today();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="today"><formula>FLOOR(C3,1)=TODAY()</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dateConditionalsProvider')]
|
|
public function testWriteDateConditionals(string $timePeriod, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType(Conditional::CONDITION_TIMEPERIOD);
|
|
$condition->setOperatorType($timePeriod);
|
|
$condition->setText($timePeriod);
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function dateConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Yesterday' => [
|
|
Conditional::TIMEPERIOD_YESTERDAY,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="yesterday"><formula>FLOOR(C3)=TODAY()-1</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Today' => [
|
|
Conditional::TIMEPERIOD_TODAY,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="today"><formula>FLOOR(C3)=TODAY()</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Tomorrow' => [
|
|
Conditional::TIMEPERIOD_TOMORROW,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="tomorrow"><formula>FLOOR(C3)=TODAY()+1</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Last 7 Days' => [
|
|
Conditional::TIMEPERIOD_LAST_7_DAYS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="last7Days"><formula>AND(TODAY()-FLOOR(C3,1)<=6,FLOOR(C3,1)<=TODAY())</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Last Week' => [
|
|
Conditional::TIMEPERIOD_LAST_WEEK,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="lastWeek"><formula>AND(TODAY()-ROUNDDOWN(C3,0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(C3,0)<(WEEKDAY(TODAY())+7))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'This Week' => [
|
|
Conditional::TIMEPERIOD_THIS_WEEK,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="thisWeek"><formula>AND(TODAY()-ROUNDDOWN(C3,0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(C3,0)-TODAY()<=7-WEEKDAY(TODAY()))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Next Week' => [
|
|
Conditional::TIMEPERIOD_NEXT_WEEK,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="nextWeek"><formula>AND(ROUNDDOWN(C3,0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(C3,0)-TODAY()<(15-WEEKDAY(TODAY())))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Last Month' => [
|
|
Conditional::TIMEPERIOD_LAST_MONTH,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="lastMonth"><formula>AND(MONTH(C3)=MONTH(EDATE(TODAY(),0-1)),YEAR(C3)=YEAR(EDATE(TODAY(),0-1)))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'This Month' => [
|
|
Conditional::TIMEPERIOD_THIS_MONTH,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="thisMonth"><formula>AND(MONTH(C3)=MONTH(TODAY()),YEAR(C3)=YEAR(TODAY()))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Next Month' => [
|
|
Conditional::TIMEPERIOD_NEXT_MONTH,
|
|
<<<XML
|
|
><conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="nextMonth"><formula>AND(MONTH(C3)=MONTH(EDATE(TODAY(),0+1)),YEAR(C3)=YEAR(EDATE(TODAY(),0+1)))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testWriteBlankConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Blanks($this->cellRange);
|
|
$wizard->isBlank();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))=0</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public function testWriteNonBlankConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Blanks($this->cellRange);
|
|
$wizard->notBlank();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="notContainsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))>0</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('blanksConditionalsProvider')]
|
|
public function testWriteBlanksConditionals(string $conditionalType, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType($conditionalType);
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function blanksConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Blanks' => [
|
|
Conditional::CONDITION_CONTAINSBLANKS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))=0</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Not Blanks' => [
|
|
Conditional::CONDITION_NOTCONTAINSBLANKS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="notContainsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))>0</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testWriteNonErrorConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Errors($this->cellRange);
|
|
$wizard->notError();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="notContainsErrors" dxfId="" priority="1"><formula>NOT(ISERROR(C3))</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public function testWriteErrorConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Errors($this->cellRange);
|
|
$wizard->isError();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsErrors" dxfId="" priority="1"><formula>ISERROR(C3)</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('errorsConditionalsProvider')]
|
|
public function testWriteErrorsConditionals(string $conditionalType, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType($conditionalType);
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function errorsConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Errors' => [
|
|
Conditional::CONDITION_CONTAINSERRORS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="containsErrors" dxfId="" priority="1"><formula>ISERROR(C3)</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Not Errors' => [
|
|
Conditional::CONDITION_NOTCONTAINSERRORS,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="notContainsErrors" dxfId="" priority="1"><formula>NOT(ISERROR(C3))</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testWriteUniqueConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Duplicates($this->cellRange);
|
|
$wizard->unique();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="uniqueValues" dxfId="" priority="1"/></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public function testWriteDuplicateConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Duplicates($this->cellRange);
|
|
$wizard->duplicates();
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="duplicateValues" dxfId="" priority="1"/></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('duplicatesConditionalsProvider')]
|
|
public function testWriteDuplicatesConditionals(string $conditionalType, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType($conditionalType);
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function duplicatesConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Duplicates' => [
|
|
Conditional::CONDITION_DUPLICATES,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="duplicateValues" dxfId="" priority="1"/></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Unique' => [
|
|
Conditional::CONDITION_UNIQUE,
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="uniqueValues" dxfId="" priority="1"/></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testWriteExpressionConditionalFromWizard(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$wizard = new Wizard\Expression($this->cellRange);
|
|
$wizard->expression('=ISODD(A1)');
|
|
$condition = $wizard->getConditional();
|
|
$condition->setStyle($this->style);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
$expected = <<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISODD(C3)</formula></cfRule></conditionalFormatting>
|
|
XML;
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('expressionsConditionalsProvider')]
|
|
public function testWriteExpressionConditionals(string $expression, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$condition = new Conditional();
|
|
$condition->setConditionType(Conditional::CONDITION_EXPRESSION);
|
|
$condition->setStyle($this->style);
|
|
$condition->setConditions([$expression]);
|
|
$worksheet->setConditionalStyles($this->cellRange, [$condition]);
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
$writerWorksheet = new Xlsx\Worksheet($writer);
|
|
$data = $writerWorksheet->writeWorksheet($worksheet, []);
|
|
|
|
self::assertStringContainsString($expected, $data);
|
|
}
|
|
|
|
public static function expressionsConditionalsProvider(): array
|
|
{
|
|
return [
|
|
'Odd' => [
|
|
'ISODD(C3)',
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISODD(C3)</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
'Even' => [
|
|
'ISEVEN(C3)',
|
|
<<<XML
|
|
<conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISEVEN(C3)</formula></cfRule></conditionalFormatting>
|
|
XML
|
|
],
|
|
];
|
|
}
|
|
}
|