Full unit tests for SUBTOTAL function

This commit is contained in:
MarkBaker 2018-05-06 23:43:02 +01:00
parent 3298667153
commit bbe11eed37
3 changed files with 18 additions and 14 deletions

View File

@ -1088,8 +1088,9 @@ class MathTrig
list(, $row, $column) = explode('.', $index); list(, $row, $column) = explode('.', $index);
if ($cellReference->getWorksheet()->cellExists($column . $row)) { if ($cellReference->getWorksheet()->cellExists($column . $row)) {
//take this cell out if it contains the SUBTOTAL formula //take this cell out if it contains the SUBTOTAL formula
return !$cellReference->getWorksheet()->getCell($column . $row)->isFormula() && $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula();
!preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); $cellFormula = !preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()));
return !$isFormula || $cellFormula;
} }
return true; return true;
}, },

View File

@ -603,9 +603,13 @@ class MathTrigTest extends TestCase
} }
protected function cellValues($cellValues) { 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) public function testNestedSUBTOTAL($expectedResult, ...$args)
{ {
$this->markTestSkipped('Revisite when cellValueGenarator() works for mocking getValue() calls');
$cellValueGenerator = $this->cellValues(Functions::flattenArray(array_slice($args, 1))); $cellValueGenerator = $this->cellValues(Functions::flattenArray(array_slice($args, 1)));
$cellIsFormulaGenerator = $this->cellIsFormula(Functions::flattenArray(array_slice($args, 1)));
$cell = $this->getMockBuilder(Cell::class) $cell = $this->getMockBuilder(Cell::class)
->setMethods(['getValue', 'isFormula']) ->setMethods(['getValue', 'isFormula'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$cell->method('getValue') $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') $cell->method('isFormula')
->willReturn(true); ->will($this->returnCallback(function() use ($cellIsFormulaGenerator) { $result = $cellIsFormulaGenerator->current(); $cellIsFormulaGenerator->next(); return $result; }));
$worksheet = $this->getMockBuilder(Worksheet::class) $worksheet = $this->getMockBuilder(Worksheet::class)
->setMethods(['cellExists', 'getCell']) ->setMethods(['cellExists', 'getCell'])
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -643,6 +646,7 @@ class MathTrigTest extends TestCase
->willReturn($worksheet); ->willReturn($worksheet);
array_push($args, $cellReference); array_push($args, $cellReference);
$result = MathTrig::SUBTOTAL(...$args); $result = MathTrig::SUBTOTAL(...$args);
self::assertEquals($expectedResult, $result, null, 1E-12); self::assertEquals($expectedResult, $result, null, 1E-12);
} }
@ -651,5 +655,4 @@ class MathTrigTest extends TestCase
{ {
return require 'data/Calculation/MathTrig/SUBTOTALNESTED.php'; return require 'data/Calculation/MathTrig/SUBTOTALNESTED.php';
} }
} }

View File

@ -1,8 +1,8 @@
<?php <?php
$baseTestData = [ $baseTestData = [
1 => ['A' => 1], 1 => ['A' => 123],
2 => ['A' => '=2*1'], 2 => ['A' => 234],
3 => ['A' => '=SUBTOTAL(1, A1:A2)'], 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("'], 5 => ['A' => 'This is text containing "=" and "SUBTOTAL("'],
@ -10,8 +10,8 @@ $baseTestData = [
return [ return [
[ [
2, 357,
2, 9,
$baseTestData, $baseTestData,
], ],
]; ];