XmlUtilsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  51. /**
  52. * @dataProvider getDataForConvertDomToArray
  53. */
  54. public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
  55. {
  56. $dom = new \DOMDocument();
  57. $dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
  58. $this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
  59. }
  60. public function getDataForConvertDomToArray()
  61. {
  62. return array(
  63. array(null, ''),
  64. array('bar', 'bar'),
  65. array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
  66. array(array('foo' => null), '<foo />'),
  67. array(array('foo' => 'bar'), '<foo>bar</foo>'),
  68. array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
  69. array(array('foo' => array('foo' => 'bar')), '<foo><foo>bar</foo></foo>'),
  70. array(array('foo' => array('foo' => 'bar', 'value' => 'text')), '<foo foo="bar">text</foo>'),
  71. array(array('foo' => array('attr' => 'bar', 'foo' => 'text')), '<foo attr="bar"><foo>text</foo></foo>'),
  72. array(array('foo' => array('bar', 'text')), '<foo>bar</foo><foo>text</foo>'),
  73. array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
  74. array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
  75. array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
  76. array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
  77. array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
  78. 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),
  79. );
  80. }
  81. /**
  82. * @dataProvider getDataForPhpize
  83. */
  84. public function testPhpize($expected, $value)
  85. {
  86. $this->assertSame($expected, XmlUtils::phpize($value));
  87. }
  88. public function getDataForPhpize()
  89. {
  90. return array(
  91. array(null, 'null'),
  92. array(true, 'true'),
  93. array(false, 'false'),
  94. array(null, 'Null'),
  95. array(true, 'True'),
  96. array(false, 'False'),
  97. array(0, '0'),
  98. array(1, '1'),
  99. array(0777, '0777'),
  100. array(255, '0xFF'),
  101. array(100.0, '1e2'),
  102. array(-120.0, '-1.2E2'),
  103. array(-10100.1, '-10100.1'),
  104. array('-10,100.1', '-10,100.1'),
  105. array('1234 5678 9101 1121 3141', '1234 5678 9101 1121 3141'),
  106. array('1,2,3,4', '1,2,3,4'),
  107. array('11,22,33,44', '11,22,33,44'),
  108. array('11,222,333,4', '11,222,333,4'),
  109. array('1,222,333,444', '1,222,333,444'),
  110. array('11,222,333,444', '11,222,333,444'),
  111. array('111,222,333,444', '111,222,333,444'),
  112. array('1111,2222,3333,4444,5555', '1111,2222,3333,4444,5555'),
  113. array('foo', 'foo'),
  114. );
  115. }
  116. }
  117. interface Validator
  118. {
  119. public function validate();
  120. }