XmlFileLoaderTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\DependencyInjection\Tests\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. protected static $fixturesPath;
  24. protected function setUp()
  25. {
  26. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  27. $this->markTestSkipped('The "Config" component is not available');
  28. }
  29. }
  30. public static function setUpBeforeClass()
  31. {
  32. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  33. require_once self::$fixturesPath.'/includes/foo.php';
  34. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  35. require_once self::$fixturesPath.'/includes/ProjectWithXsdExtension.php';
  36. }
  37. public function testLoad()
  38. {
  39. $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
  40. try {
  41. $loader->load('foo.xml');
  42. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  43. } catch (\Exception $e) {
  44. $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  45. $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  46. }
  47. }
  48. public function testParseFile()
  49. {
  50. $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
  51. $r = new \ReflectionObject($loader);
  52. $m = $r->getMethod('parseFile');
  53. $m->setAccessible(true);
  54. try {
  55. $m->invoke($loader, self::$fixturesPath.'/ini/parameters.ini');
  56. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  59. $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'parameters.ini'), $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  60. $e = $e->getPrevious();
  61. $this->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  62. $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');
  63. }
  64. $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/xml'));
  65. try {
  66. $m->invoke($loader, self::$fixturesPath.'/xml/nonvalid.xml');
  67. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  70. $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'nonvalid.xml'), $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  71. $e = $e->getPrevious();
  72. $this->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  73. $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');
  74. }
  75. $xml = $m->invoke($loader, self::$fixturesPath.'/xml/services1.xml');
  76. $this->assertEquals('Symfony\\Component\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
  77. }
  78. public function testLoadParameters()
  79. {
  80. $container = new ContainerBuilder();
  81. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  82. $loader->load('services2.xml');
  83. $actual = $container->getParameterBag()->all();
  84. $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'), 'mixedcase' => array('MixedCaseKey' => 'value'));
  85. $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
  86. }
  87. public function testLoadImports()
  88. {
  89. if (!class_exists('Symfony\Component\Yaml\Yaml')) {
  90. $this->markTestSkipped('The "Yaml" component is not available');
  91. }
  92. $container = new ContainerBuilder();
  93. $resolver = new LoaderResolver(array(
  94. new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
  95. new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
  96. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
  97. ));
  98. $loader->setResolver($resolver);
  99. $loader->load('services4.xml');
  100. $actual = $container->getParameterBag()->all();
  101. $expected = array('a string', 'foo' => 'bar', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'bar' => '%foo%', 'imported_from_ini' => true, 'imported_from_yaml' => true);
  102. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  103. // Bad import throws no exception due to ignore_errors value.
  104. $loader->load('services4_bad_import.xml');
  105. }
  106. public function testLoadAnonymousServices()
  107. {
  108. $container = new ContainerBuilder();
  109. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  110. $loader->load('services5.xml');
  111. $services = $container->getDefinitions();
  112. $this->assertEquals(4, count($services), '->load() attributes unique ids to anonymous services');
  113. // anonymous service as an argument
  114. $args = $services['foo']->getArguments();
  115. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  116. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  117. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  118. $inner = $services[(string) $args[0]];
  119. $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  120. // inner anonymous services
  121. $args = $inner->getArguments();
  122. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  123. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  124. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  125. $inner = $services[(string) $args[0]];
  126. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  127. // anonymous service as a property
  128. $properties = $services['foo']->getProperties();
  129. $property = $properties['p'];
  130. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Reference', get_class($property), '->load() converts anonymous services to references to "normal" services');
  131. $this->assertTrue(isset($services[(string) $property]), '->load() makes a reference to the created ones');
  132. $inner = $services[(string) $property];
  133. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  134. }
  135. public function testLoadServices()
  136. {
  137. $container = new ContainerBuilder();
  138. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  139. $loader->load('services6.xml');
  140. $services = $container->getDefinitions();
  141. $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
  142. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts <service> element to Definition instances');
  143. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  144. $this->assertEquals('container', $services['scope.container']->getScope());
  145. $this->assertEquals('custom', $services['scope.custom']->getScope());
  146. $this->assertEquals('prototype', $services['scope.prototype']->getScope());
  147. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory-method attribute');
  148. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  149. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  150. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  151. $this->assertEquals(array(new Reference('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  152. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  153. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  154. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  155. $this->assertNull($services['factory_service']->getClass());
  156. $this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
  157. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  158. $this->assertTrue($services['request']->isSynthetic(), '->load() parses the synthetic flag');
  159. $this->assertTrue($services['request']->isSynchronized(), '->load() parses the synchronized flag');
  160. $this->assertTrue($services['request']->isLazy(), '->load() parses the lazy flag');
  161. $aliases = $container->getAliases();
  162. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
  163. $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
  164. $this->assertTrue($aliases['alias_for_foo']->isPublic());
  165. $this->assertTrue(isset($aliases['another_alias_for_foo']));
  166. $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
  167. $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
  168. }
  169. public function testConvertDomElementToArray()
  170. {
  171. $doc = new \DOMDocument("1.0");
  172. $doc->loadXML('<foo>bar</foo>');
  173. $this->assertEquals('bar', XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  174. $doc = new \DOMDocument("1.0");
  175. $doc->loadXML('<foo foo="bar" />');
  176. $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  177. $doc = new \DOMDocument("1.0");
  178. $doc->loadXML('<foo><foo>bar</foo></foo>');
  179. $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  180. $doc = new \DOMDocument("1.0");
  181. $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
  182. $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  183. $doc = new \DOMDocument("1.0");
  184. $doc->loadXML('<foo><foo></foo></foo>');
  185. $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  186. $doc = new \DOMDocument("1.0");
  187. $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
  188. $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  189. $doc = new \DOMDocument("1.0");
  190. $doc->loadXML('<foo><foo foo="bar"/><foo foo="bar"/></foo>');
  191. $this->assertEquals(array('foo' => array(array('foo' => 'bar'), array('foo' => 'bar'))), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  192. }
  193. public function testExtensions()
  194. {
  195. $container = new ContainerBuilder();
  196. $container->registerExtension(new \ProjectExtension());
  197. $container->registerExtension(new \ProjectWithXsdExtension());
  198. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  199. // extension without an XSD
  200. $loader->load('extensions/services1.xml');
  201. $container->compile();
  202. $services = $container->getDefinitions();
  203. $parameters = $container->getParameterBag()->all();
  204. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  205. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  206. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  207. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  208. // extension with an XSD
  209. $container = new ContainerBuilder();
  210. $container->registerExtension(new \ProjectExtension());
  211. $container->registerExtension(new \ProjectWithXsdExtension());
  212. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  213. $loader->load('extensions/services2.xml');
  214. $container->compile();
  215. $services = $container->getDefinitions();
  216. $parameters = $container->getParameterBag()->all();
  217. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  218. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  219. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  220. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  221. $container = new ContainerBuilder();
  222. $container->registerExtension(new \ProjectExtension());
  223. $container->registerExtension(new \ProjectWithXsdExtension());
  224. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  225. // extension with an XSD (does not validate)
  226. try {
  227. $loader->load('extensions/services3.xml');
  228. $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  229. } catch (\Exception $e) {
  230. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  231. $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services3.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  232. $e = $e->getPrevious();
  233. $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  234. $this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  235. }
  236. // non-registered extension
  237. try {
  238. $loader->load('extensions/services4.xml');
  239. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  240. } catch (\Exception $e) {
  241. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  242. $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');
  243. }
  244. }
  245. public function testExtensionInPhar()
  246. {
  247. if (extension_loaded('suhosin') && false === strpos(ini_get('suhosin.executor.include.whitelist'), 'phar')) {
  248. $this->markTestSkipped('To run this test, add "phar" to the "suhosin.executor.include.whitelist" settings in your php.ini file.');
  249. }
  250. require_once self::$fixturesPath.'/includes/ProjectWithXsdExtensionInPhar.phar';
  251. // extension with an XSD in PHAR archive
  252. $container = new ContainerBuilder();
  253. $container->registerExtension(new \ProjectWithXsdExtensionInPhar());
  254. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  255. $loader->load('extensions/services6.xml');
  256. // extension with an XSD in PHAR archive (does not validate)
  257. try {
  258. $loader->load('extensions/services7.xml');
  259. $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  260. } catch (\Exception $e) {
  261. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  262. $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services7.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  263. $e = $e->getPrevious();
  264. $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  265. $this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  266. }
  267. }
  268. /**
  269. * @covers Symfony\Component\DependencyInjection\Loader\XmlFileLoader::supports
  270. */
  271. public function testSupports()
  272. {
  273. $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
  274. $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
  275. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  276. }
  277. public function testNoNamingConflictsForAnonymousServices()
  278. {
  279. $container = new ContainerBuilder();
  280. $loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension1'));
  281. $loader1->load('services.xml');
  282. $services = $container->getDefinitions();
  283. $this->assertEquals(2, count($services), '->load() attributes unique ids to anonymous services');
  284. $loader2 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml/extension2'));
  285. $loader2->load('services.xml');
  286. $services = $container->getDefinitions();
  287. $this->assertEquals(4, count($services), '->load() attributes unique ids to anonymous services');
  288. $services = $container->getDefinitions();
  289. $args1 = $services['extension1.foo']->getArguments();
  290. $inner1 = $services[(string) $args1[0]];
  291. $this->assertEquals('BarClass1', $inner1->getClass(), '->load() uses the same configuration as for the anonymous ones');
  292. $args2 = $services['extension2.foo']->getArguments();
  293. $inner2 = $services[(string) $args2[0]];
  294. $this->assertEquals('BarClass2', $inner2->getClass(), '->load() uses the same configuration as for the anonymous ones');
  295. }
  296. public function testDocTypeIsNotAllowed()
  297. {
  298. $container = new ContainerBuilder();
  299. $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
  300. // document types are not allowed.
  301. try {
  302. $loader->load('withdoctype.xml');
  303. $this->fail('->load() throws an InvalidArgumentException if the configuration contains a document type');
  304. } catch (\Exception $e) {
  305. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
  306. $this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'withdoctype.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
  307. $e = $e->getPrevious();
  308. $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
  309. $this->assertSame('Document types are not allowed.', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
  310. }
  311. }
  312. }