Yaffs site version 1.1
[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->assertContains('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     public function testLoadFileWithInternalErrorsEnabled()
66     {
67         $internalErrors = libxml_use_internal_errors(true);
68
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());
72
73         libxml_clear_errors();
74         libxml_use_internal_errors($internalErrors);
75     }
76
77     /**
78      * @dataProvider getDataForConvertDomToArray
79      */
80     public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
81     {
82         $dom = new \DOMDocument();
83         $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
84
85         $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
86     }
87
88     public function getDataForConvertDomToArray()
89     {
90         return array(
91             array(null, ''),
92             array('bar', 'bar'),
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),
108         );
109     }
110
111     /**
112      * @dataProvider getDataForPhpize
113      */
114     public function testPhpize($expected, $value)
115     {
116         $this->assertSame($expected, XmlUtils::phpize($value));
117     }
118
119     public function getDataForPhpize()
120     {
121         return array(
122             array('', ''),
123             array(null, 'null'),
124             array(true, 'true'),
125             array(false, 'false'),
126             array(null, 'Null'),
127             array(true, 'True'),
128             array(false, 'False'),
129             array(0, '0'),
130             array(1, '1'),
131             array(-1, '-1'),
132             array(0777, '0777'),
133             array(255, '0xFF'),
134             array(100.0, '1e2'),
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'),
146             array('foo', 'foo'),
147             array(6, '0b0110'),
148         );
149     }
150
151     public function testLoadEmptyXmlFile()
152     {
153         $file = __DIR__.'/../Fixtures/foo.xml';
154
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));
158         } else {
159             $this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
160         }
161
162         XmlUtils::loadFile($file);
163     }
164
165     // test for issue https://github.com/symfony/symfony/issues/9731
166     public function testLoadWrongEmptyXMLWithErrorHandler()
167     {
168         $originalDisableEntities = libxml_disable_entity_loader(false);
169         $errorReporting = error_reporting(-1);
170
171         set_error_handler(function ($errno, $errstr) {
172             throw new \Exception($errstr, $errno);
173         });
174
175         $file = __DIR__.'/../Fixtures/foo.xml';
176         try {
177             try {
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());
182             }
183         } catch (\Exception $e) {
184             restore_error_handler();
185             error_reporting($errorReporting);
186
187             throw $e;
188         }
189
190         restore_error_handler();
191         error_reporting($errorReporting);
192
193         $disableEntities = libxml_disable_entity_loader(true);
194         libxml_disable_entity_loader($disableEntities);
195
196         libxml_disable_entity_loader($originalDisableEntities);
197
198         $this->assertFalse($disableEntities);
199
200         // should not throw an exception
201         XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
202     }
203 }
204
205 interface Validator
206 {
207     public function validate();
208 }