10b4a7a9b4dc7a581495b50502d581c54555d354
[yaffs-website] / vendor / symfony / config / Tests / Util / XmlUtilsTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Tests\Util;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Util\XmlUtils;
16
17 class XmlUtilsTest extends TestCase
18 {
19     public function testLoadFile()
20     {
21         $fixtures = __DIR__.'/../Fixtures/Util/';
22
23         try {
24             XmlUtils::loadFile($fixtures.'invalid.xml');
25             $this->fail();
26         } catch (\InvalidArgumentException $e) {
27             $this->assertContains('ERROR 77', $e->getMessage());
28         }
29
30         try {
31             XmlUtils::loadFile($fixtures.'document_type.xml');
32             $this->fail();
33         } catch (\InvalidArgumentException $e) {
34             $this->assertContains('Document types are not allowed', $e->getMessage());
35         }
36
37         try {
38             XmlUtils::loadFile($fixtures.'invalid_schema.xml', $fixtures.'schema.xsd');
39             $this->fail();
40         } catch (\InvalidArgumentException $e) {
41             $this->assertContains('ERROR 1845', $e->getMessage());
42         }
43
44         try {
45             XmlUtils::loadFile($fixtures.'invalid_schema.xml', 'invalid_callback_or_file');
46             $this->fail();
47         } catch (\InvalidArgumentException $e) {
48             $this->assertContains('XSD file or callable', $e->getMessage());
49         }
50
51         $mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
52         $mock->expects($this->exactly(2))->method('validate')->will($this->onConsecutiveCalls(false, true));
53
54         try {
55             XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate'));
56             $this->fail();
57         } catch (\InvalidArgumentException $e) {
58             $this->assertRegExp('/The XML file ".+" is not valid\./', $e->getMessage());
59         }
60
61         $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate')));
62         $this->assertSame(array(), libxml_get_errors());
63     }
64
65     /**
66      * @expectedException \Symfony\Component\Config\Util\Exception\InvalidXmlException
67      * @expectedExceptionMessage The XML is not valid
68      */
69     public function testParseWithInvalidValidatorCallable()
70     {
71         $fixtures = __DIR__.'/../Fixtures/Util/';
72
73         $mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
74         $mock->expects($this->once())->method('validate')->willReturn(false);
75
76         XmlUtils::parse(file_get_contents($fixtures.'valid.xml'), array($mock, 'validate'));
77     }
78
79     public function testLoadFileWithInternalErrorsEnabled()
80     {
81         $internalErrors = libxml_use_internal_errors(true);
82
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());
86
87         libxml_clear_errors();
88         libxml_use_internal_errors($internalErrors);
89     }
90
91     /**
92      * @dataProvider getDataForConvertDomToArray
93      */
94     public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
95     {
96         $dom = new \DOMDocument();
97         $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
98
99         $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
100     }
101
102     public function getDataForConvertDomToArray()
103     {
104         return array(
105             array(null, ''),
106             array('bar', 'bar'),
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),
122         );
123     }
124
125     /**
126      * @dataProvider getDataForPhpize
127      */
128     public function testPhpize($expected, $value)
129     {
130         $this->assertSame($expected, XmlUtils::phpize($value));
131     }
132
133     public function getDataForPhpize()
134     {
135         return array(
136             array('', ''),
137             array(null, 'null'),
138             array(true, 'true'),
139             array(false, 'false'),
140             array(null, 'Null'),
141             array(true, 'True'),
142             array(false, 'False'),
143             array(0, '0'),
144             array(1, '1'),
145             array(-1, '-1'),
146             array(0777, '0777'),
147             array(255, '0xFF'),
148             array(100.0, '1e2'),
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'),
160             array('foo', 'foo'),
161             array(6, '0b0110'),
162         );
163     }
164
165     public function testLoadEmptyXmlFile()
166     {
167         $file = __DIR__.'/../Fixtures/foo.xml';
168
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));
172         } else {
173             $this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
174         }
175
176         XmlUtils::loadFile($file);
177     }
178
179     // test for issue https://github.com/symfony/symfony/issues/9731
180     public function testLoadWrongEmptyXMLWithErrorHandler()
181     {
182         $originalDisableEntities = libxml_disable_entity_loader(false);
183         $errorReporting = error_reporting(-1);
184
185         set_error_handler(function ($errno, $errstr) {
186             throw new \Exception($errstr, $errno);
187         });
188
189         $file = __DIR__.'/../Fixtures/foo.xml';
190         try {
191             try {
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());
196             }
197         } finally {
198             restore_error_handler();
199             error_reporting($errorReporting);
200         }
201
202         $disableEntities = libxml_disable_entity_loader(true);
203         libxml_disable_entity_loader($disableEntities);
204
205         libxml_disable_entity_loader($originalDisableEntities);
206
207         $this->assertFalse($disableEntities);
208
209         // should not throw an exception
210         XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
211     }
212 }
213
214 interface Validator
215 {
216     public function validate();
217 }