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 = <<5 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 = <<-55 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 = <<NOT(ISERROR(SEARCH("PHP",C3))) XML; self::assertStringContainsString($expected, $data); } /** * @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 function textConditionalsProvider(): array { return [ 'Contains' => [ Conditional::CONDITION_CONTAINSTEXT, Conditional::OPERATOR_CONTAINSTEXT, <<NOT(ISERROR(SEARCH("PHP",C3))) XML ], 'Not Contains' => [ Conditional::CONDITION_NOTCONTAINSTEXT, Conditional::OPERATOR_NOTCONTAINS, <<ISERROR(SEARCH("PHP",C3)) XML ], 'Begins With' => [ Conditional::CONDITION_BEGINSWITH, Conditional::OPERATOR_BEGINSWITH, <<LEFT(C3,LEN("PHP"))="PHP" XML ], 'Ends With' => [ Conditional::CONDITION_ENDSWITH, Conditional::OPERATOR_ENDSWITH, <<RIGHT(C3,LEN("PHP"))="PHP" 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 = <<FLOOR(C3,1)=TODAY() XML; self::assertStringContainsString($expected, $data); } /** * @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 function dateConditionalsProvider(): array { return [ 'Yesterday' => [ Conditional::TIMEPERIOD_YESTERDAY, <<FLOOR(C3)=TODAY()-1 XML ], 'Today' => [ Conditional::TIMEPERIOD_TODAY, <<FLOOR(C3)=TODAY() XML ], 'Tomorrow' => [ Conditional::TIMEPERIOD_TOMORROW, <<FLOOR(C3)=TODAY()+1 XML ], 'Last 7 Days' => [ Conditional::TIMEPERIOD_LAST_7_DAYS, <<AND(TODAY()-FLOOR(C3,1)<=6,FLOOR(C3,1)<=TODAY()) XML ], 'Last Week' => [ Conditional::TIMEPERIOD_LAST_WEEK, <<AND(TODAY()-ROUNDDOWN(C3,0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(C3,0)<(WEEKDAY(TODAY())+7)) XML ], 'This Week' => [ Conditional::TIMEPERIOD_THIS_WEEK, <<AND(TODAY()-ROUNDDOWN(C3,0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(C3,0)-TODAY()<=7-WEEKDAY(TODAY())) XML ], 'Next Week' => [ Conditional::TIMEPERIOD_NEXT_WEEK, <<AND(ROUNDDOWN(C3,0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(C3,0)-TODAY()<(15-WEEKDAY(TODAY()))) XML ], 'Last Month' => [ Conditional::TIMEPERIOD_LAST_MONTH, <<AND(MONTH(C3)=MONTH(EDATE(TODAY(),0-1)),YEAR(C3)=YEAR(EDATE(TODAY(),0-1))) XML ], 'This Month' => [ Conditional::TIMEPERIOD_THIS_MONTH, <<AND(MONTH(C3)=MONTH(TODAY()),YEAR(C3)=YEAR(TODAY())) XML ], 'Next Month' => [ Conditional::TIMEPERIOD_NEXT_MONTH, <<AND(MONTH(C3)=MONTH(EDATE(TODAY(),0+1)),YEAR(C3)=YEAR(EDATE(TODAY(),0+1))) 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 = <<LEN(TRIM(C3))=0 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 = <<LEN(TRIM(C3))>0 XML; self::assertStringContainsString($expected, $data); } /** * @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 function blanksConditionalsProvider(): array { return [ 'Blanks' => [ Conditional::CONDITION_CONTAINSBLANKS, <<LEN(TRIM(C3))=0 XML ], 'Not Blanks' => [ Conditional::CONDITION_NOTCONTAINSBLANKS, <<LEN(TRIM(C3))>0 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 = <<NOT(ISERROR(C3)) 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 = <<ISERROR(C3) XML; self::assertStringContainsString($expected, $data); } /** * @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 function errorsConditionalsProvider(): array { return [ 'Errors' => [ Conditional::CONDITION_CONTAINSERRORS, <<ISERROR(C3) XML ], 'Not Errors' => [ Conditional::CONDITION_NOTCONTAINSERRORS, <<NOT(ISERROR(C3)) 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; 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; self::assertStringContainsString($expected, $data); } /** * @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 function duplicatesConditionalsProvider(): array { return [ 'Duplicates' => [ Conditional::CONDITION_DUPLICATES, << XML ], 'Unique' => [ Conditional::CONDITION_UNIQUE, << 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 = <<ISODD(C3) XML; self::assertStringContainsString($expected, $data); } /** * @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 function expressionsConditionalsProvider(): array { return [ 'Odd' => [ 'ISODD(C3)', <<ISODD(C3) XML ], 'Even' => [ 'ISEVEN(C3)', <<ISEVEN(C3) XML ], ]; } }