PhpSpreadsheet/tests/PhpSpreadsheetTests/IOFactoryTest.php

163 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests;
2020-05-18 04:49:57 +00:00
use InvalidArgumentException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer;
use PHPUnit\Framework\TestCase;
class IOFactoryTest extends TestCase
{
/**
* @dataProvider providerCreateWriter
*
* @param string $name
* @param string $expected
*/
2020-05-18 04:49:57 +00:00
public function testCreateWriter($name, $expected): void
{
$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],
];
}
2020-05-18 04:49:57 +00:00
public function testRegisterWriter(): void
{
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
*/
2020-05-18 04:49:57 +00:00
public function testCreateReader($name, $expected): void
{
$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],
];
}
2020-05-18 04:49:57 +00:00
public function testRegisterReader(): void
{
IOFactory::registerReader('Custom', Reader\Html::class);
$actual = IOFactory::createReader('Custom');
self::assertInstanceOf(Reader\Html::class, $actual);
}
/**
* @dataProvider providerIdentify
2016-12-22 14:43:37 +00:00
*
* @param string $file
* @param string $expectedName
* @param string $expectedClass
*/
2020-05-18 04:49:57 +00:00
public function testIdentify($file, $expectedName, $expectedClass): void
{
$actual = IOFactory::identify($file);
self::assertSame($expectedName, $actual);
}
/**
* @dataProvider providerIdentify
*
* @param string $file
* @param string $expectedName
* @param string $expectedClass
*/
2020-05-18 04:49:57 +00:00
public function testCreateReaderForFile($file, $expectedName, $expectedClass): void
{
$actual = IOFactory::createReaderForFile($file);
self::assertInstanceOf($expectedClass, $actual);
}
2019-10-02 19:09:45 +00:00
/**
* @dataProvider providerIdentify
*
* @param string $file
* @param string $expectedName
* @param string $expectedClass
*/
2020-05-18 04:49:57 +00:00
public function testLoad($file, $expectedName, $expectedClass): void
2019-10-02 19:09:45 +00:00
{
$actual = IOFactory::load($file);
self::assertInstanceOf(Spreadsheet::class, $actual);
}
public function providerIdentify()
{
return [
['samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
['samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
['samples/templates/old.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],
];
}
2020-05-18 04:49:57 +00:00
public function testIdentifyNonExistingFileThrowException(): void
{
2020-05-18 04:49:57 +00:00
$this->expectException(InvalidArgumentException::class);
2018-01-28 06:59:38 +00:00
IOFactory::identify('/non/existing/file');
}
2020-05-18 04:49:57 +00:00
public function testIdentifyExistingDirectoryThrowExceptions(): void
{
2020-05-18 04:49:57 +00:00
$this->expectException(InvalidArgumentException::class);
2018-01-28 06:59:38 +00:00
IOFactory::identify('.');
}
2020-05-18 04:49:57 +00:00
public function testRegisterInvalidWriter(): void
{
2018-01-28 06:59:38 +00:00
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
IOFactory::registerWriter('foo', 'bar');
}
2020-05-18 04:49:57 +00:00
public function testRegisterInvalidReader(): void
{
2018-01-28 06:59:38 +00:00
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
IOFactory::registerReader('foo', 'bar');
}
}