Prevent error when close() called while writer already closed (#402)
This commit is contained in:
parent
742780613a
commit
4a65466b61
@ -350,6 +350,10 @@ abstract class AbstractWriter implements WriterInterface
|
|||||||
*/
|
*/
|
||||||
public function close()
|
public function close()
|
||||||
{
|
{
|
||||||
|
if (!$this->isWriterOpened) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->closeWriter();
|
$this->closeWriter();
|
||||||
|
|
||||||
if (is_resource($this->filePointer)) {
|
if (is_resource($this->filePointer)) {
|
||||||
@ -378,4 +382,3 @@ abstract class AbstractWriter implements WriterInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$writer->close();
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -67,7 +67,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
|
* @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
|
||||||
*/
|
*/
|
||||||
public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
|
public function testSetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter()
|
||||||
{
|
{
|
||||||
$fileName = 'file_that_wont_be_written.ods';
|
$fileName = 'file_that_wont_be_written.ods';
|
||||||
$filePath = $this->getGeneratedResourcePath($fileName);
|
$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.');
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -194,6 +194,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($firstSheet, $writer->getCurrentSheet(), 'The current sheet should be the first one.');
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user