From f08eeaa2ed7952b25cf0ea986129ced0ac041912 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sat, 21 Apr 2018 20:34:25 +0100 Subject: [PATCH] Code works, but the tests don't yet --- src/PhpSpreadsheet/Calculation/MathTrig.php | 11 ++++++--- .../Calculation/MathTrigTest.php | 23 +++++++++++++++---- .../Calculation/MathTrig/SUBTOTALNESTED.php | 5 ++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 99a52e97..52cd6c1e 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1088,7 +1088,8 @@ class MathTrig list(, $row, $column) = explode('.', $index); if ($cellReference->getWorksheet()->cellExists($column . $row)) { //take this cell out if it contains the SUBTOTAL formula - return !preg_match('/=.*\b(SUBTOTAL)\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); + return !$cellReference->getWorksheet()->getCell($column . $row)->isFormula() && + !preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); } return true; }, @@ -1102,7 +1103,11 @@ class MathTrig * Returns a subtotal in a list or database. * * @param int the number 1 to 11 that specifies which function to - * use in calculating subtotals within a list + * use in calculating subtotals within a range + * list + * Numbers 101 to 111 shadow the functions of 1 to 11 + * but ignore any values in the range that are + * in hidden rows or columns * @param array of mixed Data Series * * @return float @@ -1117,7 +1122,7 @@ class MathTrig if ((is_numeric($subtotal)) && (!is_string($subtotal))) { if ($subtotal > 100) { $aArgs = self::filterHiddenArgs($cellReference, $aArgs); - $subtotal = $subtotal - 100; + $subtotal -= 100; } $aArgs = self::filterFormulaArgs($cellReference, $aArgs); diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index eda750af..9894a59a 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -542,7 +542,7 @@ class MathTrigTest extends TestCase return require 'data/Calculation/MathTrig/SUBTOTAL.php'; } - protected static function rowVisibility() { + protected function rowVisibility() { yield from [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 =>true]; } @@ -553,13 +553,14 @@ class MathTrigTest extends TestCase */ public function testHiddenSUBTOTAL($expectedResult, ...$args) { - $generator = \PhpOffice\PhpSpreadsheetTests\Calculation\MathTrigTest::rowVisibility(); + $visibilityGenerator = $this->rowVisibility(); + $rowDimension = $this->getMockBuilder(RowDimension::class) ->setMethods(['getVisible']) ->disableOriginalConstructor() ->getMock(); $rowDimension->method('getVisible') - ->will($this->returnCallback(function() use ($generator) { $result = $generator->current(); $generator->next(); return $result; })); + ->will($this->returnCallback(function() use ($visibilityGenerator) { $result = $visibilityGenerator->current(); $visibilityGenerator->next(); return $result; })); $columnDimension = $this->getMockBuilder(ColumnDimension::class) ->setMethods(['getVisible']) ->disableOriginalConstructor() @@ -601,6 +602,13 @@ class MathTrigTest extends TestCase return require 'data/Calculation/MathTrig/SUBTOTALHIDDEN.php'; } + public static $cellValues; + + public function cellValues() { + echo 'CALLED cellValues()', PHP_EOL; + yield from [1,2,3,4,5,6,7,8,9,10]; + } + /** * @dataProvider providerNestedSUBTOTAL * @@ -608,12 +616,17 @@ class MathTrigTest extends TestCase */ public function testNestedSUBTOTAL($expectedResult, ...$args) { + self::$cellValues = Functions::flattenArray(array_slice($args, 1)); + $cellValueGenerator = $this->cellValues(); + $cell = $this->getMockBuilder(Cell::class) - ->setMethods(['getValue']) + ->setMethods(['getValue', 'isFormula']) ->disableOriginalConstructor() ->getMock(); $cell->method('getValue') - ->willReturn(null); + ->will($this->returnCallback(function() use ($cellValueGenerator) { $result = $cellValueGenerator->current(); $cellValueGenerator->next(); var_dump($result); return $result; })); + $cell->method('isFormula') + ->willReturn(true); $worksheet = $this->getMockBuilder(Worksheet::class) ->setMethods(['cellExists', 'getCell']) ->disableOriginalConstructor() diff --git a/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php b/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php index 20a8e170..564f0f2b 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php +++ b/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php @@ -2,9 +2,10 @@ $baseTestData = [ 1 => ['A' => 1], - 2 => ['A' => 1], + 2 => ['A' => '=2*1'], 3 => ['A' => '=SUBTOTAL(1, A1:A2)'], - 4 => ['A' => '=ROMAN(SUBTOTAL(1, A1:A2))'] + 4 => ['A' => '=ROMAN(SUBTOTAL(1, A1:A2))'], + 5 => ['A' => 'This is text containing "=" and "SUBTOTAL("'], ]; return [