spreadSheet = new Spreadsheet(); $workSheet = $this->spreadSheet->getActiveSheet(); $workSheet->fromArray($this->tableData, null, 'A1'); $table = new Table('A1:E8', 'DeptSales'); $table->setShowTotalsRow(true); $table->getColumn('A')->setTotalsRowLabel('Total'); $workSheet->addTable($table); } protected function tearDown(): void { $this->spreadSheet->disconnectWorksheets(); parent::tearDown(); } public function testStructuredReferenceInvalidTable(): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('H5'); $this->expectException(Exception::class); $this->expectExceptionMessage('Table SalesResults for Structured Reference cannot be located'); $structuredReferenceObject = new StructuredReference('SalesResults[@[% Commission]]'); $structuredReferenceObject->parse($cell); } public function testStructuredReferenceInvalidCellForTable(): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('H99'); $this->expectException(Exception::class); $this->expectExceptionMessage('Table for Structured Reference cannot be identified'); $structuredReferenceObject = new StructuredReference('[@[% Commission]]'); $structuredReferenceObject->parse($cell); } /** * @dataProvider structuredReferenceProviderColumnData */ public function testStructuredReferenceColumns(string $expectedCellRange, string $structuredReference): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('E5'); $structuredReferenceObject = new StructuredReference($structuredReference); $cellRange = $structuredReferenceObject->parse($cell); self::assertSame($expectedCellRange, $cellRange); } /** * @dataProvider structuredReferenceProviderRowData */ public function testStructuredReferenceRows(string $expectedCellRange, string $structuredReference): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('E5'); $structuredReferenceObject = new StructuredReference($structuredReference); $cellRange = $structuredReferenceObject->parse($cell); self::assertSame($expectedCellRange, $cellRange); } public function testInvalidStructuredReferenceRow(): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('E5'); $this->expectException(Exception::class); $this->expectExceptionCode(1); $this->expectExceptionMessage('Invalid Structured Reference'); $this->expectExceptionCode(Exception::CALCULATION_ENGINE_PUSH_TO_STACK); $structuredReferenceObject = new StructuredReference('DeptSales[@[Sales]:[%age Commission]]'); $structuredReferenceObject->parse($cell); } public function testStructuredReferenceHeadersHidden(): void { $cell = $this->spreadSheet->getActiveSheet()->getCell('K1'); $table = $this->spreadSheet->getActiveSheet()->getTableByName('DeptSales'); /** @var Table $table */ $structuredReferenceObject = new StructuredReference('DeptSales[[#Headers],[% Commission]]'); $cellRange = $structuredReferenceObject->parse($cell); self::assertSame('D1', $cellRange); $table->setShowHeaderRow(false); $this->expectException(Exception::class); $this->expectExceptionMessage('Table Headers are Hidden, and should not be Referenced'); $this->expectExceptionCode(Exception::CALCULATION_ENGINE_PUSH_TO_STACK); $structuredReferenceObject = new StructuredReference('DeptSales[[#Headers],[% Commission]]'); $structuredReferenceObject->parse($cell); } public static function structuredReferenceProviderColumnData(): array { return [ // Full table, with no column specified, means data only, not headers or totals 'Full table Unqualified' => ['A2:E7', '[]'], 'Full table Qualified' => ['A2:E7', 'DeptSales[]'], // No item identifier, but with a column identifier, means data and header for the column, but no totals 'Column with no Item Identifier #1' => ['A2:A7', 'DeptSales[[Sales Person]]'], 'Column with no Item Identifier #2' => ['B2:B7', 'DeptSales[Region]'], // Item identifier with no column specified 'Item Identifier only #1' => ['A1:E1', 'DeptSales[#Headers]'], 'Item Identifier only #2' => ['A1:E1', 'DeptSales[[#Headers]]'], 'Item Identifier only #3' => ['A8:E8', 'DeptSales[#Totals]'], 'Item Identifier only #4' => ['A2:E7', 'DeptSales[#Data]'], // Item identifiers and column identifiers 'Full column' => ['C1:C8', 'DeptSales[[#All],[Sales Amount]]'], 'Column Header' => ['D1', 'DeptSales[[#Headers],[% Commission]]'], 'Column Total' => ['B8', 'DeptSales[[#Totals],[Region]]'], 'Column Range All' => ['C1:D8', 'DeptSales[[#All],[Sales Amount]:[% Commission]]'], 'Column Range Data' => ['D2:E7', 'DeptSales[[#Data],[% Commission]:[Commission Amount]]'], 'Column Range Headers' => ['B1:E1', 'DeptSales[[#Headers],[Region]:[Commission Amount]]'], 'Column Range Totals' => ['C8:E8', 'DeptSales[[#Totals],[Sales Amount]:[Commission Amount]]'], 'Column Range Headers and Data' => ['D1:D7', 'DeptSales[[#Headers],[#Data],[% Commission]]'], 'Column Range No Item Identifier' => ['A2:B7', 'DeptSales[[Sales Person]:[Region]]'], // ['C2:C7,E2:E7', 'DeptSales[Sales Amount],DeptSales[Commission Amount]'], // ['B2:C7', 'DeptSales[[Sales Person]:[Sales Amount]] DeptSales[[Region]:[% Commission]]'], ]; } public static function structuredReferenceProviderRowData(): array { return [ ['E5', 'DeptSales[[#This Row], [Commission Amount]]'], ['E5', 'DeptSales[@Commission Amount]'], ['E5', 'DeptSales[@[Commission Amount]]'], ['C5:D5', 'DeptSales[@[Sales Amount]:[% Commission]]'], ]; } }