XmlFileLoaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\DependencyInjection\Loader;
  10. use Symfony\Components\DependencyInjection\Builder;
  11. use Symfony\Components\DependencyInjection\Reference;
  12. use Symfony\Components\DependencyInjection\Definition;
  13. use Symfony\Components\DependencyInjection\Loader\Loader;
  14. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  15. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. static protected $fixturesPath;
  18. static public function setUpBeforeClass()
  19. {
  20. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  21. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  22. }
  23. public function testLoad()
  24. {
  25. $loader = new ProjectLoader2(self::$fixturesPath.'/ini');
  26. try
  27. {
  28. $loader->load('foo.xml');
  29. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  30. }
  31. catch (\Exception $e)
  32. {
  33. $this->assertType('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  34. $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  35. }
  36. }
  37. public function testParseFile()
  38. {
  39. $loader = new ProjectLoader2(self::$fixturesPath.'/ini');
  40. try
  41. {
  42. $loader->parseFile(self::$fixturesPath.'/ini/parameters.ini');
  43. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  44. }
  45. catch (\Exception $e)
  46. {
  47. $this->assertType('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  48. $this->assertStringStartsWith('[ERROR 4] Start tag expected, \'<\' not found (in', $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  49. }
  50. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  51. try
  52. {
  53. $loader->parseFile(self::$fixturesPath.'/xml/nonvalid.xml');
  54. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  55. }
  56. catch (\Exception $e)
  57. {
  58. $this->assertType('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  59. $this->assertStringStartsWith('[ERROR 1845] Element \'nonvalid\': No matching global declaration available for the validation root. (in', $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  60. }
  61. $xml = $loader->parseFile(self::$fixturesPath.'/xml/services1.xml');
  62. $this->assertEquals('Symfony\\Components\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
  63. }
  64. public function testLoadParameters()
  65. {
  66. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  67. $config = $loader->load('services2.xml');
  68. $actual = $config->getParameters();
  69. $expected = array('a string', 'foo' => 'bar', 'values' => array(0, 'integer' => 4, 100 => null, 'true', true, false, 'on', 'off', 'float' => 1.3, 1000.3, 'a string', array('foo', 'bar')), 'foo_bar' => new Reference('foo_bar'));
  70. $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
  71. }
  72. public function testLoadImports()
  73. {
  74. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  75. $config = $loader->load('services4.xml');
  76. $actual = $config->getParameters();
  77. $expected = array('a string', 'foo' => 'bar', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'bar' => '%foo%', 'imported_from_ini' => true, 'imported_from_yaml' => true);
  78. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  79. }
  80. public function testLoadAnonymousServices()
  81. {
  82. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  83. $config = $loader->load('services5.xml');
  84. $services = $config->getDefinitions();
  85. $this->assertEquals(3, count($services), '->load() attributes unique ids to anonymous services');
  86. $args = $services['foo']->getArguments();
  87. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  88. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  89. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  90. $inner = $services[(string) $args[0]];
  91. $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  92. $args = $inner->getArguments();
  93. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  94. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  95. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  96. $inner = $services[(string) $args[0]];
  97. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  98. }
  99. public function testLoadServices()
  100. {
  101. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  102. $config = $loader->load('services6.xml');
  103. $services = $config->getDefinitions();
  104. $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
  105. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts <service> element to Definition instances');
  106. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  107. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  108. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  109. $this->assertEquals('getInstance', $services['constructor']->getConstructor(), '->load() parses the constructor attribute');
  110. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  111. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  112. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  113. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  114. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  115. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  116. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  117. $aliases = $config->getAliases();
  118. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
  119. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  120. }
  121. public function testConvertDomElementToArray()
  122. {
  123. $doc = new \DOMDocument("1.0");
  124. $doc->loadXML('<foo>bar</foo>');
  125. $this->assertEquals('bar', ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  126. $doc = new \DOMDocument("1.0");
  127. $doc->loadXML('<foo foo="bar" />');
  128. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  129. $doc = new \DOMDocument("1.0");
  130. $doc->loadXML('<foo><foo>bar</foo></foo>');
  131. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  132. $doc = new \DOMDocument("1.0");
  133. $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
  134. $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  135. $doc = new \DOMDocument("1.0");
  136. $doc->loadXML('<foo><foo></foo></foo>');
  137. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  138. $doc = new \DOMDocument("1.0");
  139. $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
  140. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  141. }
  142. public function testExtensions()
  143. {
  144. Loader::registerExtension(new \ProjectExtension());
  145. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  146. $config = $loader->load('services10.xml');
  147. $services = $config->getDefinitions();
  148. $parameters = $config->getParameters();
  149. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  150. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  151. try
  152. {
  153. $config = $loader->load('services11.xml');
  154. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  155. }
  156. catch (\Exception $e)
  157. {
  158. $this->assertType('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  159. $this->assertStringStartsWith('There is no extension able to load the configuration for "foobar:foobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  160. }
  161. try
  162. {
  163. $config = $loader->load('services12.xml');
  164. $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  165. }
  166. catch (\Exception $e)
  167. {
  168. $this->assertType('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if an extension is not loaded');
  169. $this->assertStringStartsWith('The "foobar" tag is not valid (in', $e->getMessage(), '->load() throws an InvalidArgumentException if an extension is not loaded');
  170. }
  171. }
  172. }
  173. class ProjectLoader2 extends XmlFileLoader
  174. {
  175. public function parseFile($file)
  176. {
  177. return parent::parseFile($file);
  178. }
  179. }