Add wrappers around XMLReader and SimpleXMLElement to improve error handling
This commit is contained in:
1 parent
be3932af18
commit
1ba10ed2b0
16 files changed
+789
-155
No files matched your search
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\Wrapper;
|
||||
|
||||
use Box\Spout\TestUsingResource;
|
||||
|
||||
/**
|
||||
* Class SimpleXMLElementTest
|
||||
*
|
||||
* @package Box\Spout\Reader\Wrapper
|
||||
*/
|
||||
class SimpleXMLElementTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Reader\Exception\XMLProcessingException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testConstructShouldThrowExceptionIfInvalidData()
|
||||
{
|
||||
$invalidXML = '<invalid><xml></invalid>';
|
||||
new SimpleXMLElement($invalidXML);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestGetAttribute()
|
||||
{
|
||||
$xmlWithoutNamespace = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet foo="bar" type="test" />
|
||||
XML;
|
||||
|
||||
$xmlWithHalfNamespace = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet
|
||||
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
||||
foo="bar" r:type="test" />
|
||||
XML;
|
||||
|
||||
$xmlWithFullNamespace = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet
|
||||
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
||||
r:foo="bar" r:type="test" />
|
||||
XML;
|
||||
|
||||
return [
|
||||
[$xmlWithoutNamespace, null, ['foo' => 'bar', 'type' => 'test']],
|
||||
[$xmlWithHalfNamespace, null, ['foo' => 'bar', 'type' => null]],
|
||||
[$xmlWithFullNamespace, null, ['foo' => null, 'type' => null]],
|
||||
[$xmlWithoutNamespace, 'r', ['foo' => null, 'type' => null]],
|
||||
[$xmlWithHalfNamespace, 'r', ['foo' => null, 'type' => 'test']],
|
||||
[$xmlWithFullNamespace, 'r', ['foo' => 'bar', 'type' => 'test']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestGetAttribute
|
||||
*
|
||||
* @param string $xml
|
||||
* @param string|null $namespace
|
||||
* @param array $expectedAttributes
|
||||
* @return void
|
||||
*/
|
||||
public function testGetAttribute($xml, $namespace, $expectedAttributes)
|
||||
{
|
||||
$element = new SimpleXMLElement($xml);
|
||||
|
||||
foreach ($expectedAttributes as $name => $expectedValue) {
|
||||
$value = $element->getAttribute($name, $namespace);
|
||||
$this->assertEquals($expectedValue, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testXPath()
|
||||
{
|
||||
$xml = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet>
|
||||
<sheetData>
|
||||
<row r="1">
|
||||
<c r="A1"><v>0</v></c>
|
||||
<c r="A2"><v>1</v></c>
|
||||
</row>
|
||||
</sheetData>
|
||||
</worksheet>
|
||||
XML;
|
||||
|
||||
$element = new SimpleXMLElement($xml);
|
||||
$matchedElements = $element->xpath('//c');
|
||||
|
||||
$this->assertEquals(2, count($matchedElements));
|
||||
$this->assertTrue($matchedElements[0] instanceof SimpleXMLElement, 'The SimpleXMLElement should be wrapped');
|
||||
$this->assertEquals('A2', $matchedElements[1]->getAttribute('r'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveNodeMatchingXPath()
|
||||
{
|
||||
$xml = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet>
|
||||
<sheetData>
|
||||
<row r="1">
|
||||
<c r="A1"><v>0</v></c>
|
||||
<c r="A2"><v>1</v></c>
|
||||
</row>
|
||||
</sheetData>
|
||||
</worksheet>
|
||||
XML;
|
||||
|
||||
$element = new SimpleXMLElement($xml);
|
||||
$this->assertNotNull($element->getFirstChildByTagName('sheetData'));
|
||||
|
||||
$element->removeNodesMatchingXPath('//sheetData');
|
||||
$this->assertNull($element->getFirstChildByTagName('sheetData'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\Wrapper;
|
||||
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Reader\Exception\XMLProcessingException;
|
||||
|
||||
/**
|
||||
* Class XMLReaderTest
|
||||
*
|
||||
* @package Box\Spout\Reader\Wrapper
|
||||
*/
|
||||
class XMLReaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testOpenShouldFailIfFileInsideZipDoesNotExist()
|
||||
{
|
||||
$resourcePath = $this->getResourcePath('one_sheet_with_inline_strings.xlsx');
|
||||
$nonExistingXMLFilePath = 'zip://' . $resourcePath . '#path/to/fake/file.xml';
|
||||
|
||||
$xmlReader = new XMLReader();
|
||||
|
||||
// using "@" to prevent errors/warning to be displayed
|
||||
$wasOpenSuccessful = @$xmlReader->open($nonExistingXMLFilePath);
|
||||
|
||||
$this->assertTrue($wasOpenSuccessful === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing a HHVM bug: https://github.com/facebook/hhvm/issues/5779
|
||||
* The associated code in XMLReader::open() can be removed when the issue is fixed (and this test starts failing).
|
||||
* @see XMLReader::open()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHHVMStillDoesNotComplainWhenCallingOpenWithFileInsideZipNotExisting()
|
||||
{
|
||||
// Test should only be run on HHVM
|
||||
if ($this->isRunningHHVM()) {
|
||||
$resourcePath = $this->getResourcePath('one_sheet_with_inline_strings.xlsx');
|
||||
$nonExistingXMLFilePath = 'zip://' . $resourcePath . '#path/to/fake/file.xml';
|
||||
|
||||
libxml_clear_errors();
|
||||
$initialUseInternalErrorsSetting = libxml_use_internal_errors(true);
|
||||
|
||||
// using the built-in XMLReader
|
||||
$xmlReader = new \XMLReader();
|
||||
$this->assertTrue($xmlReader->open($nonExistingXMLFilePath) !== false);
|
||||
$this->assertTrue(libxml_get_last_error() === false);
|
||||
|
||||
libxml_use_internal_errors($initialUseInternalErrorsSetting);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool TRUE if running on HHVM, FALSE otherwise
|
||||
*/
|
||||
private function isRunningHHVM()
|
||||
{
|
||||
return defined('HHVM_VERSION');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Reader\Exception\XMLProcessingException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldThrowExceptionOnError()
|
||||
{
|
||||
$resourcePath = $this->getResourcePath('one_sheet_with_invalid_xml_characters.xlsx');
|
||||
$sheetDataXMLFilePath = 'zip://' . $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
|
||||
$xmlReader = new XMLReader();
|
||||
if ($xmlReader->open($sheetDataXMLFilePath) === false) {
|
||||
$this->fail();
|
||||
}
|
||||
|
||||
// using "@" to prevent errors/warning to be displayed
|
||||
while (@$xmlReader->read()) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Reader\Exception\XMLProcessingException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNextShouldThrowExceptionOnError()
|
||||
{
|
||||
// The sharedStrings.xml file in "attack_billion_laughs.xlsx" contains
|
||||
// a doctype element that causes read errors
|
||||
$resourcePath = $this->getResourcePath('attack_billion_laughs.xlsx');
|
||||
$sheetDataXMLFilePath = 'zip://' . $resourcePath . '#xl/sharedStrings.xml';
|
||||
|
||||
$xmlReader = new XMLReader();
|
||||
if ($xmlReader->open($sheetDataXMLFilePath) !== false) {
|
||||
@$xmlReader->next('sst');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestIsZipStream()
|
||||
{
|
||||
return [
|
||||
['/absolute/path/to/file.xlsx', false],
|
||||
['relative/path/to/file.xlsx', false],
|
||||
['php://temp', false],
|
||||
['zip:///absolute/path/to/file.xlsx', true],
|
||||
['zip://relative/path/to/file.xlsx', true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestIsZipStream
|
||||
*
|
||||
* @param string $URI
|
||||
* @param bool $expectedResult
|
||||
* @return void
|
||||
*/
|
||||
public function testIsZipStream($URI, $expectedResult)
|
||||
{
|
||||
$xmlReader = new XMLReader();
|
||||
$isZipStream = \ReflectionHelper::callMethodOnObject($xmlReader, 'isZipStream', $URI);
|
||||
|
||||
$this->assertEquals($expectedResult, $isZipStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestFileExistsWithinZip()
|
||||
{
|
||||
return [
|
||||
['[Content_Types].xml', true],
|
||||
['xl/sharedStrings.xml', true],
|
||||
['xl/worksheets/sheet1.xml', true],
|
||||
['/invalid/file.xml', false],
|
||||
['another/invalid/file.xml', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestFileExistsWithinZip
|
||||
*
|
||||
* @param string $innerFilePath
|
||||
* @param bool $expectedResult
|
||||
* @return void
|
||||
*/
|
||||
public function testFileExistsWithinZip($innerFilePath, $expectedResult)
|
||||
{
|
||||
$resourcePath = $this->getResourcePath('one_sheet_with_inline_strings.xlsx');
|
||||
$zipStreamURI = 'zip://' . $resourcePath . '#' . $innerFilePath;
|
||||
|
||||
$xmlReader = new XMLReader();
|
||||
$isZipStream = \ReflectionHelper::callMethodOnObject($xmlReader, 'fileExistsWithinZip', $zipStreamURI);
|
||||
|
||||
$this->assertEquals($expectedResult, $isZipStream);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\ReaderFactory;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
||||
|
||||
/**
|
||||
* Class ReaderTest
|
||||
|
||||
Reference in new issue
Block a user