Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 4a65466

Browse files
authored
Prevent error when close() called while writer already closed (#402)
1 parent 7427806 commit 4a65466

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/Spout/Writer/AbstractWriter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ private function resetRowStyleToDefault()
350350
*/
351351
public function close()
352352
{
353+
if (!$this->isWriterOpened) {
354+
return;
355+
}
356+
353357
$this->closeWriter();
354358

355359
if (is_resource($this->filePointer)) {
@@ -378,4 +382,3 @@ private function closeAndAttemptToCleanupAllFiles()
378382
}
379383
}
380384
}
381-

tests/Spout/Writer/CSV/WriterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
6161
$writer->close();
6262
}
6363

64+
/**
65+
* @return void
66+
*/
67+
public function testCloseShouldNoopWhenWriterIsNotOpened()
68+
{
69+
$fileName = 'test_double_close_calls.csv';
70+
$this->createGeneratedFolderIfNeeded($fileName);
71+
$resourcePath = $this->getGeneratedResourcePath($fileName);
72+
73+
$writer = WriterFactory::create(Type::CSV);
74+
$writer->close(); // This call should not cause any error
75+
76+
$writer->openToFile($fileName);
77+
$writer->close();
78+
$writer->close(); // This call should not cause any error
79+
}
80+
6481
/**
6582
* @return void
6683
*/

tests/Spout/Writer/ODS/WriterTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testSetTempFolderShouldThrowExceptionIfCalledAfterOpeningWriter(
6767
/**
6868
* @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
6969
*/
70-
public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
70+
public function testSetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
7171
{
7272
$fileName = 'file_that_wont_be_written.ods';
7373
$filePath = $this->getGeneratedResourcePath($fileName);
@@ -168,6 +168,23 @@ public function testSetCurrentSheet()
168168
$this->assertEquals($firstSheet, $writer->getCurrentSheet(), 'The current sheet should be the first one.');
169169
}
170170

171+
/**
172+
* @return void
173+
*/
174+
public function testCloseShouldNoopWhenWriterIsNotOpened()
175+
{
176+
$fileName = 'test_double_close_calls.ods';
177+
$this->createGeneratedFolderIfNeeded($fileName);
178+
$resourcePath = $this->getGeneratedResourcePath($fileName);
179+
180+
$writer = WriterFactory::create(Type::ODS);
181+
$writer->close(); // This call should not cause any error
182+
183+
$writer->openToFile($fileName);
184+
$writer->close();
185+
$writer->close(); // This call should not cause any error
186+
}
187+
171188
/**
172189
* @return void
173190
*/

tests/Spout/Writer/XLSX/WriterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ public function testSetCurrentSheet()
194194
$this->assertEquals($firstSheet, $writer->getCurrentSheet(), 'The current sheet should be the first one.');
195195
}
196196

197+
/**
198+
* @return void
199+
*/
200+
public function testCloseShouldNoopWhenWriterIsNotOpened()
201+
{
202+
$fileName = 'test_double_close_calls.xlsx';
203+
$this->createGeneratedFolderIfNeeded($fileName);
204+
$resourcePath = $this->getGeneratedResourcePath($fileName);
205+
206+
$writer = WriterFactory::create(Type::XLSX);
207+
$writer->close(); // This call should not cause any error
208+
209+
$writer->openToFile($fileName);
210+
$writer->close();
211+
$writer->close(); // This call should not cause any error
212+
}
213+
197214
/**
198215
* @return void
199216
*/

0 commit comments

Comments
 (0)