4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Util;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Util\XmlUtils;
17 class XmlUtilsTest extends TestCase
19 public function testLoadFile()
21 $fixtures = __DIR__.'/../Fixtures/Util/';
24 XmlUtils::loadFile($fixtures.'invalid.xml');
26 } catch (\InvalidArgumentException $e) {
27 $this->assertContains('ERROR 77', $e->getMessage());
31 XmlUtils::loadFile($fixtures.'document_type.xml');
33 } catch (\InvalidArgumentException $e) {
34 $this->assertContains('Document types are not allowed', $e->getMessage());
38 XmlUtils::loadFile($fixtures.'invalid_schema.xml', $fixtures.'schema.xsd');
40 } catch (\InvalidArgumentException $e) {
41 $this->assertContains('ERROR 1845', $e->getMessage());
45 XmlUtils::loadFile($fixtures.'invalid_schema.xml', 'invalid_callback_or_file');
47 } catch (\InvalidArgumentException $e) {
48 $this->assertContains('XSD file or callable', $e->getMessage());
51 $mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
52 $mock->expects($this->exactly(2))->method('validate')->will($this->onConsecutiveCalls(false, true));
55 XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate'));
57 } catch (\InvalidArgumentException $e) {
58 $this->assertRegExp('/The XML file ".+" is not valid\./', $e->getMessage());
61 $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate')));
62 $this->assertSame(array(), libxml_get_errors());
66 * @expectedException \Symfony\Component\Config\Util\Exception\InvalidXmlException
67 * @expectedExceptionMessage The XML is not valid
69 public function testParseWithInvalidValidatorCallable()
71 $fixtures = __DIR__.'/../Fixtures/Util/';
73 $mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
74 $mock->expects($this->once())->method('validate')->willReturn(false);
76 XmlUtils::parse(file_get_contents($fixtures.'valid.xml'), array($mock, 'validate'));
79 public function testLoadFileWithInternalErrorsEnabled()
81 $internalErrors = libxml_use_internal_errors(true);
83 $this->assertSame(array(), libxml_get_errors());
84 $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/invalid_schema.xml'));
85 $this->assertSame(array(), libxml_get_errors());
87 libxml_clear_errors();
88 libxml_use_internal_errors($internalErrors);
92 * @dataProvider getDataForConvertDomToArray
94 public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
96 $dom = new \DOMDocument();
97 $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
99 $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
102 public function getDataForConvertDomToArray()
107 array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
108 array(array('foo' => null), '<foo />'),
109 array(array('foo' => 'bar'), '<foo>bar</foo>'),
110 array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
111 array(array('foo' => array('foo' => 0)), '<foo><foo>0</foo></foo>'),
112 array(array('foo' => array('foo' => 'bar')), '<foo><foo>bar</foo></foo>'),
113 array(array('foo' => array('foo' => 'bar', 'value' => 'text')), '<foo foo="bar">text</foo>'),
114 array(array('foo' => array('attr' => 'bar', 'foo' => 'text')), '<foo attr="bar"><foo>text</foo></foo>'),
115 array(array('foo' => array('bar', 'text')), '<foo>bar</foo><foo>text</foo>'),
116 array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
117 array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
118 array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
119 array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
120 array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
121 array(array('attr' => 1, 'b' => 'hello'), '<foo:a xmlns:foo="http://www.example.org/foo" xmlns:h="http://www.example.org/bar" attr="1" h:bar="bar"><foo:b>hello</foo:b><h:c>2</h:c></foo:a>', true),
126 * @dataProvider getDataForPhpize
128 public function testPhpize($expected, $value)
130 $this->assertSame($expected, XmlUtils::phpize($value));
133 public function getDataForPhpize()
139 array(false, 'false'),
142 array(false, 'False'),
149 array(-120.0, '-1.2E2'),
150 array(-10100.1, '-10100.1'),
151 array('-10,100.1', '-10,100.1'),
152 array('1234 5678 9101 1121 3141', '1234 5678 9101 1121 3141'),
153 array('1,2,3,4', '1,2,3,4'),
154 array('11,22,33,44', '11,22,33,44'),
155 array('11,222,333,4', '11,222,333,4'),
156 array('1,222,333,444', '1,222,333,444'),
157 array('11,222,333,444', '11,222,333,444'),
158 array('111,222,333,444', '111,222,333,444'),
159 array('1111,2222,3333,4444,5555', '1111,2222,3333,4444,5555'),
165 public function testLoadEmptyXmlFile()
167 $file = __DIR__.'/../Fixtures/foo.xml';
169 if (method_exists($this, 'expectException')) {
170 $this->expectException('InvalidArgumentException');
171 $this->expectExceptionMessage(sprintf('File %s does not contain valid XML, it is empty.', $file));
173 $this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
176 XmlUtils::loadFile($file);
179 // test for issue https://github.com/symfony/symfony/issues/9731
180 public function testLoadWrongEmptyXMLWithErrorHandler()
182 $originalDisableEntities = libxml_disable_entity_loader(false);
183 $errorReporting = error_reporting(-1);
185 set_error_handler(function ($errno, $errstr) {
186 throw new \Exception($errstr, $errno);
189 $file = __DIR__.'/../Fixtures/foo.xml';
192 XmlUtils::loadFile($file);
193 $this->fail('An exception should have been raised');
194 } catch (\InvalidArgumentException $e) {
195 $this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
198 restore_error_handler();
199 error_reporting($errorReporting);
202 $disableEntities = libxml_disable_entity_loader(true);
203 libxml_disable_entity_loader($disableEntities);
205 libxml_disable_entity_loader($originalDisableEntities);
207 $this->assertFalse($disableEntities);
209 // should not throw an exception
210 XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
216 public function validate();