diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 52cd6c1e..474b2cdd 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1088,8 +1088,9 @@ class MathTrig list(, $row, $column) = explode('.', $index); if ($cellReference->getWorksheet()->cellExists($column . $row)) { //take this cell out if it contains the SUBTOTAL formula - return !$cellReference->getWorksheet()->getCell($column . $row)->isFormula() && - !preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); + $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula(); + $cellFormula = !preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); + return !$isFormula || $cellFormula; } return true; }, diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index dac660a4..0447ffcb 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -603,9 +603,13 @@ class MathTrigTest extends TestCase } protected function cellValues($cellValues) { - return function() use ($cellValues) { - yield from $cellValues; - }; + yield from $cellValues; + } + + protected function cellIsFormula($cellValues) { + foreach($cellValues as $cellValue) { + yield $cellValue[0] === '='; + } } /** @@ -615,18 +619,17 @@ class MathTrigTest extends TestCase */ public function testNestedSUBTOTAL($expectedResult, ...$args) { - $this->markTestSkipped('Revisite when cellValueGenarator() works for mocking getValue() calls'); - $cellValueGenerator = $this->cellValues(Functions::flattenArray(array_slice($args, 1))); + $cellIsFormulaGenerator = $this->cellIsFormula(Functions::flattenArray(array_slice($args, 1))); $cell = $this->getMockBuilder(Cell::class) ->setMethods(['getValue', 'isFormula']) ->disableOriginalConstructor() ->getMock(); $cell->method('getValue') - ->will($this->returnCallback(function() use ($cellValueGenerator) { $result = $cellValueGenerator->current(); $cellValueGenerator->next(); var_dump($result); return $result; })); + ->will($this->returnCallback(function() use ($cellValueGenerator) { $result = $cellValueGenerator->current(); $cellValueGenerator->next(); return $result; })); $cell->method('isFormula') - ->willReturn(true); + ->will($this->returnCallback(function() use ($cellIsFormulaGenerator) { $result = $cellIsFormulaGenerator->current(); $cellIsFormulaGenerator->next(); return $result; })); $worksheet = $this->getMockBuilder(Worksheet::class) ->setMethods(['cellExists', 'getCell']) ->disableOriginalConstructor() @@ -643,6 +646,7 @@ class MathTrigTest extends TestCase ->willReturn($worksheet); array_push($args, $cellReference); + $result = MathTrig::SUBTOTAL(...$args); self::assertEquals($expectedResult, $result, null, 1E-12); } @@ -651,5 +655,4 @@ class MathTrigTest extends TestCase { return require 'data/Calculation/MathTrig/SUBTOTALNESTED.php'; } - } diff --git a/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php b/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php index 564f0f2b..61446cd4 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php +++ b/tests/data/Calculation/MathTrig/SUBTOTALNESTED.php @@ -1,8 +1,8 @@ ['A' => 1], - 2 => ['A' => '=2*1'], + 1 => ['A' => 123], + 2 => ['A' => 234], 3 => ['A' => '=SUBTOTAL(1, A1:A2)'], 4 => ['A' => '=ROMAN(SUBTOTAL(1, A1:A2))'], 5 => ['A' => 'This is text containing "=" and "SUBTOTAL("'], @@ -10,8 +10,8 @@ $baseTestData = [ return [ [ - 2, - 2, + 357, + 9, $baseTestData, ], ];