Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…endframework#6730-xmlreader-fromfile-and-fromstring-should-be-closed' into develop

Close zendframework/zendframework#6761
Close zendframework/zendframework#6730
Forward port zendframework/zendframework#6761
Forward port zendframework/zendframework#6730
  • Loading branch information
Ocramius committed Dec 31, 2014
4 parents 7946ace + 1856d09 + 306b161 + 138488a commit 51f8ace
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function fromFile($filename)
$filename
));
}

$this->reader = new XMLReader();
$this->reader->open($filename, null, LIBXML_XINCLUDE);

Expand All @@ -76,6 +75,7 @@ function ($error, $message = '') use ($filename) {
);
$return = $this->process();
restore_error_handler();
$this->reader->close();

return $return;
}
Expand Down Expand Up @@ -110,6 +110,7 @@ function ($error, $message = '') {
);
$return = $this->process();
restore_error_handler();
$this->reader->close();

return $return;
}
Expand Down
85 changes: 84 additions & 1 deletion test/Reader/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace ZendTest\Config\Reader;

use ReflectionProperty;
use XMLReader;
use Zend\Config\Reader\Xml;

/**
* @group Zend_Config
*
* @covers \Zend\Config\Reader\Xml
*/
class XmlTest extends AbstractReaderTestCase
{
Expand Down Expand Up @@ -72,7 +76,7 @@ public function testInvalidString()
ECS;
$this->setExpectedException('Zend\Config\Exception\RuntimeException');
$arrayXml = $this->reader->fromString($xml);
$this->reader->fromString($xml);
}

public function testZF300_MultipleKeysOfTheSameName()
Expand Down Expand Up @@ -121,4 +125,83 @@ public function testElementWithBothAttributesAndAStringValueIsProcessedCorrectly
$this->assertArrayHasKey('_', $arrayXml['one'][1]);
$this->assertEquals('bazbat', $arrayXml['one'][1]['_']);
}

/**
* @group 6761
* @group 6730
*/
public function testReadNonExistingFilesWillFailWithException()
{
$configReader = new Xml();

$this->setExpectedException('Zend\Config\Exception\RuntimeException');

$configReader->fromFile(sys_get_temp_dir() . '/path/that/does/not/exist');
}

/**
* @group 6761
* @group 6730
*/
public function testCloseWhenCallFromFileReaderGetInvalid()
{
$configReader = new Xml();

$configReader->fromFile($this->getTestAssetPath('attributes'));

$xmlReader = $this->getInternalXmlReader($configReader);

$this->setExpectedException('PHPUnit_Framework_Error_Warning');

// following operation should fail because the internal reader is closed (and expected to be closed)
$xmlReader->setParserProperty(XMLReader::VALIDATE, true);
}

/**
* @group 6761
* @group 6730
*/
public function testCloseWhenCallFromStringReaderGetInvalid()
{
$xml = <<<ECS
<?xml version="1.0" encoding="UTF-8"?>
<zend-config>
<test>foo</test>
<bar>baz</bar>
<bar>foo</bar>
</zend-config>
ECS;

$configReader = new Xml();

$configReader->fromString($xml);

$xmlReader = $this->getInternalXmlReader($configReader);

$this->setExpectedException('PHPUnit_Framework_Error_Warning');

// following operation should fail because the internal reader is closed (and expected to be closed)
$xmlReader->setParserProperty(XMLReader::VALIDATE, true);
}

/**
* Reads the internal XML reader from a given Xml config reader
*
* @param Xml $xml
*
* @return XMLReader
*/
private function getInternalXmlReader(Xml $xml)
{
$reflectionReader = new ReflectionProperty('Zend\Config\Reader\Xml', 'reader');

$reflectionReader->setAccessible(true);

$xmlReader = $reflectionReader->getValue($xml);

$this->assertInstanceOf('XMLReader', $xmlReader);

return $xmlReader;
}
}

0 comments on commit 51f8ace

Please sign in to comment.