Skip to content

Commit ea32636

Browse files
committed
Minimum Table range validation
Range must be at least 1 column and 2 rows
1 parent 3c4a51a commit ea32636

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/PhpSpreadsheet/Worksheet/Table.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public function setRange(string $range): self
190190
throw new PhpSpreadsheetException('Table must be set on a range of cells.');
191191
}
192192

193+
[$width, $height] = Coordinate::rangeDimension($range);
194+
if ($width < 1 || $height < 2) {
195+
throw new PhpSpreadsheetException('The table range must be at least 1 column and 2 rows');
196+
}
197+
193198
$this->range = $range;
194199
// Discard any column ruless that are no longer valid within this range
195200
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($this->range);

tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,26 @@ public function testClearRange(): void
173173
self::assertEquals($expectedResult, $result);
174174
}
175175

176-
public function testSetRangeInvalidRange(): void
176+
/**
177+
* @dataProvider invalidTableRangeProvider
178+
*/
179+
public function testSetRangeInvalidRange(string $range): void
177180
{
178181
$this->expectException(PhpSpreadsheetException::class);
179182

180-
$expectedResult = 'A1';
181-
182183
$sheet = $this->getSheet();
183-
$table = new Table($expectedResult, $sheet);
184+
new Table($range, $sheet);
185+
}
186+
187+
public function invalidTableRangeProvider(): array
188+
{
189+
return [
190+
['A1'],
191+
['A1:A1'],
192+
['B1:A4'],
193+
['A1:D1'],
194+
['D1:A1'],
195+
];
184196
}
185197

186198
public function testGetColumnsEmpty(): void

0 commit comments

Comments
 (0)