XmlFileLoaderTest.php 17 KB

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