Skip to content

Commit abc1d3d

Browse files
authored
Big Memory Leak in One Test (#2958)
* Big Memory Leak in One Test Many tests leak a little by not issuing disconnectWorksheets at the end. CellMatcherTest leaks a lot by opening a spreadsheet many times. Phpunit reported a high watermark of 390MB; fixing CellMatcherTest brings that figure down to 242MB. I have also changed it to use a formal assertion in many cases where markTestSkipped was (IMO inappropriately) used. * Another Leak Issue2301Test lacks a disconnect. Adding one reduces HWM from 242MB to 224.
1 parent 0ee1330 commit abc1d3d

File tree

2 files changed

+44
-63
lines changed

2 files changed

+44
-63
lines changed

tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2301Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public static function testReadRichText(): void
2424
self::assertSame('Arial CE', $font->getName());
2525
self::assertSame(9.0, $font->getSize());
2626
self::assertSame('protected', $sheet->getCell('BT10')->getStyle()->getProtection()->getHidden());
27+
$spreadsheet->disconnectWorksheets();
28+
unset($spreadsheet);
2729
}
2830
}

tests/PhpSpreadsheetTests/Style/ConditionalFormatting/CellMatcherTest.php

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,38 @@
1010
class CellMatcherTest extends TestCase
1111
{
1212
/**
13-
* @var Spreadsheet
13+
* @var ?Spreadsheet
1414
*/
1515
protected $spreadsheet;
1616

17-
protected function setUp(): void
17+
protected function loadSpreadsheet(): Spreadsheet
1818
{
1919
$filename = 'tests/data/Style/ConditionalFormatting/CellMatcher.xlsx';
2020
$reader = IOFactory::createReader('Xlsx');
21-
$this->spreadsheet = $reader->load($filename);
21+
22+
return $reader->load($filename);
23+
}
24+
25+
protected function tearDown(): void
26+
{
27+
if ($this->spreadsheet !== null) {
28+
$this->spreadsheet->disconnectWorksheets();
29+
$this->spreadsheet = null;
30+
}
2231
}
2332

2433
/**
2534
* @dataProvider basicCellIsComparisonDataProvider
2635
*/
2736
public function testBasicCellIsComparison(string $sheetname, string $cellAddress, array $expectedMatches): void
2837
{
38+
$this->spreadsheet = $this->loadSpreadsheet();
2939
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
30-
if ($worksheet === null) {
31-
self::markTestSkipped("{$sheetname} not found in test workbook");
32-
}
40+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
3341
$cell = $worksheet->getCell($cellAddress);
3442

3543
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
36-
if ($cfRange === null) {
37-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
38-
}
44+
self::assertNotNull($cfRange, "{$cellAddress} is not in a Conditional Format range");
3945
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
4046

4147
$matcher = new CellMatcher($cell, $cfRange);
@@ -82,16 +88,13 @@ public function basicCellIsComparisonDataProvider(): array
8288
*/
8389
public function testRangeCellIsComparison(string $sheetname, string $cellAddress, bool $expectedMatch): void
8490
{
91+
$this->spreadsheet = $this->loadSpreadsheet();
8592
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
86-
if ($worksheet === null) {
87-
self::markTestSkipped("{$sheetname} not found in test workbook");
88-
}
93+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
8994
$cell = $worksheet->getCell($cellAddress);
9095

9196
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
92-
if ($cfRange === null) {
93-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
94-
}
97+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
9598
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
9699

97100
$matcher = new CellMatcher($cell, $cfRange);
@@ -128,16 +131,13 @@ public function rangeCellIsComparisonDataProvider(): array
128131
*/
129132
public function testCellIsMultipleExpression(string $sheetname, string $cellAddress, array $expectedMatches): void
130133
{
134+
$this->spreadsheet = $this->loadSpreadsheet();
131135
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
132-
if ($worksheet === null) {
133-
self::markTestSkipped("{$sheetname} not found in test workbook");
134-
}
136+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
135137
$cell = $worksheet->getCell($cellAddress);
136138

137139
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
138-
if ($cfRange === null) {
139-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
140-
}
140+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
141141
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
142142

143143
$matcher = new CellMatcher($cell, $cfRange);
@@ -167,16 +167,13 @@ public function cellIsExpressionMultipleDataProvider(): array
167167
*/
168168
public function testCellIsExpression(string $sheetname, string $cellAddress, bool $expectedMatch): void
169169
{
170+
$this->spreadsheet = $this->loadSpreadsheet();
170171
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
171-
if ($worksheet === null) {
172-
self::markTestSkipped("{$sheetname} not found in test workbook");
173-
}
172+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
174173
$cell = $worksheet->getCell($cellAddress);
175174

176175
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
177-
if ($cfRange === null) {
178-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
179-
}
176+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
180177
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
181178

182179
$matcher = new CellMatcher($cell, $cfRange);
@@ -216,16 +213,13 @@ public function cellIsExpressionDataProvider(): array
216213
*/
217214
public function testTextExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
218215
{
216+
$this->spreadsheet = $this->loadSpreadsheet();
219217
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
220-
if ($worksheet === null) {
221-
self::markTestSkipped("{$sheetname} not found in test workbook");
222-
}
218+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
223219
$cell = $worksheet->getCell($cellAddress);
224220

225221
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
226-
if ($cfRange === null) {
227-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
228-
}
222+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
229223
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
230224

231225
$matcher = new CellMatcher($cell, $cfRange);
@@ -329,16 +323,13 @@ public function textExpressionsDataProvider(): array
329323
*/
330324
public function testBlankExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
331325
{
326+
$this->spreadsheet = $this->loadSpreadsheet();
332327
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
333-
if ($worksheet === null) {
334-
self::markTestSkipped("{$sheetname} not found in test workbook");
335-
}
328+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
336329
$cell = $worksheet->getCell($cellAddress);
337330

338331
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
339-
if ($cfRange === null) {
340-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
341-
}
332+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
342333
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
343334

344335
$matcher = new CellMatcher($cell, $cfRange);
@@ -365,16 +356,13 @@ public function blanksDataProvider(): array
365356
*/
366357
public function testErrorExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
367358
{
359+
$this->spreadsheet = $this->loadSpreadsheet();
368360
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
369-
if ($worksheet === null) {
370-
self::markTestSkipped("{$sheetname} not found in test workbook");
371-
}
361+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
372362
$cell = $worksheet->getCell($cellAddress);
373363

374364
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
375-
if ($cfRange === null) {
376-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
377-
}
365+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
378366
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
379367

380368
$matcher = new CellMatcher($cell, $cfRange);
@@ -400,16 +388,13 @@ public function errorDataProvider(): array
400388
*/
401389
public function testDateOccurringExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
402390
{
391+
$this->spreadsheet = $this->loadSpreadsheet();
403392
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
404-
if ($worksheet === null) {
405-
self::markTestSkipped("{$sheetname} not found in test workbook");
406-
}
393+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
407394
$cell = $worksheet->getCell($cellAddress);
408395

409396
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
410-
if ($cfRange === null) {
411-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
412-
}
397+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
413398
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
414399

415400
$matcher = new CellMatcher($cell, $cfRange);
@@ -447,16 +432,13 @@ public function dateOccurringDataProvider(): array
447432
*/
448433
public function testDuplicatesExpressions(string $sheetname, string $cellAddress, array $expectedMatches): void
449434
{
435+
$this->spreadsheet = $this->loadSpreadsheet();
450436
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
451-
if ($worksheet === null) {
452-
self::markTestSkipped("{$sheetname} not found in test workbook");
453-
}
437+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
454438
$cell = $worksheet->getCell($cellAddress);
455439

456440
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
457-
if ($cfRange === null) {
458-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
459-
}
441+
self::AssertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
460442
$cfStyles = $worksheet->getConditionalStyles($cell->getCoordinate());
461443

462444
$matcher = new CellMatcher($cell, $cfRange);
@@ -486,16 +468,13 @@ public function duplicatesDataProvider(): array
486468
*/
487469
public function testCrossWorksheetExpressions(string $sheetname, string $cellAddress, bool $expectedMatch): void
488470
{
471+
$this->spreadsheet = $this->loadSpreadsheet();
489472
$worksheet = $this->spreadsheet->getSheetByName($sheetname);
490-
if ($worksheet === null) {
491-
self::markTestSkipped("{$sheetname} not found in test workbook");
492-
}
473+
self::assertNotNull($worksheet, "$sheetname not found in test workbook");
493474
$cell = $worksheet->getCell($cellAddress);
494475

495476
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
496-
if ($cfRange === null) {
497-
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
498-
}
477+
self::assertNotNull($cfRange, "$cellAddress is not in a Conditional Format range");
499478
$cfStyle = $worksheet->getConditionalStyles($cell->getCoordinate());
500479

501480
$matcher = new CellMatcher($cell, $cfRange);

0 commit comments

Comments
 (0)