diff --git a/src/Reader/Xml.php b/src/Reader/Xml.php index 348354f..4961c76 100644 --- a/src/Reader/Xml.php +++ b/src/Reader/Xml.php @@ -59,7 +59,6 @@ public function fromFile($filename) $filename )); } - $this->reader = new XMLReader(); $this->reader->open($filename, null, LIBXML_XINCLUDE); @@ -76,6 +75,7 @@ function ($error, $message = '', $file = '', $line = 0) use ($filename) { ); $return = $this->process(); restore_error_handler(); + $this->reader->close(); return $return; } @@ -110,6 +110,7 @@ function ($error, $message = '', $file = '', $line = 0) { ); $return = $this->process(); restore_error_handler(); + $this->reader->close(); return $return; } diff --git a/test/Reader/XmlTest.php b/test/Reader/XmlTest.php index ee3ffe9..6a945e7 100644 --- a/test/Reader/XmlTest.php +++ b/test/Reader/XmlTest.php @@ -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 { @@ -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() @@ -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 = << + + foo + baz + foo + + +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; + } }