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

Commit

Permalink
Prevent error when close() called while writer already closed (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrilo authored Mar 28, 2017
1 parent 7427806 commit 4a65466
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Spout/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ private function resetRowStyleToDefault()
*/
public function close()
{
if (!$this->isWriterOpened) {
return;
}

$this->closeWriter();

if (is_resource($this->filePointer)) {
Expand Down Expand Up @@ -378,4 +382,3 @@ private function closeAndAttemptToCleanupAllFiles()
}
}
}

17 changes: 17 additions & 0 deletions tests/Spout/Writer/CSV/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
$writer->close();
}

/**
* @return void
*/
public function testCloseShouldNoopWhenWriterIsNotOpened()
{
$fileName = 'test_double_close_calls.csv';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);

$writer = WriterFactory::create(Type::CSV);
$writer->close(); // This call should not cause any error

$writer->openToFile($fileName);
$writer->close();
$writer->close(); // This call should not cause any error
}

/**
* @return void
*/
Expand Down
19 changes: 18 additions & 1 deletion tests/Spout/Writer/ODS/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testSetTempFolderShouldThrowExceptionIfCalledAfterOpeningWriter(
/**
* @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
*/
public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
public function testSetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
{
$fileName = 'file_that_wont_be_written.ods';
$filePath = $this->getGeneratedResourcePath($fileName);
Expand Down Expand Up @@ -168,6 +168,23 @@ public function testSetCurrentSheet()
$this->assertEquals($firstSheet, $writer->getCurrentSheet(), 'The current sheet should be the first one.');
}

/**
* @return void
*/
public function testCloseShouldNoopWhenWriterIsNotOpened()
{
$fileName = 'test_double_close_calls.ods';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);

$writer = WriterFactory::create(Type::ODS);
$writer->close(); // This call should not cause any error

$writer->openToFile($fileName);
$writer->close();
$writer->close(); // This call should not cause any error
}

/**
* @return void
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Spout/Writer/XLSX/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ public function testSetCurrentSheet()
$this->assertEquals($firstSheet, $writer->getCurrentSheet(), 'The current sheet should be the first one.');
}

/**
* @return void
*/
public function testCloseShouldNoopWhenWriterIsNotOpened()
{
$fileName = 'test_double_close_calls.xlsx';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);

$writer = WriterFactory::create(Type::XLSX);
$writer->close(); // This call should not cause any error

$writer->openToFile($fileName);
$writer->close();
$writer->close(); // This call should not cause any error
}

/**
* @return void
*/
Expand Down

0 comments on commit 4a65466

Please sign in to comment.