diff --git a/src/Spout/Writer/XLSX/Writer.php b/src/Spout/Writer/XLSX/Writer.php index 965955a..1a6ad3f 100644 --- a/src/Spout/Writer/XLSX/Writer.php +++ b/src/Spout/Writer/XLSX/Writer.php @@ -125,7 +125,7 @@ class Writer extends AbstractMultiSheetsWriter */ protected function closeWriter() { - if ($this->book) { + if ($this->book && $this->isWriterOpened) { $this->book->close($this->filePointer); } } diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index c7f4f96..71aba50 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -507,6 +507,19 @@ class WriterTest extends \PHPUnit_Framework_TestCase $this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $finfo->file($resourcePath)); } + /** + * @return void + */ + public function testCloseIsNotAppliedWhenWriterIsNotOpened() + { + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::XLSX); + $writer->openToFile('test_close.xlsx'); + $writer->addRow(['foo']); + $writer->close(); + $writer->close(); + } + /** * @param array $allRows * @param string $fileName