XmlFileLoaderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Component\DependencyInjection\Loader;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Loader\Loader;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  16. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  17. use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
  18. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. static protected $fixturesPath;
  21. static public function setUpBeforeClass()
  22. {
  23. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  24. require_once self::$fixturesPath.'/includes/foo.php';
  25. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  26. require_once self::$fixturesPath.'/includes/ProjectWithXsdExtension.php';
  27. }
  28. public function testLoad()
  29. {
  30. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/ini');
  31. try {
  32. $loader->load('foo.xml');
  33. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  34. } catch (\Exception $e) {
  35. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  36. $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  37. }
  38. }
  39. public function testParseFile()
  40. {
  41. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/ini');
  42. try {
  43. $loader->parseFile(self::$fixturesPath.'/ini/parameters.ini');
  44. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  45. } catch (\Exception $e) {
  46. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  47. $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');
  48. }
  49. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/xml');
  50. try {
  51. $loader->parseFile(self::$fixturesPath.'/xml/nonvalid.xml');
  52. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  53. } catch (\Exception $e) {
  54. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  55. $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');
  56. }
  57. $xml = $loader->parseFile(self::$fixturesPath.'/xml/services1.xml');
  58. $this->assertEquals('Symfony\\Component\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
  59. }
  60. public function testLoadParameters()
  61. {
  62. $container = new ContainerBuilder();
  63. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  64. $loader->load('services2.xml');
  65. $actual = $container->getParameterBag()->all();
  66. $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'));
  67. $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
  68. }
  69. public function testLoadImports()
  70. {
  71. $container = new ContainerBuilder();
  72. $resolver = new LoaderResolver(array(
  73. new IniFileLoader($container, self::$fixturesPath.'/xml'),
  74. new YamlFileLoader($container, self::$fixturesPath.'/xml'),
  75. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml'),
  76. ));
  77. $loader->setResolver($resolver);
  78. $loader->load('services4.xml');
  79. $actual = $container->getParameterBag()->all();
  80. $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);
  81. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  82. }
  83. public function testLoadAnonymousServices()
  84. {
  85. $container = new ContainerBuilder();
  86. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  87. $loader->load('services5.xml');
  88. $services = $container->getDefinitions();
  89. $this->assertEquals(3, count($services), '->load() attributes unique ids to anonymous services');
  90. $args = $services['foo']->getArguments();
  91. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  92. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  93. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  94. $inner = $services[(string) $args[0]];
  95. $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  96. $args = $inner->getArguments();
  97. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  98. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  99. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  100. $inner = $services[(string) $args[0]];
  101. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  102. }
  103. public function testLoadServices()
  104. {
  105. $container = new ContainerBuilder();
  106. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  107. $loader->load('services6.xml');
  108. $services = $container->getDefinitions();
  109. $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
  110. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts <service> element to Definition instances');
  111. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  112. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  113. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  114. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory-method attribute');
  115. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  116. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  117. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  118. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  119. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  120. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  121. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  122. $this->assertNull($services['factory_service']->getClass());
  123. $this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
  124. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  125. $aliases = $container->getAliases();
  126. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
  127. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  128. }
  129. public function testConvertDomElementToArray()
  130. {
  131. $doc = new \DOMDocument("1.0");
  132. $doc->loadXML('<foo>bar</foo>');
  133. $this->assertEquals('bar', ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  134. $doc = new \DOMDocument("1.0");
  135. $doc->loadXML('<foo foo="bar" />');
  136. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  137. $doc = new \DOMDocument("1.0");
  138. $doc->loadXML('<foo><foo>bar</foo></foo>');
  139. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  140. $doc = new \DOMDocument("1.0");
  141. $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
  142. $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  143. $doc = new \DOMDocument("1.0");
  144. $doc->loadXML('<foo><foo></foo></foo>');
  145. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  146. $doc = new \DOMDocument("1.0");
  147. $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
  148. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  149. $doc = new \DOMDocument("1.0");
  150. $doc->loadXML('<foo><foo foo="bar"/><foo foo="bar"/></foo>');
  151. $this->assertEquals(array('foo' => array(array('foo' => 'bar'), array('foo' => 'bar'))), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  152. }
  153. public function testExtensions()
  154. {
  155. $container = new ContainerBuilder();
  156. $container->registerExtension(new \ProjectExtension());
  157. $container->registerExtension(new \ProjectWithXsdExtension());
  158. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  159. // extension without an XSD
  160. $loader->load('extensions/services1.xml');
  161. $container->freeze();
  162. $services = $container->getDefinitions();
  163. $parameters = $container->getParameterBag()->all();
  164. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  165. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  166. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  167. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  168. // extension with an XSD
  169. $container = new ContainerBuilder();
  170. $container->registerExtension(new \ProjectExtension());
  171. $container->registerExtension(new \ProjectWithXsdExtension());
  172. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  173. $loader->load('extensions/services2.xml');
  174. $container->freeze();
  175. $services = $container->getDefinitions();
  176. $parameters = $container->getParameterBag()->all();
  177. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  178. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  179. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  180. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  181. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/xml');
  182. // extension with an XSD (does not validate)
  183. try {
  184. $loader->load('extensions/services3.xml');
  185. $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  186. } catch (\Exception $e) {
  187. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  188. $this->assertRegexp('/The attribute \'bar\' is not allowed/', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  189. }
  190. // non-registered extension
  191. try {
  192. $loader->load('extensions/services4.xml');
  193. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  194. } catch (\Exception $e) {
  195. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  196. $this->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  197. }
  198. }
  199. /**
  200. * @covers Symfony\Component\DependencyInjection\Loader\XmlFileLoader::supports
  201. */
  202. public function testSupports()
  203. {
  204. $loader = new XmlFileLoader(new ContainerBuilder());
  205. $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
  206. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  207. }
  208. public function testLoadInterfaceInjectors()
  209. {
  210. $container = new ContainerBuilder();
  211. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  212. $loader->load('interfaces1.xml');
  213. $interfaces = $container->getInterfaceInjectors('FooClass');
  214. $this->assertEquals(1, count($interfaces), '->load() parses <interface> elements');
  215. $interface = $interfaces['FooClass'];
  216. $this->assertTrue($interface->hasMethodCall('setBar'), '->load() applies method calls correctly');
  217. }
  218. }
  219. class ProjectLoader2 extends XmlFileLoader
  220. {
  221. public function parseFile($file)
  222. {
  223. return parent::parseFile($file);
  224. }
  225. }