2016-12-08 15:15:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests;
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
use InvalidArgumentException;
|
2016-12-08 15:15:22 +00:00
|
|
|
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
|
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testCreateWriter($name, $expected): void
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
|
|
|
$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
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
|
|
|
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
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
|
|
|
$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
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testIdentify($file, $expectedName, $expectedClass): void
|
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
|
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testCreateReaderForFile($file, $expectedName, $expectedClass): void
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
public function providerIdentify()
|
|
|
|
{
|
|
|
|
return [
|
2020-05-17 09:35:55 +00:00
|
|
|
['samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
|
|
|
|
['samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
|
2020-06-19 18:34:02 +00:00
|
|
|
['samples/templates/old.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
|
2020-05-17 09:35:55 +00:00
|
|
|
['samples/templates/30template.xls', 'Xls', Reader\Xls::class],
|
|
|
|
['samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
|
|
|
|
['samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
|
Improving Coverage for Excel2003 XML Reader (#1557)
* Improving Coverage for Excel2003 XML Reader
Reader/Xml is now 100% covered.
File templates/Excel2003XMLTest.xml, used in some tests, is *not*
readable by a current version of Excel. I have substituted a new file
excel2003.xml to be used in its place. I have not deleted the original
in case someone in future (possibly me) wants to see what it needs to
make it usable.
There are minimal code changes.
- Unused protected functions pixel2WidthUnits and widthUnits2Pixel
are deleted.
- One regex looking to convert hex characters is changed from a-z to a-f,
and made case insensitive.
- No calculation performed for "error" cell (previously calculation
was attempted and threw exception).
- Empty relative row/cell is now handled correctly.
- Style applied to empty cell when appropriate.
- Support added for textRotation.
- Support added for border styles.
- Support added for diagonal borders.
- Support added for superscript and subscript.
- Support added for fill patterns.
In theory, encodings other than UTF-8 were supported.
In fact, I was unable to get SecurityScanner to pass *any* xml which is
not UTF-8. Eliminating the assumption that strings might not be UTF-8
allowed much of the code to be greatly simplified.
After that, I added some code that would permit the use of
some ASCII-compatible encodings (there is a test of ISO-8859-1).
It would be more difficult to handle other encodings (such as UTF-16).
I am not convinced that even the ISO-8859 effort is worth it,
but am willing to investigate either expanding or eliminating
non-UTF8 support.
I added a number of tests, creating an Xml directory, and moving
XmlTest to that directory.
Pull Request had problems reading old invalid sample in the code
coverage phase, not in any of the other test phases, and not in
the code coverage phase on my local machine.
As it turns out, aside from being invalid, the sample
is much larger than any of the other samples. Tests have been
adjusted accordingly.
* Smaller Test File
Should eliminate need to avoid test during xml coverage.
* Break Up Style Test into Multiple Tests
Per suggestion from Mark Baker.
* Integrate AddressHelper Change
The introduction of AddressHelper introduced a conflict which needed to
be resolved. I wanted to test it locally before resolving. This required
me to add (unchanged) AddressHelper to my local copy. I hope this is
an okay manner of resolving the conflict.
* Weird Travis Error
XmlOddTest works just fine on my local machine, but Travis failed it.
Even worse, the lines which Travis flags don't even make any sense
(one was the empty line between two methods!).
This test is not essential to the rest of the change. I am removing
it from the package, and will attempt to re-add it when I have a chance
to sync up my fork with the main project.
2020-10-11 11:26:56 +00:00
|
|
|
['samples/templates/excel2003.xml', 'Xml', Reader\Xml::class],
|
|
|
|
// Following not readable by Excel.
|
|
|
|
//['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
|
2020-05-17 09:35:55 +00:00
|
|
|
['samples/templates/46readHtml.html', 'Html', Reader\Html::class],
|
2016-12-08 15:15:22 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testIdentifyNonExistingFileThrowException(): void
|
2016-12-08 15:15:22 +00:00
|
|
|
{
|
2020-05-18 04:49:57 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-01-28 06:59:38 +00:00
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
IOFactory::identify('/non/existing/file');
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testIdentifyExistingDirectoryThrowExceptions(): void
|
2016-12-08 15:15:22 +00:00
|
|
|
{
|
2020-05-18 04:49:57 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-01-28 06:59:38 +00:00
|
|
|
|
2016-12-08 15:15:22 +00:00
|
|
|
IOFactory::identify('.');
|
|
|
|
}
|
2017-10-21 16:54:14 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testRegisterInvalidWriter(): void
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
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');
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testRegisterInvalidReader(): void
|
2017-10-21 16:54:14 +00:00
|
|
|
{
|
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');
|
|
|
|
}
|
2020-11-27 14:50:01 +00:00
|
|
|
|
|
|
|
public function testCreateInvalidWriter(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
IOFactory::createWriter($spreadsheet, 'bad');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateInvalidReader(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
|
|
IOFactory::createReader('bad');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateReaderUnknownExtension(): void
|
|
|
|
{
|
|
|
|
$filename = 'samples/Reader/sampleData/example1.tsv';
|
|
|
|
$reader = IOFactory::createReaderForFile($filename);
|
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateReaderCsvExtension(): void
|
|
|
|
{
|
|
|
|
$filename = 'samples/Reader/sampleData/example1.csv';
|
|
|
|
$reader = IOFactory::createReaderForFile($filename);
|
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateReaderNoExtension(): void
|
|
|
|
{
|
|
|
|
$filename = 'samples/Reader/sampleData/example1xls';
|
|
|
|
$reader = IOFactory::createReaderForFile($filename);
|
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Xls', get_class($reader));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateReaderNotSpreadsheet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
|
|
$filename = __FILE__;
|
2020-11-27 15:16:23 +00:00
|
|
|
IOFactory::createReaderForFile($filename);
|
2020-11-27 14:50:01 +00:00
|
|
|
}
|
2016-12-08 15:15:22 +00:00
|
|
|
}
|