From 4a65466b6135d2aa28dd3bc52785a5e999f95fe8 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 28 Mar 2017 15:07:48 +0200 Subject: [PATCH] Prevent error when close() called while writer already closed (#402) --- src/Spout/Writer/AbstractWriter.php | 5 ++++- tests/Spout/Writer/CSV/WriterTest.php | 17 +++++++++++++++++ tests/Spout/Writer/ODS/WriterTest.php | 19 ++++++++++++++++++- tests/Spout/Writer/XLSX/WriterTest.php | 17 +++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 85b16aa7..83e190f5 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -350,6 +350,10 @@ private function resetRowStyleToDefault() */ public function close() { + if (!$this->isWriterOpened) { + return; + } + $this->closeWriter(); if (is_resource($this->filePointer)) { @@ -378,4 +382,3 @@ private function closeAndAttemptToCleanupAllFiles() } } } - diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php index 1a95012d..0b9fdf94 100644 --- a/tests/Spout/Writer/CSV/WriterTest.php +++ b/tests/Spout/Writer/CSV/WriterTest.php @@ -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 */ diff --git a/tests/Spout/Writer/ODS/WriterTest.php b/tests/Spout/Writer/ODS/WriterTest.php index 7c4b396e..513f617e 100644 --- a/tests/Spout/Writer/ODS/WriterTest.php +++ b/tests/Spout/Writer/ODS/WriterTest.php @@ -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); @@ -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 */ diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index 562db8dd..8b9b233e 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -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 */