2016-12-08 15:15:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests;
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
2017-10-21 16:54:14 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Reader;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Writer;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-12-08 15:15:22 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class IOFactoryTest extends TestCase
|
2016-12-08 15:15:22 +00:00
|
|
|
{
|
2017-10-21 16:54:14 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerCreateWriter
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testCreateWriter($name, $expected)
|
|
|
|
{
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$actual = IOFactory::createWriter($spreadsheet, $name);
|
|
|
|
self::assertInstanceOf($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerCreateWriter()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['Xls', Writer\Xls::class],
|
|
|
|
['Xlsx', Writer\Xlsx::class],
|
|
|
|
['Ods', Writer\Ods::class],
|
|
|
|
['Csv', Writer\Csv::class],
|
|
|
|
['Html', Writer\Html::class],
|
|
|
|
['Mpdf', Writer\Pdf\Mpdf::class],
|
|
|
|
['Tcpdf', Writer\Pdf\Tcpdf::class],
|
|
|
|
['Dompdf', Writer\Pdf\Dompdf::class],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterWriter()
|
|
|
|
{
|
|
|
|
IOFactory::registerWriter('Pdf', Writer\Pdf\Mpdf::class);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$actual = IOFactory::createWriter($spreadsheet, 'Pdf');
|
|
|
|
self::assertInstanceOf(Writer\Pdf\Mpdf::class, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerCreateReader
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testCreateReader($name, $expected)
|
|
|
|
{
|
|
|
|
$actual = IOFactory::createReader($name);
|
|
|
|
self::assertInstanceOf($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerCreateReader()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['Xls', Reader\Xls::class],
|
|
|
|
['Xlsx', Reader\Xlsx::class],
|
|
|
|
['Xml', Reader\Xml::class],
|
|
|
|
['Ods', Reader\Ods::class],
|
|
|
|
['Gnumeric', Reader\Gnumeric::class],
|
|
|
|
['Csv', Reader\Csv::class],
|
|
|
|
['Slk', Reader\Slk::class],
|
|
|
|
['Html', Reader\Html::class],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterReader()
|
|
|
|
{
|
|
|
|
IOFactory::registerReader('Custom', Reader\Html::class);
|
|
|
|
$actual = IOFactory::createReader('Custom');
|
|
|
|
self::assertInstanceOf(Reader\Html::class, $actual);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerIdentify
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
2017-10-21 16:54:14 +00:00
|
|
|
* @param string $file
|
|
|
|
* @param string $expectedName
|
|
|
|
* @param string $expectedClass
|
2016-12-08 15:15:22 +00:00
|
|
|
*/
|
2017-10-21 16:54:14 +00:00
|
|
|
public function testIdentify($file, $expectedName, $expectedClass)
|
2016-12-08 15:15:22 +00:00
|
|
|
{
|
|
|
|
$actual = IOFactory::identify($file);
|
2017-10-21 16:54:14 +00:00
|
|
|
self::assertSame($expectedName, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerIdentify
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $expectedName
|
|
|
|
* @param string $expectedClass
|
|
|
|
*/
|
|
|
|
public function testCreateReaderForFile($file, $expectedName, $expectedClass)
|
|
|
|
{
|
|
|
|
$actual = IOFactory::createReaderForFile($file);
|
|
|
|
self::assertInstanceOf($expectedClass, $actual);
|
2016-12-08 15:15:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:09:45 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerIdentify
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $expectedName
|
|
|
|
* @param string $expectedClass
|
|
|
|
*/
|
|
|
|
public function testLoad($file, $expectedName, $expectedClass)
|
|
|
|
{
|
|
|
|
$actual = IOFactory::load($file);
|
|
|
|
self::assertInstanceOf(Spreadsheet::class, $actual);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
public function providerIdentify()
|
|
|
|
{
|
|
|
|
return [
|
2017-10-21 16:54:14 +00:00
|
|
|
['../samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
|
|
|
|
['../samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
|
|
|
|
['../samples/templates/30template.xls', 'Xls', Reader\Xls::class],
|
|
|
|
['../samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
|
|
|
|
['../samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
|
|
|
|
['../samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
|
|
|
|
['../samples/templates/46readHtml.html', 'Html', Reader\Html::class],
|
2016-12-08 15:15:22 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIdentifyNonExistingFileThrowException()
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
IOFactory::identify('/non/existing/file');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIdentifyExistingDirectoryThrowExceptions()
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
IOFactory::identify('.');
|
|
|
|
}
|
2017-10-21 16:54:14 +00:00
|
|
|
|
|
|
|
public function testRegisterInvalidWriter()
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
|
|
|
|
|
2017-10-21 16:54:14 +00:00
|
|
|
IOFactory::registerWriter('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterInvalidReader()
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
|
|
|
2017-10-21 16:54:14 +00:00
|
|
|
IOFactory::registerReader('foo', 'bar');
|
|
|
|
}
|
2016-12-08 15:15:22 +00:00
|
|
|
}
|