Throw if XLSX Writer configured after being opened

This commit is contained in:
Adrien Loison 2015-08-24 10:44:46 -07:00
parent b4ace972e7
commit 1812b4f996
3 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace Box\Spout\Writer\Exception;
/**
* Class WriterAlreadyOpenedException
*
* @package Box\Spout\Writer\Exception
*/
class WriterAlreadyOpenedException extends WriterException
{
}

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Writer\AbstractWriter;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\XLSX\Internal\Workbook;
@ -38,37 +39,67 @@ class Writer extends AbstractWriter
protected $highestRowIndex = 0;
/**
* Sets a custom temporary folder for creating intermediate files/folders.
* This must be set before opening the writer.
*
* @param string $tempFolder Temporary folder where the files to create the XLSX will be stored
* @return Writer
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
*/
public function setTempFolder($tempFolder)
{
$this->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.
*

View File

@ -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
*/