From 5d309e982c32a8cfde37846cf55e8b8dab687d7f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 31 Jan 2021 15:09:56 +0100 Subject: [PATCH] Extract remaining Excel function unit tests into separate test classes for each function (#1817) * Extract remaining Financial function unit tests into separate test classes for each function This makes it easier to manage unit tests if they are individual files rather than all in a single file It also provides a stepping stone toward making it easier to test Excel functions when Excel errors no longer return a string, but an actual Excel exception that can be handled more cleanly --- src/PhpSpreadsheet/Shared/OLE.php | 3 +- src/PhpSpreadsheet/Worksheet/AutoFilter.php | 4 +- .../Calculation/FinancialTest.php | 500 ------------------ .../Functions/Financial/CumIpmtTest.php | 31 ++ .../Functions/Financial/CumPrincTest.php | 31 ++ .../Functions/Financial/DbTest.php | 31 ++ .../Functions/Financial/DdbTest.php | 31 ++ .../Functions/Financial/DiscTest.php | 31 ++ .../Functions/Financial/DollarDeTest.php | 31 ++ .../Functions/Financial/DollarFrTest.php | 31 ++ .../Functions/Financial/EffectTest.php | 31 ++ .../Functions/Financial/FvScheduleTest.php | 31 ++ .../Functions/Financial/FvTest.php | 31 ++ .../Functions/Financial/IPmtTest.php | 31 ++ .../Functions/Financial/IntRateTest.php | 31 ++ .../Functions/Financial/IrrTest.php | 31 ++ .../Functions/Financial/IsPmtTest.php | 31 ++ .../Functions/Financial/MirrTest.php | 31 ++ .../Functions/Financial/NPerTest.php | 31 ++ .../Functions/Financial/NominalTest.php | 31 ++ .../Functions/Financial/NpvTest.php | 31 ++ .../Functions/Financial/PDurationTest.php | 31 ++ .../Functions/Financial/PriceDiscTest.php | 31 ++ .../Functions/Financial/PriceTest.php | 50 ++ .../Functions/Financial/PvTest.php | 31 ++ .../Functions/Financial/RateTest.php | 31 ++ .../Functions/Financial/RriTest.php | 31 ++ .../Functions/Financial/SlnTest.php | 31 ++ .../Functions/Financial/SydTest.php | 31 ++ .../Functions/Financial/XNpvTest.php | 40 ++ .../Functions/Financial/XirrTest.php | 40 ++ 31 files changed, 910 insertions(+), 502 deletions(-) delete mode 100644 tests/PhpSpreadsheetTests/Calculation/FinancialTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CumIpmtTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CumPrincTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DbTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DdbTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DiscTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarDeTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarFrTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/EffectTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvScheduleTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IPmtTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IntRateTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IrrTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IsPmtTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/MirrTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NPerTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NominalTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NpvTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/PDurationTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/PriceDiscTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/PriceTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/PvTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/RateTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/RriTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/SlnTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/SydTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/XNpvTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/XirrTest.php diff --git a/src/PhpSpreadsheet/Shared/OLE.php b/src/PhpSpreadsheet/Shared/OLE.php index d380995c..1c745bdb 100644 --- a/src/PhpSpreadsheet/Shared/OLE.php +++ b/src/PhpSpreadsheet/Shared/OLE.php @@ -227,7 +227,8 @@ class OLE // in OLE_ChainedBlockStream::stream_open(). // Object is removed from self::$instances in OLE_Stream::close(). $GLOBALS['_OLE_INSTANCES'][] = $this; - $instanceId = end(array_keys($GLOBALS['_OLE_INSTANCES'])); + $keys = array_keys($GLOBALS['_OLE_INSTANCES']); + $instanceId = end($keys); $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId; if ($blockIdOrPps instanceof OLE\PPS) { diff --git a/src/PhpSpreadsheet/Worksheet/AutoFilter.php b/src/PhpSpreadsheet/Worksheet/AutoFilter.php index c2ded195..4c33eb37 100644 --- a/src/PhpSpreadsheet/Worksheet/AutoFilter.php +++ b/src/PhpSpreadsheet/Worksheet/AutoFilter.php @@ -595,7 +595,9 @@ class AutoFilter sort($dataValues); } - return array_pop(array_slice($dataValues, 0, $ruleValue)); + $slice = array_slice($dataValues, 0, $ruleValue); + + return array_pop($slice); } /** diff --git a/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php b/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php deleted file mode 100644 index d6135a46..00000000 --- a/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php +++ /dev/null @@ -1,500 +0,0 @@ - 0.999999 && $frac < 1.000001) { - $result = $expectedResult; - } - } - } - self::assertEquals($expectedResult, $result, $message); - } - - public function providerXIRR() - { - return require 'tests/data/Calculation/Financial/XIRR.php'; - } - - /** - * @dataProvider providerXNPV - * - * @param mixed $expectedResult - * @param mixed $message - */ - public function testXNPV($expectedResult, $message, ...$args): void - { - $result = Financial::XNPV(...$args); - if (is_numeric($result) && is_numeric($expectedResult)) { - if ($expectedResult != 0) { - $frac = $result / $expectedResult; - if ($frac > 0.999999 && $frac < 1.000001) { - $result = $expectedResult; - } - } - } - self::assertEquals($expectedResult, $result, $message); - } - - public function providerXNPV() - { - return require 'tests/data/Calculation/Financial/XNPV.php'; - } - - /** - * @dataProvider providerPDURATION - * - * @param mixed $expectedResult - */ - public function testPDURATION($expectedResult, array $args): void - { - $result = Financial::PDURATION(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerPDURATION() - { - return require 'tests/data/Calculation/Financial/PDURATION.php'; - } - - /** - * @dataProvider providerRRI - * - * @param mixed $expectedResult - */ - public function testRRI($expectedResult, array $args): void - { - $result = Financial::RRI(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerRRI() - { - return require 'tests/data/Calculation/Financial/RRI.php'; - } - - /** - * @dataProvider providerSLN - * - * @param mixed $expectedResult - */ - public function testSLN($expectedResult, array $args): void - { - $result = Financial::SLN(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerSLN() - { - return require 'tests/data/Calculation/Financial/SLN.php'; - } - - /** - * @dataProvider providerSYD - * - * @param mixed $expectedResult - */ - public function testSYD($expectedResult, array $args): void - { - $result = Financial::SYD(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerSYD() - { - return require 'tests/data/Calculation/Financial/SYD.php'; - } -} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CumIpmtTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CumIpmtTest.php new file mode 100644 index 00000000..ff82bcbc --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CumIpmtTest.php @@ -0,0 +1,31 @@ + 0.999999 && $frac < 1.000001) { + $result = $expectedResult; + } + } + } + self::assertEquals($expectedResult, $result, $message); + } + + public function providerXNPV() + { + return require 'tests/data/Calculation/Financial/XNPV.php'; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/XirrTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/XirrTest.php new file mode 100644 index 00000000..042ef298 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/XirrTest.php @@ -0,0 +1,40 @@ + 0.999999 && $frac < 1.000001) { + $result = $expectedResult; + } + } + } + self::assertEquals($expectedResult, $result, $message); + } + + public function providerXIRR() + { + return require 'tests/data/Calculation/Financial/XIRR.php'; + } +}