Improve Coverage

Take on some low-hanging fruit, mostly in Conditional Formatting Wizard.
This commit is contained in:
oleibman
2026-04-06 09:28:17 -07:00
parent 4a476071b3
commit 5b174d37f2
12 changed files with 163 additions and 26 deletions
@@ -54,7 +54,8 @@ class CellValue extends WizardAbstract implements WizardInterface
protected function operator(string $operator): void
{
if ((!isset(self::SINGLE_OPERATORS[$operator])) && (!isset(self::RANGE_OPERATORS[$operator]))) {
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard');
// should not happen - compareKeys confirms
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard 1'); // @codeCoverageIgnore
}
$this->operator = $operator;
@@ -158,10 +159,6 @@ class CellValue extends WizardAbstract implements WizardInterface
*/
public function __call(string $methodName, array $arguments): self
{
if (!isset(self::MAGIC_OPERATIONS[$methodName]) && $methodName !== 'and') {
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard');
}
if ($methodName === 'and') {
if (!isset(self::RANGE_OPERATORS[$this->operator])) {
throw new Exception('AND Value is only appropriate for range operators');
@@ -172,6 +169,10 @@ class CellValue extends WizardAbstract implements WizardInterface
return $this;
}
if (!isset(self::MAGIC_OPERATIONS[$methodName])) {
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard');
}
$this->operator(self::MAGIC_OPERATIONS[$methodName]);
//$this->operand(0, ...$arguments);
if (count($arguments) < 2) {
@@ -184,4 +185,16 @@ class CellValue extends WizardAbstract implements WizardInterface
return $this;
}
/** @internal */
public static function compareKeys(): bool
{
$retVal = true;
$array = array_merge(array_keys(self::SINGLE_OPERATORS), array_keys(self::RANGE_OPERATORS));
foreach ($array as $value) {
$retVal = $retVal && in_array($value, self::MAGIC_OPERATIONS, true);
}
return $retVal;
}
}
@@ -64,7 +64,7 @@ class Duplicates extends WizardAbstract implements WizardInterface
public function __call(string $methodName, array $arguments): self
{
if (!array_key_exists($methodName, self::OPERATORS)) {
throw new Exception('Invalid Operation for Errors CF Rule Wizard');
throw new Exception('Invalid Operation for Duplicates CF Rule Wizard');
}
$this->inverse(self::OPERATORS[$methodName]);
@@ -54,7 +54,8 @@ class TextValue extends WizardAbstract implements WizardInterface
protected function operator(string $operator): void
{
if (!isset(self::OPERATORS[$operator])) {
throw new Exception('Invalid Operator for Text Value CF Rule Wizard');
// should not happen - compareKeys confirms
throw new Exception('Invalid Operator for Text Value CF Rule Wizard'); // @codeCoverageIgnore
}
$this->operator = $operator;
@@ -161,4 +162,16 @@ class TextValue extends WizardAbstract implements WizardInterface
return $this;
}
/** @internal */
public static function compareKeys(): bool
{
$retVal = true;
$array = array_keys(self::OPERATORS);
foreach ($array as $value) {
$retVal = $retVal && in_array($value, self::MAGIC_OPERATIONS, true);
}
return $retVal;
}
}
@@ -6,10 +6,7 @@ use PhpOffice\PhpSpreadsheet\Style\Border;
class CellBorder
{
/**
* @var array<string, int>
*/
protected static array $styleMap = [
protected const BORDER_MAP = [
Border::BORDER_NONE => 0x00,
Border::BORDER_THIN => 0x01,
Border::BORDER_MEDIUM => 0x02,
@@ -31,10 +28,6 @@ class CellBorder
{
$borderStyle = $border->getBorderStyle();
if (array_key_exists($borderStyle, self::$styleMap)) {
return self::$styleMap[$borderStyle];
}
return self::$styleMap[Border::BORDER_NONE];
return self::BORDER_MAP[$borderStyle] ?? self::BORDER_MAP[Border::BORDER_NONE];
}
}
@@ -6,10 +6,7 @@ use PhpOffice\PhpSpreadsheet\Style\Fill;
class CellFill
{
/**
* @var array<string, int>
*/
protected static array $fillStyleMap = [
protected const FILL_MAP = [
Fill::FILL_NONE => 0x00,
Fill::FILL_SOLID => 0x01,
Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
@@ -35,12 +32,8 @@ class CellFill
public static function style(Fill $fill): int
{
$fillStyle = $fill->getFillType();
$fillStyle = (string) $fill->getFillType();
if (is_string($fillStyle) && array_key_exists($fillStyle, self::$fillStyleMap)) {
return self::$fillStyleMap[$fillStyle];
}
return self::$fillStyleMap[Fill::FILL_NONE];
return self::FILL_MAP[$fillStyle] ?? self::FILL_MAP[Fill::FILL_NONE];
}
}
@@ -106,4 +106,28 @@ class BlankWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\Blanks::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Blanks CF Rule Wizard');
$ruleType = Wizard::BLANKS;
/** @var Wizard\Blanks $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
public function testStopIfTrue(): void
{
$ruleType = Wizard::BLANKS;
/** @var Wizard\Blanks $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$wizard->setStopIfTrue(false);
self::assertFalse($wizard->getStopIfTrue());
$wizard->setStopIfTrue(true);
self::assertTrue($wizard->getStopIfTrue());
}
}
@@ -234,4 +234,35 @@ class CellValueWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\CellValue::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operator for Cell Value CF Rule Wizard');
$ruleType = Wizard::CELL_VALUE;
/** @var Wizard\CellValue $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
public function testBadAnd(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('AND Value is only appropriate for range operators');
$operands = [1, 5];
$ruleType = Wizard::CELL_VALUE;
/** @var Wizard\CellValue $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$wizard
->equals($operands[0], Wizard::VALUE_TYPE_LITERAL)
->and($operands[1], Wizard::VALUE_TYPE_LITERAL);
}
public function testCompareKeys(): void
{
self::assertTrue(Wizard\CellValue::compareKeys());
}
}
@@ -65,4 +65,17 @@ class DateValueWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\DateValue::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Date Value CF Rule Wizard');
$ruleType = Wizard::DATES_OCCURRING;
/** @var Wizard\DateValue $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
}
@@ -98,4 +98,17 @@ class DuplicatesWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\Duplicates::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Duplicates CF Rule Wizard');
$ruleType = Wizard::DUPLICATES;
/** @var Wizard\Duplicates $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
}
@@ -106,4 +106,17 @@ class ErrorWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\Errors::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Errors CF Rule Wizard');
$ruleType = Wizard::ERRORS;
/** @var Wizard\Errors $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
}
@@ -78,4 +78,17 @@ class ExpressionWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\Expression::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Expression CF Rule Wizard');
$ruleType = Wizard::EXPRESSION;
/** @var Wizard\Expression $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
}
@@ -138,4 +138,22 @@ class TextValueWizardTest extends TestCase
$conditional->setConditionType($ruleType);
Wizard\TextValue::fromConditional($conditional);
}
protected string $unknown = 'UNKNOWN';
public function testInvalidOperator(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid Operation for Text Value CF Rule Wizard');
$ruleType = Wizard::TEXT_VALUE;
/** @var Wizard\TextValue $wizard */
$wizard = $this->wizardFactory->newRule($ruleType);
$ruleType = $this->unknown;
$wizard->$ruleType();
}
public function testCompareKeys(): void
{
self::assertTrue(Wizard\TextValue::compareKeys());
}
}