From 41c8a4f1b419c3812db5f9da86bb5c702d7be892 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 30 Jan 2021 18:45:31 +0100 Subject: [PATCH] Additional unit tests for previously untested financial functions (#1815) * Additional unit tests for previously untested financial functions, and some additions to follow untested paths * Start splitting Financial function tests out from the large FinancialTests class into individual test classes for each function --- src/PhpSpreadsheet/Calculation/Financial.php | 6 +- .../Calculation/FinancialTest.php | 128 ------------------ .../Functions/Financial/AmorDegRcTest.php | 31 +++++ .../Functions/Financial/AmorLincTest.php | 31 +++++ .../Functions/Financial/CoupDayBsTest.php | 31 +++++ .../Functions/Financial/CoupDaysNcTest.php | 31 +++++ .../Functions/Financial/CoupDaysTest.php | 31 +++++ .../Functions/Financial/CoupNcdTest.php | 31 +++++ .../Functions/Financial/CoupNumTest.php | 31 +++++ .../Functions/Financial/CoupPcdTest.php | 31 +++++ .../Functions/Financial/PriceMatTest.php | 31 +++++ .../Functions/Financial/ReceivedTest.php | 31 +++++ .../Functions/Financial/TBillEqTest.php | 31 +++++ .../Functions/Financial/TBillPriceTest.php | 31 +++++ .../Functions/Financial/TBillYieldTest.php | 31 +++++ .../Functions/Financial/YieldDiscTest.php | 31 +++++ .../Functions/Financial/YieldMatTest.php | 31 +++++ .../data/Calculation/Financial/AMORDEGRC.php | 28 ++-- tests/data/Calculation/Financial/AMORLINC.php | 28 ++-- tests/data/Calculation/Financial/PRICEMAT.php | 16 +++ tests/data/Calculation/Financial/RECEIVED.php | 16 +++ tests/data/Calculation/Financial/TBILLEQ.php | 20 +++ .../data/Calculation/Financial/TBILLPRICE.php | 20 +++ .../data/Calculation/Financial/TBILLYIELD.php | 12 ++ .../data/Calculation/Financial/YIELDDISC.php | 12 ++ tests/data/Calculation/Financial/YIELDMAT.php | 12 ++ 26 files changed, 604 insertions(+), 159 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/AmorDegRcTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/AmorLincTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupDayBsTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupDaysNcTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupDaysTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupNcdTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupNumTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/CoupPcdTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/PriceMatTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/ReceivedTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/TBillEqTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/TBillPriceTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/TBillYieldTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/YieldDiscTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Financial/YieldMatTest.php create mode 100644 tests/data/Calculation/Financial/PRICEMAT.php create mode 100644 tests/data/Calculation/Financial/RECEIVED.php create mode 100644 tests/data/Calculation/Financial/TBILLEQ.php create mode 100644 tests/data/Calculation/Financial/TBILLPRICE.php create mode 100644 tests/data/Calculation/Financial/TBILLYIELD.php create mode 100644 tests/data/Calculation/Financial/YIELDDISC.php create mode 100644 tests/data/Calculation/Financial/YIELDMAT.php diff --git a/src/PhpSpreadsheet/Calculation/Financial.php b/src/PhpSpreadsheet/Calculation/Financial.php index 5a908aa513..728167f426 100644 --- a/src/PhpSpreadsheet/Calculation/Financial.php +++ b/src/PhpSpreadsheet/Calculation/Financial.php @@ -2037,7 +2037,7 @@ public static function TBILLEQ($settlement, $maturity, $discount) return Functions::VALUE(); } - if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) { + if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) { ++$maturity; $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360; } else { @@ -2076,7 +2076,7 @@ public static function TBILLPRICE($settlement, $maturity, $discount) return Functions::NAN(); } - if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) { + if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) { ++$maturity; $daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity) * 360; if (!is_numeric($daysBetweenSettlementAndMaturity)) { @@ -2087,7 +2087,7 @@ public static function TBILLPRICE($settlement, $maturity, $discount) $daysBetweenSettlementAndMaturity = (DateTime::getDateValue($maturity) - DateTime::getDateValue($settlement)); } - if ($daysBetweenSettlementAndMaturity > 360) { + if ($daysBetweenSettlementAndMaturity > self::daysPerYear(DateTime::YEAR($maturity), 1)) { return Functions::NAN(); } diff --git a/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php b/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php index e80ef35b66..d6135a466d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/FinancialTest.php @@ -13,134 +13,6 @@ protected function setUp(): void Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); } - /** - * @dataProvider providerAMORDEGRC - * - * @param mixed $expectedResult - */ - public function testAMORDEGRC($expectedResult, ...$args): void - { - $result = Financial::AMORDEGRC(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerAMORDEGRC() - { - return require 'tests/data/Calculation/Financial/AMORDEGRC.php'; - } - - /** - * @dataProvider providerAMORLINC - * - * @param mixed $expectedResult - */ - public function testAMORLINC($expectedResult, ...$args): void - { - $result = Financial::AMORLINC(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerAMORLINC() - { - return require 'tests/data/Calculation/Financial/AMORLINC.php'; - } - - /** - * @dataProvider providerCOUPDAYBS - * - * @param mixed $expectedResult - */ - public function testCOUPDAYBS($expectedResult, ...$args): void - { - $result = Financial::COUPDAYBS(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPDAYBS() - { - return require 'tests/data/Calculation/Financial/COUPDAYBS.php'; - } - - /** - * @dataProvider providerCOUPDAYS - * - * @param mixed $expectedResult - */ - public function testCOUPDAYS($expectedResult, ...$args): void - { - $result = Financial::COUPDAYS(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPDAYS() - { - return require 'tests/data/Calculation/Financial/COUPDAYS.php'; - } - - /** - * @dataProvider providerCOUPDAYSNC - * - * @param mixed $expectedResult - */ - public function testCOUPDAYSNC($expectedResult, ...$args): void - { - $result = Financial::COUPDAYSNC(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPDAYSNC() - { - return require 'tests/data/Calculation/Financial/COUPDAYSNC.php'; - } - - /** - * @dataProvider providerCOUPNCD - * - * @param mixed $expectedResult - */ - public function testCOUPNCD($expectedResult, ...$args): void - { - $result = Financial::COUPNCD(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPNCD() - { - return require 'tests/data/Calculation/Financial/COUPNCD.php'; - } - - /** - * @dataProvider providerCOUPNUM - * - * @param mixed $expectedResult - */ - public function testCOUPNUM($expectedResult, ...$args): void - { - $result = Financial::COUPNUM(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPNUM() - { - return require 'tests/data/Calculation/Financial/COUPNUM.php'; - } - - /** - * @dataProvider providerCOUPPCD - * - * @param mixed $expectedResult - */ - public function testCOUPPCD($expectedResult, ...$args): void - { - $result = Financial::COUPPCD(...$args); - self::assertEqualsWithDelta($expectedResult, $result, 1E-8); - } - - public function providerCOUPPCD() - { - return require 'tests/data/Calculation/Financial/COUPPCD.php'; - } - /** * @dataProvider providerCUMIPMT * diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/AmorDegRcTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/AmorDegRcTest.php new file mode 100644 index 0000000000..5d0cb8efba --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/AmorDegRcTest.php @@ -0,0 +1,31 @@ +