XmlFileLoaderTest.php 17 KB

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