From 1ece540254fa4894d6ee16b9c059d7316657a7e6 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 19 Apr 2018 22:44:23 +0100 Subject: [PATCH] Basic unit test for SUBTOTAL() function --- src/PhpSpreadsheet/Calculation/MathTrig.php | 11 +++-- .../Calculation/MathTrigTest.php | 20 ++++++++ tests/data/Calculation/MathTrig/SUBTOTAL.php | 48 +++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/data/Calculation/MathTrig/SUBTOTAL.php diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 86598f87..889d72a9 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1087,10 +1087,11 @@ class MathTrig $args, function ($index) use ($cellReference) { list(, $row, $column) = explode('.', $index); - - //take this cell out if it contains the SUBTOTAL formula - return strpos(strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()), '=SUBTOTAL(') === false; - + if ($cellReference->getWorksheet()->cellExists($column . $row)) { + //take this cell out if it contains the SUBTOTAL formula + return strpos(strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()), '=SUBTOTAL(') === false; + } + return true; }, ARRAY_FILTER_USE_KEY ); @@ -1109,8 +1110,8 @@ class MathTrig */ public static function SUBTOTAL(...$args) { + $cellReference = array_pop($args); $aArgs = Functions::flattenArrayIndexed($args); - $cellReference = array_pop($aArgs); $subtotal = array_shift($aArgs); // Calculate diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index c863f6ce..e6992360 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -499,4 +499,24 @@ class MathTrigTest extends TestCase { return require 'data/Calculation/MathTrig/SUMIF.php'; } + + /** + * @dataProvider providerSUBTOTAL + * + * @param mixed $expectedResult + */ + public function testSUBTOTAL($expectedResult, ...$args) + { + $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $cellReference = $spreadsheet->getActiveSheet()->getCell('A1'); + + array_push($args, $cellReference); + $result = MathTrig::SUBTOTAL(...$args); + self::assertEquals($expectedResult, $result, null, 1E-12); + } + + public function providerSUBTOTAL() + { + return require 'data/Calculation/MathTrig/SUBTOTAL.php'; + } } diff --git a/tests/data/Calculation/MathTrig/SUBTOTAL.php b/tests/data/Calculation/MathTrig/SUBTOTAL.php new file mode 100644 index 00000000..b11819fd --- /dev/null +++ b/tests/data/Calculation/MathTrig/SUBTOTAL.php @@ -0,0 +1,48 @@ + ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 12, + 2, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 12, + 3, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 89, + 4, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 0, + 5, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 0, + 6, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 27.5196899207337, + 7, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 26.3480971271593, + 8, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 232, + 9, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 757.3333333333330, + 10, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], + [ + 694.2222222222220, + 11, [1 => ['A' => 0], 2 => ['A' => 1], 3 => ['A' => 1], 4 => ['A' => 2], 5 => ['A' => 3], 6 => ['A' => 5], 7 => ['A' => 8], 8 => ['A' => 13], 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], 12 => ['A' => 89] ] + ], +];