PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/config/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

https://gitlab.com/fabiorf/chat
PHP | 197 lines | 152 code | 29 blank | 16 comment | 0 complexity | fc9a3f1b497c77b0134c8e9ce302c096 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. 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. }
  59. /**
  60. * @dataProvider getDataForConvertDomToArray
  61. */
  62. public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
  63. {
  64. $dom = new \DOMDocument();
  65. $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
  66. $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
  67. }
  68. public function getDataForConvertDomToArray()
  69. {
  70. return array(
  71. array(null, ''),
  72. array('bar', 'bar'),
  73. array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
  74. array(array('foo' => null), '<foo />'),
  75. array(array('foo' => 'bar'), '<foo>bar</foo>'),
  76. array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
  77. array(array('foo' => array('foo' => 0)), '<foo><foo>0</foo></foo>'),
  78. array(array('foo' => array('foo' => 'bar')), '<foo><foo>bar</foo></foo>'),
  79. array(array('foo' => array('foo' => 'bar', 'value' => 'text')), '<foo foo="bar">text</foo>'),
  80. array(array('foo' => array('attr' => 'bar', 'foo' => 'text')), '<foo attr="bar"><foo>text</foo></foo>'),
  81. array(array('foo' => array('bar', 'text')), '<foo>bar</foo><foo>text</foo>'),
  82. array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
  83. array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
  84. array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
  85. array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
  86. array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
  87. 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),
  88. );
  89. }
  90. /**
  91. * @dataProvider getDataForPhpize
  92. */
  93. public function testPhpize($expected, $value)
  94. {
  95. $this->assertSame($expected, XmlUtils::phpize($value));
  96. }
  97. public function getDataForPhpize()
  98. {
  99. return array(
  100. array('', ''),
  101. array(null, 'null'),
  102. array(true, 'true'),
  103. array(false, 'false'),
  104. array(null, 'Null'),
  105. array(true, 'True'),
  106. array(false, 'False'),
  107. array(0, '0'),
  108. array(1, '1'),
  109. array(-1, '-1'),
  110. array(0777, '0777'),
  111. array(255, '0xFF'),
  112. array(100.0, '1e2'),
  113. array(-120.0, '-1.2E2'),
  114. array(-10100.1, '-10100.1'),
  115. array('-10,100.1', '-10,100.1'),
  116. array('1234 5678 9101 1121 3141', '1234 5678 9101 1121 3141'),
  117. array('1,2,3,4', '1,2,3,4'),
  118. array('11,22,33,44', '11,22,33,44'),
  119. array('11,222,333,4', '11,222,333,4'),
  120. array('1,222,333,444', '1,222,333,444'),
  121. array('11,222,333,444', '11,222,333,444'),
  122. array('111,222,333,444', '111,222,333,444'),
  123. array('1111,2222,3333,4444,5555', '1111,2222,3333,4444,5555'),
  124. array('foo', 'foo'),
  125. array(6, '0b0110'),
  126. );
  127. }
  128. public function testLoadEmptyXmlFile()
  129. {
  130. $file = __DIR__.'/../Fixtures/foo.xml';
  131. $this->setExpectedException('InvalidArgumentException', 'File '.$file.' does not contain valid XML, it is empty.');
  132. XmlUtils::loadFile($file);
  133. }
  134. // test for issue https://github.com/symfony/symfony/issues/9731
  135. public function testLoadWrongEmptyXMLWithErrorHandler()
  136. {
  137. $originalDisableEntities = libxml_disable_entity_loader(false);
  138. $errorReporting = error_reporting(-1);
  139. set_error_handler(function ($errno, $errstr) {
  140. throw new \Exception($errstr, $errno);
  141. });
  142. $file = __DIR__.'/../Fixtures/foo.xml';
  143. try {
  144. try {
  145. XmlUtils::loadFile($file);
  146. $this->fail('An exception should have been raised');
  147. } catch (\InvalidArgumentException $e) {
  148. $this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
  149. }
  150. } catch (\Exception $e) {
  151. restore_error_handler();
  152. error_reporting($errorReporting);
  153. throw $e;
  154. }
  155. restore_error_handler();
  156. error_reporting($errorReporting);
  157. $disableEntities = libxml_disable_entity_loader(true);
  158. libxml_disable_entity_loader($disableEntities);
  159. libxml_disable_entity_loader($originalDisableEntities);
  160. $this->assertFalse($disableEntities);
  161. // should not throw an exception
  162. XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
  163. }
  164. }
  165. interface Validator
  166. {
  167. public function validate();
  168. }