From 3697352e286372a986c65b6f5e3bf0e43faf2094 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Wed, 23 Nov 2022 07:49:28 -0800 Subject: [PATCH] Fix Unintential Deprecated Calls in Tests - LOGICAL (#3178) * Fix Unintential Deprecated Calls in Tests - LOGICAL I think it's best to install these before PR #3166. There are no changes to source code, only to doc-blocks and to test members which continue to inadvertently use calls to deprecated functions. * Change Unit Tests to Run in Spreadsheet Context They had been run as direct calls, which is not how most users would use them. Making this change exposed some minor coding errors - SWITCH needs to flatten its arguments, and IFERROR and IFNA were not handling a null testValue in the same manner as Excel. --- src/PhpSpreadsheet/Calculation/Logical.php | 55 ++++---- .../Calculation/Logical/Conditional.php | 4 +- .../Functions/Logical/AllSetupTeardown.php | 120 ++++++++++++++++++ .../Calculation/Functions/Logical/AndTest.php | 14 +- .../Logical/DeprecatedFunctionsTest.php | 30 +++++ .../Functions/Logical/FalseTest.php | 14 +- .../Functions/Logical/IfErrorTest.php | 17 +-- .../Functions/Logical/IfNaTest.php | 17 +-- .../Calculation/Functions/Logical/IfTest.php | 16 +-- .../Calculation/Functions/Logical/IfsTest.php | 46 +++++-- .../Calculation/Functions/Logical/NotTest.php | 11 +- .../Calculation/Functions/Logical/OrTest.php | 14 +- .../Functions/Logical/SwitchTest.php | 54 ++++++-- .../Functions/Logical/TrueTest.php | 14 +- .../Calculation/Functions/Logical/XorTest.php | 14 +- tests/data/Calculation/Logical/AND.php | 5 +- tests/data/Calculation/Logical/IF.php | 18 +-- tests/data/Calculation/Logical/IFERROR.php | 4 +- tests/data/Calculation/Logical/IFNA.php | 2 + tests/data/Calculation/Logical/IFS.php | 11 +- tests/data/Calculation/Logical/NOT.php | 4 +- tests/data/Calculation/Logical/OR.php | 5 +- tests/data/Calculation/Logical/SWITCH.php | 35 +---- tests/data/Calculation/Logical/XOR.php | 5 +- 24 files changed, 301 insertions(+), 228 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AllSetupTeardown.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Logical/DeprecatedFunctionsTest.php diff --git a/src/PhpSpreadsheet/Calculation/Logical.php b/src/PhpSpreadsheet/Calculation/Logical.php index d5d993ae3..92e779e06 100644 --- a/src/PhpSpreadsheet/Calculation/Logical.php +++ b/src/PhpSpreadsheet/Calculation/Logical.php @@ -17,10 +17,9 @@ class Logical * Excel Function: * =TRUE() * - * @Deprecated 1.17.0 - * - * @see Logical\Boolean::TRUE() + * @deprecated 1.17.0 * Use the TRUE() method in the Logical\Boolean class instead + * @see Logical\Boolean::TRUE() * * @return bool True */ @@ -37,10 +36,9 @@ class Logical * Excel Function: * =FALSE() * - * @Deprecated 1.17.0 - * - * @see Logical\Boolean::FALSE() + * @deprecated 1.17.0 * Use the FALSE() method in the Logical\Boolean class instead + * @see Logical\Boolean::FALSE() * * @return bool False */ @@ -65,10 +63,9 @@ class Logical * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value * - * @Deprecated 1.17.0 - * - * @see Logical\Operations::logicalAnd() + * @deprecated 1.17.0 * Use the logicalAnd() method in the Logical\Operations class instead + * @see Logical\Operations::logicalAnd() * * @param mixed ...$args Data values * @@ -95,10 +92,9 @@ class Logical * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value * - * @Deprecated 1.17.0 - * - * @see Logical\Operations::logicalOr() + * @deprecated 1.17.0 * Use the logicalOr() method in the Logical\Operations class instead + * @see Logical\Operations::logicalOr() * * @param mixed $args Data values * @@ -127,10 +123,9 @@ class Logical * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value * - * @Deprecated 1.17.0 - * - * @see Logical\Operations::logicalXor() + * @deprecated 1.17.0 * Use the logicalXor() method in the Logical\Operations class instead + * @see Logical\Operations::logicalXor() * * @param mixed $args Data values * @@ -156,10 +151,9 @@ class Logical * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value * - * @Deprecated 1.17.0 - * - * @see Logical\Operations::NOT() + * @deprecated 1.17.0 * Use the NOT() method in the Logical\Operations class instead + * @see Logical\Operations::NOT() * * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE * @@ -195,10 +189,9 @@ class Logical * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned. * ReturnIfFalse can be another formula. * - * @Deprecated 1.17.0 - * - * @see Logical\Conditional::statementIf() + * @deprecated 1.17.0 * Use the statementIf() method in the Logical\Conditional class instead + * @see Logical\Conditional::statementIf() * * @param mixed $condition Condition to evaluate * @param mixed $returnIfTrue Value to return when condition is true @@ -231,10 +224,9 @@ class Logical * Optional. It is the default to return if expression does not match any of the values * (value1, value2, ... value_n). * - * @Deprecated 1.17.0 - * - * @see Logical\Conditional::statementSwitch() + * @deprecated 1.17.0 * Use the statementSwitch() method in the Logical\Conditional class instead + * @see Logical\Conditional::statementSwitch() * * @param mixed $arguments Statement arguments * @@ -251,10 +243,9 @@ class Logical * Excel Function: * =IFERROR(testValue,errorpart) * - * @Deprecated 1.17.0 - * - * @see Logical\Conditional::IFERROR() + * @deprecated 1.17.0 * Use the IFERROR() method in the Logical\Conditional class instead + * @see Logical\Conditional::IFERROR() * * @param mixed $testValue Value to check, is also the value returned when no error * @param mixed $errorpart Value to return when testValue is an error condition @@ -272,10 +263,9 @@ class Logical * Excel Function: * =IFNA(testValue,napart) * - * @Deprecated 1.17.0 - * - * @see Logical\Conditional::IFNA() + * @deprecated 1.17.0 * Use the IFNA() method in the Logical\Conditional class instead + * @see Logical\Conditional::IFNA() * * @param mixed $testValue Value to check, is also the value returned when not an NA * @param mixed $napart Value to return when testValue is an NA condition @@ -298,10 +288,9 @@ class Logical * returnIfTrue1 ... returnIfTrue_n * Value returned if corresponding testValue (nth) was true * - * @Deprecated 1.17.0 - * - * @see Logical\Conditional::IFS() + * @deprecated 1.17.0 * Use the IFS() method in the Logical\Conditional class instead + * @see Logical\Conditional::IFS() * * @param mixed ...$arguments Statement arguments * diff --git a/src/PhpSpreadsheet/Calculation/Logical/Conditional.php b/src/PhpSpreadsheet/Calculation/Logical/Conditional.php index 6a7757ce2..55d5f3274 100644 --- a/src/PhpSpreadsheet/Calculation/Logical/Conditional.php +++ b/src/PhpSpreadsheet/Calculation/Logical/Conditional.php @@ -100,7 +100,7 @@ class Conditional $switchSatisfied = false; if ($switchCount > 0) { for ($index = 0; $index < $switchCount; ++$index) { - if ($targetValue == $arguments[$index * 2 + 1]) { + if ($targetValue == Functions::flattenSingleValue($arguments[$index * 2 + 1])) { $result = $arguments[$index * 2 + 2]; $switchSatisfied = true; @@ -139,6 +139,7 @@ class Conditional } $errorpart = $errorpart ?? ''; + $testValue = $testValue ?? 0; // this is how Excel handles empty cell return self::statementIf(ErrorValue::isError($testValue), $errorpart, $testValue); } @@ -165,6 +166,7 @@ class Conditional } $napart = $napart ?? ''; + $testValue = $testValue ?? 0; // this is how Excel handles empty cell return self::statementIf(ErrorValue::isNa($testValue), $napart, $testValue); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AllSetupTeardown.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AllSetupTeardown.php new file mode 100644 index 000000000..34432c6a1 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AllSetupTeardown.php @@ -0,0 +1,120 @@ +compatibilityMode = Functions::getCompatibilityMode(); + } + + protected function tearDown(): void + { + Functions::setCompatibilityMode($this->compatibilityMode); + $this->sheet = null; + if ($this->spreadsheet !== null) { + $this->spreadsheet->disconnectWorksheets(); + $this->spreadsheet = null; + } + } + + protected static function setOpenOffice(): void + { + Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); + } + + protected static function setGnumeric(): void + { + Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC); + } + + /** + * @param mixed $expectedResult + */ + protected function mightHaveException($expectedResult): void + { + if ($expectedResult === 'exception') { + $this->expectException(CalcException::class); + } + } + + /** + * @param mixed $value + */ + protected function setCell(string $cell, $value): void + { + if ($value !== null) { + if (is_string($value) && is_numeric($value)) { + $this->getSheet()->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING); + } else { + $this->getSheet()->getCell($cell)->setValue($value); + } + } + } + + protected function getSpreadsheet(): Spreadsheet + { + if ($this->spreadsheet !== null) { + return $this->spreadsheet; + } + $this->spreadsheet = new Spreadsheet(); + + return $this->spreadsheet; + } + + protected function getSheet(): Worksheet + { + if ($this->sheet !== null) { + return $this->sheet; + } + $this->sheet = $this->getSpreadsheet()->getActiveSheet(); + + return $this->sheet; + } + + /** + * @param mixed $expectedResult + * @param array $args + */ + protected function runTestCase(string $functionName, $expectedResult, ...$args): void + { + $this->mightHaveException($expectedResult); + $sheet = $this->getSheet(); + $formula = "=$functionName("; + $comma = ''; + $row = 0; + foreach ($args as $arg) { + ++$row; + $cellId = "A$row"; + $formula .= "$comma$cellId"; + $comma = ','; + $this->setCell($cellId, $arg); + } + $formula .= ')'; + $this->setCell('B1', $formula); + self::assertSame($expectedResult, $sheet->getCell('B1')->getCalculatedValue()); + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AndTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AndTest.php index a1d546b02..e5f14d776 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AndTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/AndTest.php @@ -2,17 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; - -class AndTest extends TestCase +class AndTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerAND * @@ -20,8 +11,7 @@ class AndTest extends TestCase */ public function testAND($expectedResult, ...$args): void { - $result = Logical::logicalAnd(...$args); - self::assertEquals($expectedResult, $result); + $this->runTestCase('AND', $expectedResult, ...$args); } public function providerAND(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/DeprecatedFunctionsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/DeprecatedFunctionsTest.php new file mode 100644 index 000000000..11b1beaf9 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/DeprecatedFunctionsTest.php @@ -0,0 +1,30 @@ +runTestCase('FALSE', false); } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfErrorTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfErrorTest.php index 03ad8b3ae..4ce1eb91b 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfErrorTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfErrorTest.php @@ -3,28 +3,17 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; -class IfErrorTest extends TestCase +class IfErrorTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerIFERROR * * @param mixed $expectedResult - * @param mixed $value - * @param mixed $return */ - public function testIFERROR($expectedResult, $value, $return): void + public function testIFERROR($expectedResult, ...$args): void { - $result = Logical::IFERROR($value, $return); - self::assertEquals($expectedResult, $result); + $this->runTestCase('IFERROR', $expectedResult, ...$args); } public function providerIFERROR(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfNaTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfNaTest.php index 1c87de77c..c9d9c8449 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfNaTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfNaTest.php @@ -3,28 +3,17 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; -class IfNaTest extends TestCase +class IfNaTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerIFNA * * @param mixed $expectedResult - * @param mixed $value - * @param mixed $return */ - public function testIFNA($expectedResult, $value, $return): void + public function testIFNA($expectedResult, ...$args): void { - $result = Logical::IFNA($value, $return); - self::assertEquals($expectedResult, $result); + $this->runTestCase('IFNA', $expectedResult, ...$args); } public function providerIFNA(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfTest.php index 589971610..b7d3104f7 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfTest.php @@ -2,10 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; - -class IfTest extends TestCase +class IfTest extends AllSetupTeardown { /** * @dataProvider providerIF @@ -14,16 +11,7 @@ class IfTest extends TestCase */ public function testIF($expectedResult, ...$args): void { - if (count($args) === 0) { - $result = Logical::statementIf(); - } elseif (count($args) === 1) { - $result = Logical::statementIf($args[0]); - } elseif (count($args) === 2) { - $result = Logical::statementIf($args[0], $args[1]); - } else { - $result = Logical::statementIf($args[0], $args[1], $args[2]); - } - self::assertEquals($expectedResult, $result); + $this->runTestCase('IF', $expectedResult, ...$args); } public function providerIF(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfsTest.php index 6660b9d3d..d6e702632 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/IfsTest.php @@ -2,17 +2,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; -class IfsTest extends TestCase +class IfsTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerIFS * @@ -21,12 +14,43 @@ class IfsTest extends TestCase */ public function testIFS($expectedResult, ...$args): void { - $result = Logical::IFS(...$args); - self::assertEquals($expectedResult, $result); + $this->runTestCase('IFS', $expectedResult, ...$args); } public function providerIFS(): array { return require 'tests/data/Calculation/Logical/IFS.php'; } + + /** + * @dataProvider providerIfsArray + */ + public function testIfsArray(array $expectedResult, string $bool1, string $argument1, string $bool2, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=IFS($bool1, {" . "$argument1}, $bool2, {" . "$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerIfsArray(): array + { + return [ + 'array return first item' => [ + [[1, 2, 3]], + 'true', + '1, 2, 3', + 'true', + '4, 5, 6', + ], + 'array return second item' => [ + [[4, 5, 6]], + 'false', + '1, 2, 3', + 'true', + '4, 5, 6', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/NotTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/NotTest.php index 6d8b2ca9e..43fa69d9d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/NotTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/NotTest.php @@ -3,10 +3,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; -class NotTest extends TestCase +class NotTest extends AllSetupTeardown { /** * @dataProvider providerNOT @@ -15,12 +13,7 @@ class NotTest extends TestCase */ public function testNOT($expectedResult, ...$args): void { - if (count($args) === 0) { - $result = Logical::NOT(); - } else { - $result = Logical::NOT($args[0]); - } - self::assertEquals($expectedResult, $result); + $this->runTestCase('NOT', $expectedResult, ...$args); } public function providerNOT(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/OrTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/OrTest.php index af67c5069..2338a46f8 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/OrTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/OrTest.php @@ -2,17 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; - -class OrTest extends TestCase +class OrTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerOR * @@ -20,8 +11,7 @@ class OrTest extends TestCase */ public function testOR($expectedResult, ...$args): void { - $result = Logical::logicalOr(...$args); - self::assertEquals($expectedResult, $result); + $this->runTestCase('OR', $expectedResult, ...$args); } public function providerOR(): array diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/SwitchTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/SwitchTest.php index 766b1b7c7..84c53063d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/SwitchTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/SwitchTest.php @@ -2,17 +2,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; -class SwitchTest extends TestCase +class SwitchTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSwitch * @@ -20,12 +13,51 @@ class SwitchTest extends TestCase */ public function testSWITCH($expectedResult, ...$args): void { - $result = Logical::statementSwitch(...$args); - self::assertEquals($expectedResult, $result); + $this->runTestCase('SWITCH', $expectedResult, ...$args); } public function providerSwitch(): array { return require 'tests/data/Calculation/Logical/SWITCH.php'; } + + /** + * @dataProvider providerSwitchArray + * + * @param mixed $expression + * @param mixed $value1 + * @param mixed $value2 + */ + public function testIfsArray(array $expectedResult, $expression, $value1, string $result1, $value2, string $result2, string $default): void + { + $calculation = Calculation::getInstance(); + + $formula = "=SWITCH($expression, $value1, {" . "$result1}, $value2, {" . "$result2}, {" . "$default})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerSwitchArray(): array + { + return [ + 'Array return' => [ + [[4, 5, 6]], + 2, + 1, + '1, 2, 3', + 2, + '4, 5, 6', + '7, 8, 9', + ], + 'Array return default' => [ + [[7, 8, 9]], + 3, + 1, + '1, 2, 3', + 2, + '4, 5, 6', + '7, 8, 9', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/TrueTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/TrueTest.php index 21e65b76c..5ce451702 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/TrueTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/TrueTest.php @@ -2,20 +2,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; - -class TrueTest extends TestCase +class TrueTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - public function testTRUE(): void { - $result = Logical::TRUE(); - self::assertTrue($result); + $this->runTestCase('TRUE', true); } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/XorTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/XorTest.php index 27cb359cb..086d59807 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/XorTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Logical/XorTest.php @@ -2,17 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Logical; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\Logical; -use PHPUnit\Framework\TestCase; - -class XorTest extends TestCase +class XorTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerXOR * @@ -20,8 +11,7 @@ class XorTest extends TestCase */ public function testXOR($expectedResult, ...$args): void { - $result = Logical::logicalXor(...$args); - self::assertEquals($expectedResult, $result); + $this->runTestCase('XOR', $expectedResult, ...$args); } public function providerXOR(): array diff --git a/tests/data/Calculation/Logical/AND.php b/tests/data/Calculation/Logical/AND.php index 2ef175901..221a4cb2d 100644 --- a/tests/data/Calculation/Logical/AND.php +++ b/tests/data/Calculation/Logical/AND.php @@ -1,9 +1,8 @@ [ + 'exception', ], // NULL [ diff --git a/tests/data/Calculation/Logical/IF.php b/tests/data/Calculation/Logical/IF.php index e665c05d9..3bf7d90f2 100644 --- a/tests/data/Calculation/Logical/IF.php +++ b/tests/data/Calculation/Logical/IF.php @@ -1,27 +1,29 @@ [ + 'exception', ], - [ - 0, + '1 argument true' => [ + 'exception', true, ], - [ - false, + '1 argument false' => [ + 'exception', false, ], - [ + 'value_if_false omitted condtion is true' => [ 'ABC', true, 'ABC', ], - [ + 'value_if_false omitted condition is false' => [ false, false, 'ABC', ], + 'value_if_true omitted condition is true' => [0, true, null, 'error'], + 'value_if_true omitted condition is false' => ['error', false, null, 'error'], [ 'ABC', true, diff --git a/tests/data/Calculation/Logical/IFERROR.php b/tests/data/Calculation/Logical/IFERROR.php index 3f890eaa5..a379a5fbf 100644 --- a/tests/data/Calculation/Logical/IFERROR.php +++ b/tests/data/Calculation/Logical/IFERROR.php @@ -1,8 +1,8 @@ [ + 0, null, 'Error', ], diff --git a/tests/data/Calculation/Logical/IFNA.php b/tests/data/Calculation/Logical/IFNA.php index 68e73da28..d7fb7193f 100644 --- a/tests/data/Calculation/Logical/IFNA.php +++ b/tests/data/Calculation/Logical/IFNA.php @@ -9,4 +9,6 @@ return [ 'not found', '#N/A', 'not found', ], + 'non-NA error' => ['#VALUE!', '#VALUE!', 'not found'], + 'empty cell treated as 0' => [0, null, 'Error'], ]; diff --git a/tests/data/Calculation/Logical/IFS.php b/tests/data/Calculation/Logical/IFS.php index f1b8649cc..d4dd607e4 100644 --- a/tests/data/Calculation/Logical/IFS.php +++ b/tests/data/Calculation/Logical/IFS.php @@ -1,9 +1,7 @@ ['exception'], [ 1, true, @@ -47,11 +45,4 @@ return [ true, 'ABC', ], - 'array return' => [ - [[4, 5, 6]], - false, - [[1, 2, 3]], - true, - [[4, 5, 6]], - ], ]; diff --git a/tests/data/Calculation/Logical/NOT.php b/tests/data/Calculation/Logical/NOT.php index f5e5c95a2..9936c2681 100644 --- a/tests/data/Calculation/Logical/NOT.php +++ b/tests/data/Calculation/Logical/NOT.php @@ -1,8 +1,8 @@ [ + 'exception', ], [ true, diff --git a/tests/data/Calculation/Logical/OR.php b/tests/data/Calculation/Logical/OR.php index 11e70b310..148147fb3 100644 --- a/tests/data/Calculation/Logical/OR.php +++ b/tests/data/Calculation/Logical/OR.php @@ -1,9 +1,8 @@ [ + 'exception', ], // NULL [ diff --git a/tests/data/Calculation/Logical/SWITCH.php b/tests/data/Calculation/Logical/SWITCH.php index 17dc31d81..689d1d8fa 100644 --- a/tests/data/Calculation/Logical/SWITCH.php +++ b/tests/data/Calculation/Logical/SWITCH.php @@ -1,8 +1,7 @@ [ 'C', 'A', 'A', @@ -11,8 +10,7 @@ return [ 'D', '??', ], - // Must be Female - [ + 'match value2 2 result is female' => [ 'Female', 2, '1', @@ -20,8 +18,7 @@ return [ '2', 'Female', ], - // Must be X using default - [ + 'defined default value' => [ 'X', 'U', 'ABC', @@ -30,8 +27,7 @@ return [ 'Z', 'X', ], - // Must be N/A default value not defined - [ + 'undefined default value' => [ '#N/A', 'U', 'ABC', @@ -39,26 +35,5 @@ return [ 'DEF', 'Z', ], - 'Array return' => [ - [[4, 5, 6]], - 2, - 1, - [[1, 2, 3]], - 2, - [[4, 5, 6]], - [[7, 8, 9]], - ], - 'Array return as default' => [ - [[7, 8, 9]], - 3, - 1, - [[1, 2, 3]], - 2, - [[4, 5, 6]], - [[7, 8, 9]], - ], - // Must be value - no parameter - [ - '#VALUE!', - ], + 'no arguments' => ['exception'], ]; diff --git a/tests/data/Calculation/Logical/XOR.php b/tests/data/Calculation/Logical/XOR.php index 739c8da05..51c041b56 100644 --- a/tests/data/Calculation/Logical/XOR.php +++ b/tests/data/Calculation/Logical/XOR.php @@ -1,9 +1,8 @@ [ + 'exception', ], [ false,