Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Style/ConditionalFormatting/Wizard/TextValueWizardTest.php
Mark Baker 4a04499bff Read conditional styling for cell (#2491)
* Allow single-cell checks on conditional styles, even when the style is configured for a range of cells
* Work on the CellMatcher logic to evaluate Conditionals for a cell based on its value, and identify which conditional styles should be applied
* Refactor style merging and cell matching for conditional formatting into separate classes; this should make it easier to test, and easier to extend for other CF expressions subsequently
* Added support for containsErrors and notContainsErrors
* Initial work on a wizard to help simplify created Conditional Formatting rules, to ensure that the correct expressions are set
* Further work on extending the Conditional Formatting rules to cover more of the options that are available in MS Excel
* Prevent phpcs-fixer from removing class @method annotations, used to identify the signature for magic methods used in Wizard classes
* Implement `fromConditional()`` method to allow the creation of a CF Wizard from an existing Conditional
* Ensure that xlsx Reader picks up the timePeriod attribute for DatesOccurring CF Rules
* Allow Duplicates/Uniques CF Rules to be recognised in the Xlsx Reader
* Basic Xlsx reading of CF Rules/Styles from <extLst><ext><ConditinalFormattings> element, and not just the <ConditinalFormatting> element of the worksheet

* Add some validation for operands passed to the CF Wizards
 - remove any leading ``=` from formulae, because they'll be embedded into other formulae
 - unwrap any string literals from quotes, because that's also handled internally

Handle cross-worksheet cell references in cellReferences and Formulae/Expressions

* re-baseline phpstan

* Update Change Log with details of the CF Improvements
2022-01-22 19:18:26 +01:00

154 lines
6.0 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PHPUnit\Framework\TestCase;
class TextValueWizardTest extends TestCase
{
/**
* @var Style
*/
protected $style;
/**
* @var string
*/
protected $range = '$C$3:$E$5';
/**
* @var Wizard
*/
protected $wizardFactory;
protected function setUp(): void
{
$this->wizardFactory = new Wizard($this->range);
$this->style = new Style();
}
public function testTextContainsWizardWithText(): void
{
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $textWizard */
$textWizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf(Wizard\TextValue::class, $textWizard);
$textWizard->setStyle($this->style);
$textWizard->contains('LL');
$conditional = $textWizard->getConditional();
self::assertSame(Conditional::CONDITION_CONTAINSTEXT, $conditional->getConditionType());
self::assertSame(Conditional::OPERATOR_CONTAINSTEXT, $conditional->getOperatorType());
self::assertSame('LL', $conditional->getText());
$conditions = $conditional->getConditions();
self::assertSame(['NOT(ISERROR(SEARCH("LL",C3)))'], $conditions);
$newWizard = Wizard::fromConditional($conditional, $this->range);
$newWizard->getConditional();
self::assertEquals($newWizard, $textWizard, 'fromConditional() Failure');
}
public function testTextContainsWizardWithCellReference(): void
{
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $textWizard */
$textWizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf(Wizard\TextValue::class, $textWizard);
$textWizard->setStyle($this->style);
$textWizard->contains('$A1', Wizard::VALUE_TYPE_CELL);
$conditional = $textWizard->getConditional();
self::assertSame(Conditional::CONDITION_CONTAINSTEXT, $conditional->getConditionType());
self:self::assertSame(Conditional::OPERATOR_CONTAINSTEXT, $conditional->getOperatorType());
self::assertSame('$A3', $conditional->getText());
$conditions = $conditional->getConditions();
self::assertSame(['NOT(ISERROR(SEARCH($A3,C3)))'], $conditions);
$newWizard = Wizard::fromConditional($conditional, $this->range);
$newWizard->getConditional();
self::assertEquals($newWizard, $textWizard, 'fromConditional() Failure');
}
public function testTextNotContainsWizardWithText(): void
{
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $textWizard */
$textWizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf(Wizard\TextValue::class, $textWizard);
$textWizard->setStyle($this->style);
$textWizard->doesNotContain('LL');
$conditional = $textWizard->getConditional();
self::assertSame(Conditional::CONDITION_NOTCONTAINSTEXT, $conditional->getConditionType());
self:self::assertSame(Conditional::OPERATOR_NOTCONTAINS, $conditional->getOperatorType());
self::assertSame('LL', $conditional->getText());
$conditions = $conditional->getConditions();
self::assertSame(['ISERROR(SEARCH("LL",C3))'], $conditions);
$newWizard = Wizard::fromConditional($conditional, $this->range);
$newWizard->getConditional();
self::assertEquals($newWizard, $textWizard, 'fromConditional() Failure');
}
public function testTextBeginsWithWizardWithText(): void
{
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $textWizard */
$textWizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf(Wizard\TextValue::class, $textWizard);
$textWizard->setStyle($this->style);
$textWizard->beginsWith('LL');
$conditional = $textWizard->getConditional();
self::assertSame(Conditional::CONDITION_BEGINSWITH, $conditional->getConditionType());
self:self::assertSame(Conditional::OPERATOR_BEGINSWITH, $conditional->getOperatorType());
self::assertSame('LL', $conditional->getText());
$conditions = $conditional->getConditions();
self::assertSame(['LEFT(C3,LEN("LL"))="LL"'], $conditions);
$newWizard = Wizard::fromConditional($conditional, $this->range);
$newWizard->getConditional();
self::assertEquals($newWizard, $textWizard, 'fromConditional() Failure');
}
public function testTextEndsWithWizardWithText(): void
{
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $textWizard */
$textWizard = $this->wizardFactory->newRule($ruleType);
self::assertInstanceOf(Wizard\TextValue::class, $textWizard);
$textWizard->setStyle($this->style);
$textWizard->endsWith('LL');
$conditional = $textWizard->getConditional();
self::assertSame(Conditional::CONDITION_ENDSWITH, $conditional->getConditionType());
self:self::assertSame(Conditional::OPERATOR_ENDSWITH, $conditional->getOperatorType());
$conditions = $conditional->getConditions();
self::assertSame(['RIGHT(C3,LEN("LL"))="LL"'], $conditions);
$newWizard = Wizard::fromConditional($conditional, $this->range);
$newWizard->getConditional();
self::assertEquals($newWizard, $textWizard, 'fromConditional() Failure');
}
public function testInvalidFromConditional(): void
{
$ruleType = 'Unknown';
$this->expectException(Exception::class);
$this->expectExceptionMessage('Conditional is not a Text Value CF Rule conditional');
$conditional = new Conditional();
$conditional->setConditionType($ruleType);
Wizard\TextValue::fromConditional($conditional);
}
}