Prevent error when close() called while writer already closed

This commit is contained in:
Adrien Loison 2017-03-28 14:53:00 +02:00
parent 742780613a
commit 2a9d35af3d
4 changed files with 56 additions and 2 deletions

View File

@ -350,6 +350,10 @@ abstract class AbstractWriter implements WriterInterface
*/
public function close()
{
if (!$this->isWriterOpened) {
return;
}
$this->closeWriter();
if (is_resource($this->filePointer)) {
@ -378,4 +382,3 @@ abstract class AbstractWriter implements WriterInterface
}
}
}

View File

@ -61,6 +61,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$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
*/

View File

@ -67,7 +67,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
*/
public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
public function testSetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
{
$fileName = 'file_that_wont_be_written.ods';
$filePath = $this->getGeneratedResourcePath($fileName);
@ -168,6 +168,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$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
*/

View File

@ -194,6 +194,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$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
*/