Test filtered columns are still excluded

This commit is contained in:
Alex Wright 2020-12-13 16:34:00 +01:00
parent e1018e2330
commit 0f4d11577a
1 changed files with 19 additions and 5 deletions

View File

@ -43,20 +43,26 @@ class ColumnWidthTest extends AbstractFunctional
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
foreach (range(1, 5) as $row) { foreach (range(1, 5) as $row) {
$sheet->setCellValue("A{$row}", 'Hello World !'); $sheet->setCellValue("A{$row}", 'Hello');
$sheet->setCellValue("B{$row}", 'World !');
} }
// isFilteredColumn iterates rowAttributes when calling the read filter // isFilteredColumn iterates rowAttributes when calling the read filter
$sheet->getRowDimension(5)->setRowHeight(10); $sheet->getRowDimension(5)->setRowHeight(10);
$sheet->getColumnDimension('A')->setWidth(20); $sheet->getColumnDimension('A')->setWidth(20);
$sheet->getColumnDimension('B')->setWidth(30);
$this->assertColumn($spreadsheet); $this->assertColumn($spreadsheet);
$this->assertColumn($spreadsheet, 'B', 30);
// A reader-customeiser closure and ReadFilter implementation that skips rows >4 // A reader-customeiser closure and ReadFilter implementation that skips rows >4
$readerCustomizer = function ($reader) { $readerCustomizer = function ($reader) {
$readFilterStub = $this->createMock(IReadFilter::class); $readFilterStub = $this->createMock(IReadFilter::class);
$readFilterStub->method('readCell') $readFilterStub->method('readCell')
->willReturnCallback(function ($column, $row, $worksheetName = '') { ->willReturnCallback(function ($column, $row, $worksheetName = '') {
if ($column == 'B') {
return false;
}
return $row <= 4; return $row <= 4;
}); });
$reader->setReadFilter($readFilterStub); $reader->setReadFilter($readFilterStub);
@ -65,15 +71,23 @@ class ColumnWidthTest extends AbstractFunctional
// Save and reload a filtered set and assert the same width // Save and reload a filtered set and assert the same width
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, $readerCustomizer); $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, $readerCustomizer);
$this->assertColumn($reloadedSpreadsheet); $this->assertColumn($reloadedSpreadsheet);
$this->assertNotColumn($reloadedSpreadsheet, 'B', 30);
} }
private function assertColumn(Spreadsheet $spreadsheet): void private function assertColumn(Spreadsheet $spreadsheet, $column = 'A', $expected = 20): void
{ {
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
$columnDimensions = $sheet->getColumnDimensions(); $columnDimensions = $sheet->getColumnDimensions();
self::assertArrayHasKey('A', $columnDimensions); self::assertArrayHasKey($column, $columnDimensions);
$column = array_shift($columnDimensions); self::assertEquals($expected, $columnDimensions[$column]->getWidth());
self::assertEquals(20, $column->getWidth()); }
private function assertNotColumn(Spreadsheet $spreadsheet, $column = 'A', $expected = 20): void
{
$sheet = $spreadsheet->getActiveSheet();
$columnDimensions = $sheet->getColumnDimensions();
self::assertArrayNotHasKey($column, $columnDimensions);
} }
} }