| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Calculation\Functions; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Cell\Cell; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | 
					
						
							|  |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SubTotalTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-27 10:28:36 +00:00
										 |  |  |     protected function setUp(): void | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider providerSUBTOTAL | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $expectedResult | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |     public function testSUBTOTAL($expectedResult, ...$args): void | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $cell = $this->getMockBuilder(Cell::class) | 
					
						
							|  |  |  |             ->setMethods(['getValue', 'isFormula']) | 
					
						
							|  |  |  |             ->disableOriginalConstructor() | 
					
						
							|  |  |  |             ->getMock(); | 
					
						
							|  |  |  |         $cell->method('getValue') | 
					
						
							|  |  |  |             ->willReturn(null); | 
					
						
							|  |  |  |         $cell->method('getValue') | 
					
						
							|  |  |  |             ->willReturn(false); | 
					
						
							|  |  |  |         $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); | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |         self::assertEqualsWithDelta($expectedResult, $result, 1E-12); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function providerSUBTOTAL() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-05-17 09:35:55 +00:00
										 |  |  |         return require 'tests/data/Calculation/MathTrig/SUBTOTAL.php'; | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function rowVisibility() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 => true]; | 
					
						
							|  |  |  |         foreach ($data as $k => $v) { | 
					
						
							|  |  |  |             yield $k => $v; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider providerHiddenSUBTOTAL | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $expectedResult | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |     public function testHiddenSUBTOTAL($expectedResult, ...$args): void | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $visibilityGenerator = $this->rowVisibility(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $rowDimension = $this->getMockBuilder(RowDimension::class) | 
					
						
							|  |  |  |             ->setMethods(['getVisible']) | 
					
						
							|  |  |  |             ->disableOriginalConstructor() | 
					
						
							|  |  |  |             ->getMock(); | 
					
						
							|  |  |  |         $rowDimension->method('getVisible') | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             ->willReturnCallback(function () use ($visibilityGenerator) { | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |                 $result = $visibilityGenerator->current(); | 
					
						
							|  |  |  |                 $visibilityGenerator->next(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |         $columnDimension = $this->getMockBuilder(ColumnDimension::class) | 
					
						
							|  |  |  |             ->setMethods(['getVisible']) | 
					
						
							|  |  |  |             ->disableOriginalConstructor() | 
					
						
							|  |  |  |             ->getMock(); | 
					
						
							|  |  |  |         $columnDimension->method('getVisible') | 
					
						
							|  |  |  |             ->willReturn(true); | 
					
						
							|  |  |  |         $cell = $this->getMockBuilder(Cell::class) | 
					
						
							|  |  |  |             ->setMethods(['getValue', 'isFormula']) | 
					
						
							|  |  |  |             ->disableOriginalConstructor() | 
					
						
							|  |  |  |             ->getMock(); | 
					
						
							|  |  |  |         $cell->method('getValue') | 
					
						
							|  |  |  |             ->willReturn(''); | 
					
						
							|  |  |  |         $cell->method('getValue') | 
					
						
							|  |  |  |             ->willReturn(false); | 
					
						
							|  |  |  |         $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); | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |         self::assertEqualsWithDelta($expectedResult, $result, 1E-12); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function providerHiddenSUBTOTAL() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-05-17 09:35:55 +00:00
										 |  |  |         return require 'tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php'; | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function cellValues(array $cellValues) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($cellValues as $k => $v) { | 
					
						
							|  |  |  |             yield $k => $v; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function cellIsFormula(array $cellValues) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($cellValues as $cellValue) { | 
					
						
							| 
									
										
										
										
											2019-11-29 22:41:02 +00:00
										 |  |  |             yield is_string($cellValue) && $cellValue[0] === '='; | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider providerNestedSUBTOTAL | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $expectedResult | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |     public function testNestedSUBTOTAL($expectedResult, ...$args): void | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $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') | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             ->willReturnCallback(function () use ($cellValueGenerator) { | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |                 $result = $cellValueGenerator->current(); | 
					
						
							|  |  |  |                 $cellValueGenerator->next(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |         $cell->method('isFormula') | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             ->willReturnCallback(function () use ($cellIsFormulaGenerator) { | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |                 $result = $cellIsFormulaGenerator->current(); | 
					
						
							|  |  |  |                 $cellIsFormulaGenerator->next(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |         $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); | 
					
						
							| 
									
										
										
										
											2020-05-18 04:49:57 +00:00
										 |  |  |         self::assertEqualsWithDelta($expectedResult, $result, 1E-12); | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function providerNestedSUBTOTAL() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-05-17 09:35:55 +00:00
										 |  |  |         return require 'tests/data/Calculation/MathTrig/SUBTOTALNESTED.php'; | 
					
						
							| 
									
										
										
										
											2019-07-22 22:50:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |