XmlFileLoaderTest.php 18 KB

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