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->assertContains('is not valid', $e->getMessage());
61 $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate')));
62 $this->assertSame(array(), libxml_get_errors());
65 public function testLoadFileWithInternalErrorsEnabled()
67 $internalErrors = libxml_use_internal_errors(true);
69 $this->assertSame(array(), libxml_get_errors());
70 $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/invalid_schema.xml'));
71 $this->assertSame(array(), libxml_get_errors());
73 libxml_clear_errors();
74 libxml_use_internal_errors($internalErrors);
78 * @dataProvider getDataForConvertDomToArray
80 public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
82 $dom = new \DOMDocument();
83 $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
85 $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
88 public function getDataForConvertDomToArray()
93 array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
94 array(array('foo' => null), '<foo />'),
95 array(array('foo' => 'bar'), '<foo>bar</foo>'),
96 array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
97 array(array('foo' => array('foo' => 0)), '<foo><foo>0</foo></foo>'),
98 array(array('foo' => array('foo' => 'bar')), '<foo><foo>bar</foo></foo>'),
99 array(array('foo' => array('foo' => 'bar', 'value' => 'text')), '<foo foo="bar">text</foo>'),
100 array(array('foo' => array('attr' => 'bar', 'foo' => 'text')), '<foo attr="bar"><foo>text</foo></foo>'),
101 array(array('foo' => array('bar', 'text')), '<foo>bar</foo><foo>text</foo>'),
102 array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
103 array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
104 array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
105 array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
106 array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
107 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),
112 * @dataProvider getDataForPhpize
114 public function testPhpize($expected, $value)
116 $this->assertSame($expected, XmlUtils::phpize($value));
119 public function getDataForPhpize()
125 array(false, 'false'),
128 array(false, 'False'),
135 array(-120.0, '-1.2E2'),
136 array(-10100.1, '-10100.1'),
137 array('-10,100.1', '-10,100.1'),
138 array('1234 5678 9101 1121 3141', '1234 5678 9101 1121 3141'),
139 array('1,2,3,4', '1,2,3,4'),
140 array('11,22,33,44', '11,22,33,44'),
141 array('11,222,333,4', '11,222,333,4'),
142 array('1,222,333,444', '1,222,333,444'),
143 array('11,222,333,444', '11,222,333,444'),
144 array('111,222,333,444', '111,222,333,444'),
145 array('1111,2222,3333,4444,5555', '1111,2222,3333,4444,5555'),
151 public function testLoadEmptyXmlFile()
153 $file = __DIR__.'/../Fixtures/foo.xml';
155 if (method_exists($this, 'expectException')) {
156 $this->expectException('InvalidArgumentException');
157 $this->expectExceptionMessage(sprintf('File %s does not contain valid XML, it is empty.', $file));
159 $this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
162 XmlUtils::loadFile($file);
165 // test for issue https://github.com/symfony/symfony/issues/9731
166 public function testLoadWrongEmptyXMLWithErrorHandler()
168 $originalDisableEntities = libxml_disable_entity_loader(false);
169 $errorReporting = error_reporting(-1);
171 set_error_handler(function ($errno, $errstr) {
172 throw new \Exception($errstr, $errno);
175 $file = __DIR__.'/../Fixtures/foo.xml';
178 XmlUtils::loadFile($file);
179 $this->fail('An exception should have been raised');
180 } catch (\InvalidArgumentException $e) {
181 $this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
183 } catch (\Exception $e) {
184 restore_error_handler();
185 error_reporting($errorReporting);
190 restore_error_handler();
191 error_reporting($errorReporting);
193 $disableEntities = libxml_disable_entity_loader(true);
194 libxml_disable_entity_loader($disableEntities);
196 libxml_disable_entity_loader($originalDisableEntities);
198 $this->assertFalse($disableEntities);
200 // should not throw an exception
201 XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
207 public function validate();