XmlFileLoaderTest.php 17 KB

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