spout/tests/Spout/Reader/Common/Creator/ReaderFactoryTest.php
Adrien Loison 40ee386edd Add helper functions to create specific readers and writers
Removed the `ReaderEntityFactory::createReader(Type)` method and replaced it by 3 methods:
- `ReaderEntityFactory::createCSVReader()`
- `ReaderEntityFactory::createXLSXReader()`
- `ReaderEntityFactory::createODSReader()`

This has the advantage of enabling autocomplete in the IDE, as the return type is no longer the interface but the concrete type. Since readers may expose different options, this is pretty useful.

Similarly, removed the `WriterEntityFactory::createWriter(Type)` method and replaced it by 3 methods:
- `WriterEntityFactory::createCSVWriter()`
- `WriterEntityFactory::createXLSXWriter()`
- `WriterEntityFactory::createODSWriter()`

Since this is a breaking change, I also updated the Upgrade guide.
Finally, the doc is up to date too.
2019-05-17 21:22:03 +02:00

86 lines
2.3 KiB
PHP

<?php
namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;
/**
* Class ReaderFactoryTest
*/
class ReaderFactoryTest extends TestCase
{
use TestUsingResource;
/**
* @return void
*/
public function testCreateFromFileCSV()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.csv');
$reader = ReaderFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Reader\CSV\Reader', $reader);
}
/**
* @return void
*/
public function testCreateFromFileCSVAllCaps()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.CSV');
$reader = ReaderFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Reader\CSV\Reader', $reader);
}
/**
* @return void
*/
public function testCreateFromFileODS()
{
$validOds = $this->getResourcePath('csv_test_create_from_file.ods');
$reader = ReaderFactory::createFromFile($validOds);
$this->assertInstanceOf('Box\Spout\Reader\ODS\Reader', $reader);
}
/**
* @return void
*/
public function testCreateFromFileXLSX()
{
$validXlsx = $this->getResourcePath('csv_test_create_from_file.xlsx');
$reader = ReaderFactory::createFromFile($validXlsx);
$this->assertInstanceOf('Box\Spout\Reader\XLSX\Reader', $reader);
}
/**
* @return void
*/
public function testCreateReaderShouldThrowWithUnsupportedType()
{
$this->expectException(UnsupportedTypeException::class);
ReaderFactory::createFromType('unsupportedType');
}
/**
* @return void
*/
public function testCreateFromFileUnsupported()
{
$this->expectException(UnsupportedTypeException::class);
$invalid = $this->getResourcePath('test_unsupported_file_type.other');
ReaderFactory::createFromFile($invalid);
}
/**
* @return void
*/
public function testCreateFromFileMissingShouldWork()
{
$notExistingFile = 'thereisnosuchfile.csv';
$reader = ReaderEntityFactory::createReaderFromFile($notExistingFile);
$this->assertInstanceOf('Box\Spout\Reader\CSV\Reader', $reader);
}
}