2015-04-29 21:23:14 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
2015-04-29 21:23:14 +00:00
|
|
|
|
2017-09-18 08:03:32 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\BaseReader;
|
2017-09-20 05:20:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xml;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2018-01-14 07:59:34 +00:00
|
|
|
class XmlTest extends TestCase
|
2015-04-29 21:23:14 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider providerInvalidXML
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
|
|
|
* @param mixed $filename
|
2015-04-29 21:23:14 +00:00
|
|
|
*/
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testInvalidXML($filename)
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$reader = $this->getMockForAbstractClass(BaseReader::class);
|
2015-04-29 21:23:14 +00:00
|
|
|
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
|
2015-05-17 13:00:02 +00:00
|
|
|
$result = $reader->securityScanFile($filename);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-29 21:23:14 +00:00
|
|
|
|
|
|
|
public function providerInvalidXML()
|
|
|
|
{
|
|
|
|
$tests = [];
|
2018-01-14 07:59:34 +00:00
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestInvalidUTF*.xml') as $file) {
|
2017-09-20 05:20:12 +00:00
|
|
|
$tests[basename($file)] = [realpath($file)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerInvalidSimpleXML
|
|
|
|
*
|
|
|
|
* @param $filename
|
|
|
|
*/
|
|
|
|
public function testInvalidSimpleXML($filename)
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
|
|
|
2017-09-20 05:20:12 +00:00
|
|
|
$xmlReader = new Xml();
|
|
|
|
$xmlReader->trySimpleXMLLoadString($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerInvalidSimpleXML()
|
|
|
|
{
|
|
|
|
$tests = [];
|
2018-01-14 07:59:34 +00:00
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestInvalidSimpleXML*.xml') as $file) {
|
2016-10-23 18:16:36 +00:00
|
|
|
$tests[basename($file)] = [realpath($file)];
|
2015-04-29 21:23:14 +00:00
|
|
|
}
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-04-29 21:23:14 +00:00
|
|
|
return $tests;
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-29 21:23:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerValidXML
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
|
|
|
* @param mixed $filename
|
|
|
|
* @param mixed $expectedResult
|
2015-04-29 21:23:14 +00:00
|
|
|
*/
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testValidXML($filename, $expectedResult)
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$reader = $this->getMockForAbstractClass(BaseReader::class);
|
2015-05-17 13:00:02 +00:00
|
|
|
$result = $reader->securityScanFile($filename);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-29 21:23:14 +00:00
|
|
|
|
|
|
|
public function providerValidXML()
|
|
|
|
{
|
2016-08-16 15:33:57 +00:00
|
|
|
$tests = [];
|
2018-01-14 07:59:34 +00:00
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestValid*.xml') as $file) {
|
2016-10-23 18:16:36 +00:00
|
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)];
|
2015-04-29 21:23:14 +00:00
|
|
|
}
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-04-29 21:23:14 +00:00
|
|
|
return $tests;
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2017-09-18 08:03:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if it can read XML Hyperlink correctly.
|
|
|
|
*/
|
|
|
|
public function testReadHyperlinks()
|
|
|
|
{
|
2018-01-14 07:59:34 +00:00
|
|
|
$reader = new Xml();
|
|
|
|
$spreadsheet = $reader->load('../samples/templates/Excel2003XMLTest.xml');
|
2017-09-18 08:03:32 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
|
|
|
$hyperlink = $firstSheet->getCell('L1');
|
|
|
|
|
|
|
|
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
|
|
|
|
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
|
|
|
|
self::assertEquals('http://phpspreadsheet.readthedocs.io/', $hyperlink->getHyperlink()->getUrl());
|
|
|
|
}
|
2018-01-14 07:59:34 +00:00
|
|
|
|
|
|
|
public function testReadWithoutStyle()
|
|
|
|
{
|
|
|
|
$reader = new Xml();
|
|
|
|
$spreadsheet = $reader->load(__DIR__ . '/../../data/Reader/Xml/WithoutStyle.xml');
|
|
|
|
self::assertSame('Test String 1', $spreadsheet->getActiveSheet()->getCell('A1')->getValue());
|
|
|
|
}
|
2015-04-29 21:23:14 +00:00
|
|
|
}
|