From fab61effafe71a08cfa25f60bd350a217253f68e Mon Sep 17 00:00:00 2001 From: Andrej Hudec Date: Thu, 11 Sep 2014 22:00:09 +0200 Subject: [PATCH] [Translation] [Config] Clear libxml errors after parsing XML file --- .../Component/Config/Tests/Util/XmlUtilsTest.php | 10 ++++++++++ src/Symfony/Component/Config/Util/XmlUtils.php | 7 ++++--- .../Translation/Loader/XliffFileLoader.php | 3 ++- .../Tests/Loader/XliffFileLoaderTest.php | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php index d10863a922..900e6b0f75 100644 --- a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php +++ b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php @@ -58,6 +58,16 @@ class XmlUtilsTest extends \PHPUnit_Framework_TestCase } $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate'))); + $this->assertSame(array(), libxml_get_errors()); + } + + public function testLoadFileWithInternalErrorsEnabled() + { + libxml_use_internal_errors(true); + + $this->assertSame(array(), libxml_get_errors()); + $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/invalid_schema.xml')); + $this->assertSame(array(), libxml_get_errors()); } /** diff --git a/src/Symfony/Component/Config/Util/XmlUtils.php b/src/Symfony/Component/Config/Util/XmlUtils.php index b120fdf188..0123417176 100644 --- a/src/Symfony/Component/Config/Util/XmlUtils.php +++ b/src/Symfony/Component/Config/Util/XmlUtils.php @@ -31,7 +31,7 @@ class XmlUtils /** * Loads an XML file. * - * @param string $file An XML file path + * @param string $file An XML file path * @param string|callable $schemaOrCallable An XSD schema file path or callable * * @return \DOMDocument @@ -95,10 +95,11 @@ class XmlUtils } throw new \InvalidArgumentException(implode("\n", $messages), 0, $e); } - - libxml_use_internal_errors($internalErrors); } + libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); + return $dom; } diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index a86b02c2ee..c9fa2a95b5 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -116,6 +116,7 @@ class XliffFileLoader implements LoaderInterface $dom->normalizeDocument(); + libxml_clear_errors(); libxml_use_internal_errors($internalErrors); return array(simplexml_import_dom($dom), strtoupper($dom->encoding)); @@ -124,7 +125,7 @@ class XliffFileLoader implements LoaderInterface /** * Returns the XML errors of the internal XML parser * - * @param bool $internalErrors + * @param bool $internalErrors * * @return array An array of errors */ diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index 020a8721f1..50865c2452 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php @@ -31,6 +31,22 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals('en', $catalogue->getLocale()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); + $this->assertSame(array(), libxml_get_errors()); + } + + public function testLoadWithInternalErrorsEnabled() + { + libxml_use_internal_errors(true); + + $this->assertSame(array(), libxml_get_errors()); + + $loader = new XliffFileLoader(); + $resource = __DIR__.'/../fixtures/resources.xlf'; + $catalogue = $loader->load($resource, 'en', 'domain1'); + + $this->assertEquals('en', $catalogue->getLocale()); + $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); + $this->assertSame(array(), libxml_get_errors()); } public function testLoadWithResname()