Update Reader/WriterEntityFactory

Add `WriterEntityFactory::createWriterFromFile`, working like `ReaderEntityFactory::createReaderFromFile` (guessing writer type from file name).
Use static functions when needed.
This commit is contained in:
Adrien Loison authored and Adrien Loison committed 2019-05-17 13:22:27 +02:00
1 parent e8693834a0
commit 4a9d0398ad
6 files changed
+124 -19

No files matched your search

@@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;
/**
@@ -10,6 +11,48 @@ use PHPUnit\Framework\TestCase;
*/
class WriterFactoryTest extends TestCase
{
use TestUsingResource;
/**
* @return void
*/
public function testCreateFromFileCSV()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.csv');
$writer = WriterFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Writer\CSV\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileCSVAllCaps()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.CSV');
$writer = WriterFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Writer\CSV\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileODS()
{
$validOds = $this->getResourcePath('csv_test_create_from_file.ods');
$writer = WriterFactory::createFromFile($validOds);
$this->assertInstanceOf('Box\Spout\Writer\ODS\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileXLSX()
{
$validXlsx = $this->getResourcePath('csv_test_create_from_file.xlsx');
$writer = WriterFactory::createFromFile($validXlsx);
$this->assertInstanceOf('Box\Spout\Writer\XLSX\Writer', $writer);
}
/**
* @return void
*/
@@ -17,6 +60,16 @@ class WriterFactoryTest extends TestCase
{
$this->expectException(UnsupportedTypeException::class);
(new WriterFactory())->create('unsupportedType');
WriterFactory::create('unsupportedType');
}
/**
* @return void
*/
public function testCreateFromFileUnsupported()
{
$this->expectException(UnsupportedTypeException::class);
$invalid = $this->getResourcePath('test_unsupported_file_type.other');
WriterFactory::createFromFile($invalid);
}
}