diff --git a/src/Spout/Writer/Exception/WriterAlreadyOpenedException.php b/src/Spout/Writer/Exception/WriterAlreadyOpenedException.php new file mode 100644 index 0000000..f4c49a8 --- /dev/null +++ b/src/Spout/Writer/Exception/WriterAlreadyOpenedException.php @@ -0,0 +1,12 @@ +throwIfWriterAlreadyOpened(); + $this->tempFolder = $tempFolder; return $this; } /** * Use inline string to be more memory efficient. If set to false, it will use shared strings. + * This must be set before opening the writer. * * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used * @return Writer + * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened */ public function setShouldUseInlineStrings($shouldUseInlineStrings) { + $this->throwIfWriterAlreadyOpened(); + $this->shouldUseInlineStrings = $shouldUseInlineStrings; return $this; } /** + * Sets whether new sheets should be automatically created when the max rows limit per sheet is reached. + * This must be set before opening the writer. + * * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached * @return Writer + * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened */ public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) { + $this->throwIfWriterAlreadyOpened(); + $this->shouldCreateNewSheetsAutomatically = $shouldCreateNewSheetsAutomatically; return $this; } + /** + * Checks if the writer has already been opened, since some actions must be done before it gets opened. + * Throws an exception if already opened. + * + * @return void + * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. + */ + protected function throwIfWriterAlreadyOpened() + { + if ($this->isWriterOpened) { + throw new WriterAlreadyOpenedException('Writer must be configured before opening it.'); + } + } + /** * Configures the write and sets the current sheet pointer to a new sheet. * diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index 7924248..3c07838 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -46,6 +46,51 @@ class WriterTest extends \PHPUnit_Framework_TestCase $writer->addRows([['xlsx--11', 'xlsx--12']]); } + /** + * @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException + */ + public function testSetTempFolderShouldThrowExceptionIfCalledAfterOpeningWriter() + { + $fileName = 'file_that_wont_be_written.xlsx'; + $filePath = $this->getGeneratedResourcePath($fileName); + + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::XLSX); + $writer->openToFile($filePath); + + $writer->setTempFolder(''); + } + + /** + * @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException + */ + public function testSetShouldUseInlineStringsShouldThrowExceptionIfCalledAfterOpeningWriter() + { + $fileName = 'file_that_wont_be_written.xlsx'; + $filePath = $this->getGeneratedResourcePath($fileName); + + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::XLSX); + $writer->openToFile($filePath); + + $writer->setShouldUseInlineStrings(true); + } + + /** + * @expectedException \Box\Spout\Writer\Exception\WriterAlreadyOpenedException + */ + public function testsetShouldCreateNewSheetsAutomaticallyShouldThrowExceptionIfCalledAfterOpeningWriter() + { + $fileName = 'file_that_wont_be_written.xlsx'; + $filePath = $this->getGeneratedResourcePath($fileName); + + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::XLSX); + $writer->openToFile($filePath); + + $writer->setShouldCreateNewSheetsAutomatically(true); + } + /** * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException */