PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/config/Tests/Util/XmlUtilsTest.php

https://gitlab.com/alexandresgv/siteentec
PHP | 195 lines | 151 code | 28 blank | 16 comment | 0 complexity | 1eaf30fd5b3653e926699b95223b3f07 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Tests\Loader;
  11. use Symfony\Component\Config\Util\XmlUtils;
  12. class XmlUtilsTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testLoadFile()
  15. {
  16. $fixtures = __DIR__.'/../Fixtures/Util/';
  17. try {
  18. XmlUtils::loadFile($fixtures.'invalid.xml');
  19. $this->fail();
  20. } catch (\InvalidArgumentException $e) {
  21. $this->assertContains('ERROR 77', $e->getMessage());
  22. }
  23. try {
  24. XmlUtils::loadFile($fixtures.'document_type.xml');
  25. $this->fail();
  26. } catch (\InvalidArgumentException $e) {
  27. $this->assertContains('Document types are not allowed', $e->getMessage());
  28. }
  29. try {
  30. XmlUtils::loadFile($fixtures.'invalid_schema.xml', $fixtures.'schema.xsd');
  31. $this->fail();
  32. } catch (\InvalidArgumentException $e) {
  33. $this->assertContains('ERROR 1845', $e->getMessage());
  34. }
  35. try {
  36. XmlUtils::loadFile($fixtures.'invalid_schema.xml', 'invalid_callback_or_file');
  37. $this->fail();
  38. } catch (\InvalidArgumentException $e) {
  39. $this->assertContains('XSD file or callable', $e->getMessage());
  40. }
  41. $mock = $this->getMock(__NAMESPACE__.'\Validator');
  42. $mock->expects($this->exactly(2))->method('validate')->will($this->onConsecutiveCalls(false, true));
  43. try {
  44. XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate'));
  45. $this->fail();
  46. } catch (\InvalidArgumentException $e) {
  47. $this->assertContains('is not valid', $e->getMessage());
  48. }
  49. $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate')));
  50. $this->assertSame(array(), libxml_get_errors());
  51. }
  52. public function testLoadFileWithInternalErrorsEnabled()
  53. {
  54. $internalErrors = libxml_use_internal_errors(true);
  55. $this->assertSame(array(), libxml_get_errors());
  56. $this->assertInstanceOf('DOMDocument', XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/invalid_schema.xml'));
  57. $this->assertSame(array(), libxml_get_errors());
  58. libxml_clear_errors();
  59. libxml_use_internal_errors($internalErrors);
  60. }
  61. /**
  62. * @dataProvider getDataForConvertDomToArray
  63. */
  64. public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
  65. {
  66. $dom = new \DOMDocument();
  67. $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
  68. $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
  69. }
  70. public function getDataForConvertDomToArray()
  71. {
  72. return array(
  73. array(null, ''),
  74. array('bar', 'bar'),
  75. array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
  76. array(array('foo' => null), '<foo />'),
  77. array(array('foo' => 'bar'), '<foo>bar</foo>'),
  78. array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
  79. array(array('foo' => array('foo' => 0)), '<foo><foo>0</foo></foo>'),
  80. array(array('foo' => array('foo' => 'bar')), '<foo><foo>bar</foo></foo>'),
  81. array(array('foo' => array('foo' => 'bar', 'value' => 'text')), '<foo foo="bar">text</foo>'),
  82. array(array('foo' => array('attr' => 'bar', 'foo' => 'text')), '<foo attr="bar"><foo>text</foo></foo>'),
  83. array(array('foo' => array('bar', 'text')), '<foo>bar</foo><foo>text</foo>'),
  84. array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
  85. array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
  86. array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
  87. array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
  88. array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
  89. 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),
  90. );
  91. }
  92. /**
  93. * @dataProvider getDataForPhpize
  94. */
  95. public function testPhpize($expected, $value)
  96. {
  97. $this->assertSame($expected, XmlUtils::phpize($value));
  98. }
  99. public function getDataForPhpize()
  100. {
  101. return array(
  102. array('', ''),
  103. array(null, 'null'),
  104. array(true, 'true'),
  105. array(false, 'false'),
  106. array(null, 'Null'),
  107. array(true, 'True'),
  108. array(false, 'False'),
  109. array(0, '0'),
  110. array(1, '1'),
  111. array(-1, '-1'),
  112. array(0777, '0777'),
  113. array(255, '0xFF'),
  114. array(100.0, '1e2'),
  115. array(-120.0, '-1.2E2'),
  116. array(-10100.1, '-10100.1'),
  117. array('-10,100.1', '-10,100.1'),
  118. array('1234 5678 9101 1121 3141', '1234 5678 9101 1121 3141'),
  119. array('1,2,3,4', '1,2,3,4'),
  120. array('11,22,33,44', '11,22,33,44'),
  121. array('11,222,333,4', '11,222,333,4'),
  122. array('1,222,333,444', '1,222,333,444'),
  123. array('11,222,333,444', '11,222,333,444'),
  124. array('111,222,333,444', '111,222,333,444'),
  125. array('1111,2222,3333,4444,5555', '1111,2222,3333,4444,5555'),
  126. array('foo', 'foo'),
  127. array(6, '0b0110'),
  128. );
  129. }
  130. public function testLoadEmptyXmlFile()
  131. {
  132. $file = __DIR__.'/../Fixtures/foo.xml';
  133. $this->setExpectedException('InvalidArgumentException', sprintf('File %s does not contain valid XML, it is empty.', $file));
  134. XmlUtils::loadFile($file);
  135. }
  136. // test for issue https://github.com/symfony/symfony/issues/9731
  137. public function testLoadWrongEmptyXMLWithErrorHandler()
  138. {
  139. $originalDisableEntities = libxml_disable_entity_loader(false);
  140. $errorReporting = error_reporting(-1);
  141. set_error_handler(function ($errno, $errstr) {
  142. throw new \Exception($errstr, $errno);
  143. });
  144. $file = __DIR__.'/../Fixtures/foo.xml';
  145. try {
  146. try {
  147. XmlUtils::loadFile($file);
  148. $this->fail('An exception should have been raised');
  149. } catch (\InvalidArgumentException $e) {
  150. $this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
  151. }
  152. } finally {
  153. restore_error_handler();
  154. error_reporting($errorReporting);
  155. }
  156. $disableEntities = libxml_disable_entity_loader(true);
  157. libxml_disable_entity_loader($disableEntities);
  158. libxml_disable_entity_loader($originalDisableEntities);
  159. $this->assertFalse($disableEntities);
  160. // should not throw an exception
  161. XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
  162. }
  163. }
  164. interface Validator
  165. {
  166. public function validate();
  167. }