This repository was archived by the owner on May 26, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +56
-2
lines changed
Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Original file line number Diff line number Diff line change @@ -350,6 +350,10 @@ private function resetRowStyleToDefault()
350350 */
351351 public function close ()
352352 {
353+ if (!$ this ->isWriterOpened ) {
354+ return ;
355+ }
356+
353357 $ this ->closeWriter ();
354358
355359 if (is_resource ($ this ->filePointer )) {
@@ -378,4 +382,3 @@ private function closeAndAttemptToCleanupAllFiles()
378382 }
379383 }
380384}
381-
Original file line number Diff line number Diff line change @@ -61,6 +61,23 @@ public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
6161 $ writer ->close ();
6262 }
6363
64+ /**
65+ * @return void
66+ */
67+ public function testCloseShouldNoopWhenWriterIsNotOpened ()
68+ {
69+ $ fileName = 'test_double_close_calls.csv ' ;
70+ $ this ->createGeneratedFolderIfNeeded ($ fileName );
71+ $ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
72+
73+ $ writer = WriterFactory::create (Type::CSV );
74+ $ writer ->close (); // This call should not cause any error
75+
76+ $ writer ->openToFile ($ fileName );
77+ $ writer ->close ();
78+ $ writer ->close (); // This call should not cause any error
79+ }
80+
6481 /**
6582 * @return void
6683 */
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ public function testSetTempFolderShouldThrowExceptionIfCalledAfterOpeningWriter(
6767 /**
6868 * @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException
6969 */
70- public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter ()
70+ public function testSetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter ()
7171 {
7272 $ fileName = 'file_that_wont_be_written.ods ' ;
7373 $ filePath = $ this ->getGeneratedResourcePath ($ fileName );
@@ -168,6 +168,23 @@ public function testSetCurrentSheet()
168168 $ this ->assertEquals ($ firstSheet , $ writer ->getCurrentSheet (), 'The current sheet should be the first one. ' );
169169 }
170170
171+ /**
172+ * @return void
173+ */
174+ public function testCloseShouldNoopWhenWriterIsNotOpened ()
175+ {
176+ $ fileName = 'test_double_close_calls.ods ' ;
177+ $ this ->createGeneratedFolderIfNeeded ($ fileName );
178+ $ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
179+
180+ $ writer = WriterFactory::create (Type::ODS );
181+ $ writer ->close (); // This call should not cause any error
182+
183+ $ writer ->openToFile ($ fileName );
184+ $ writer ->close ();
185+ $ writer ->close (); // This call should not cause any error
186+ }
187+
171188 /**
172189 * @return void
173190 */
Original file line number Diff line number Diff line change @@ -194,6 +194,23 @@ public function testSetCurrentSheet()
194194 $ this ->assertEquals ($ firstSheet , $ writer ->getCurrentSheet (), 'The current sheet should be the first one. ' );
195195 }
196196
197+ /**
198+ * @return void
199+ */
200+ public function testCloseShouldNoopWhenWriterIsNotOpened ()
201+ {
202+ $ fileName = 'test_double_close_calls.xlsx ' ;
203+ $ this ->createGeneratedFolderIfNeeded ($ fileName );
204+ $ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
205+
206+ $ writer = WriterFactory::create (Type::XLSX );
207+ $ writer ->close (); // This call should not cause any error
208+
209+ $ writer ->openToFile ($ fileName );
210+ $ writer ->close ();
211+ $ writer ->close (); // This call should not cause any error
212+ }
213+
197214 /**
198215 * @return void
199216 */
You can’t perform that action at this time.
0 commit comments