Basic unit test for SUBTOTAL() function

This commit is contained in:
MarkBaker 2018-04-19 22:44:23 +01:00
parent df34b15079
commit 1ece540254
3 changed files with 74 additions and 5 deletions

View File

@ -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

View File

@ -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';
}
}

View File

@ -0,0 +1,48 @@
<?php
return [
[
19.3333333333333,
1, [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,
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] ]
],
];