From 04b4e74ec7234b8aa369a395fb5579232c8b8c4a Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 20 Apr 2018 07:24:11 +0100 Subject: [PATCH] Basic unit testing for 100 series actions for SUBTOTAL() with hidden rows/columns --- src/PhpSpreadsheet/Calculation/MathTrig.php | 1 - .../Calculation/MathTrigTest.php | 85 ++++++++++++++++++- tests/data/Calculation/MathTrig/SUBTOTAL.php | 48 ++++++++--- .../Calculation/MathTrig/SUBTOTALHIDDEN.php | 74 ++++++++++++++++ 4 files changed, 194 insertions(+), 14 deletions(-) create mode 100644 tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 889d72a9..bdc1e026 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1073,7 +1073,6 @@ class MathTrig $args, function ($index) use ($cellReference) { list(, $row, $column) = explode('.', $index); - return $cellReference->getWorksheet()->getRowDimension($row)->getVisible() && $cellReference->getWorksheet()->getColumnDimension($column)->getVisible(); }, diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index e6992360..4d6048f1 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -5,6 +5,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; +use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; +use PhpOffice\PhpSpreadsheet\Cell\Cell; use PHPUnit\Framework\TestCase; class MathTrigTest extends TestCase @@ -507,8 +511,26 @@ class MathTrigTest extends TestCase */ public function testSUBTOTAL($expectedResult, ...$args) { - $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); - $cellReference = $spreadsheet->getActiveSheet()->getCell('A1'); + $cell = $this->getMockBuilder(Cell::class) + ->setMethods(['getValue']) + ->disableOriginalConstructor() + ->getMock(); + $cell->method('getValue') + ->willReturn(null); + $worksheet = $this->getMockBuilder(Worksheet::class) + ->setMethods(['cellExists', 'getCell']) + ->disableOriginalConstructor() + ->getMock(); + $worksheet->method('cellExists') + ->willReturn(true); + $worksheet->method('getCell') + ->willReturn($cell); + $cellReference = $this->getMockBuilder(Cell::class) + ->setMethods(['getWorksheet']) + ->disableOriginalConstructor() + ->getMock(); + $cellReference->method('getWorksheet') + ->willReturn($worksheet); array_push($args, $cellReference); $result = MathTrig::SUBTOTAL(...$args); @@ -519,4 +541,63 @@ class MathTrigTest extends TestCase { return require 'data/Calculation/MathTrig/SUBTOTAL.php'; } + + protected static 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]; + } + + /** + * @dataProvider providerSUBTOTALHIDDEN + * + * @param mixed $expectedResult + */ + public function testSUBTOTALHIDDEN($expectedResult, ...$args) + { + $generator = \PhpOffice\PhpSpreadsheetTests\Calculation\MathTrigTest::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; })); + $columnDimension = $this->getMockBuilder(ColumnDimension::class) + ->setMethods(['getVisible']) + ->disableOriginalConstructor() + ->getMock(); + $columnDimension->method('getVisible') + ->willReturn(true); + $cell = $this->getMockBuilder(Cell::class) + ->setMethods(['getValue']) + ->disableOriginalConstructor() + ->getMock(); + $cell->method('getValue') + ->willReturn(''); + $worksheet = $this->getMockBuilder(Worksheet::class) + ->setMethods(['cellExists', 'getCell', 'getRowDimension', 'getColumnDimension']) + ->disableOriginalConstructor() + ->getMock(); + $worksheet->method('cellExists') + ->willReturn(true); + $worksheet->method('getCell') + ->willReturn($cell); + $worksheet->method('getRowDimension') + ->willReturn($rowDimension); + $worksheet->method('getColumnDimension') + ->willReturn($columnDimension); + $cellReference = $this->getMockBuilder(Cell::class) + ->setMethods(['getWorksheet']) + ->disableOriginalConstructor() + ->getMock(); + $cellReference->method('getWorksheet') + ->willReturn($worksheet); + + array_push($args, $cellReference); + $result = MathTrig::SUBTOTAL(...$args); + self::assertEquals($expectedResult, $result, null, 1E-12); + } + + public function providerSUBTOTALHIDDEN() + { + return require 'data/Calculation/MathTrig/SUBTOTALHIDDEN.php'; + } } diff --git a/tests/data/Calculation/MathTrig/SUBTOTAL.php b/tests/data/Calculation/MathTrig/SUBTOTAL.php index b11819fd..419c71c5 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTAL.php +++ b/tests/data/Calculation/MathTrig/SUBTOTAL.php @@ -1,48 +1,74 @@ ['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] +]; + 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] ] + 1, + $baseTestData, ], [ 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] ] + 2, + $baseTestData, ], [ 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] ] + 3, + $baseTestData, ], [ 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] ] + 4, + $baseTestData, ], [ 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] ] + 5, + $baseTestData, ], [ 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] ] + 6, + $baseTestData, ], [ 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] ] + 7, + $baseTestData, ], [ 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] ] + 8, + $baseTestData, ], [ 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] ] + 9, + $baseTestData, ], [ 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] ] + 10, + $baseTestData, ], [ 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] ] + 11, + $baseTestData, ], ]; diff --git a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php new file mode 100644 index 00000000..e70b81d5 --- /dev/null +++ b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php @@ -0,0 +1,74 @@ + ['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] +]; + +return [ + [ + 21, + 101, + $baseTestData, + ], + [ + 5, + 102, + $baseTestData, + ], + [ + 5, + 103, + $baseTestData, + ], + [ + 55, + 104, + $baseTestData, + ], + [ + 1, + 105, + $baseTestData, + ], + [ + 48620, + 106, + $baseTestData, + ], + [ + 23.1840462387393, + 107, + $baseTestData, + ], + [ + 20.7364413533277, + 108, + $baseTestData, + ], + [ + 105, + 109, + $baseTestData, + ], + [ + 537.5, + 110, + $baseTestData, + ], + [ + 430, + 111, + $baseTestData, + ], +];