XmlFileLoaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\Components\DependencyInjection\Loader;
  10. use Symfony\Components\DependencyInjection\ContainerBuilder;
  11. use Symfony\Components\DependencyInjection\Reference;
  12. use Symfony\Components\DependencyInjection\Definition;
  13. use Symfony\Components\DependencyInjection\Loader\Loader;
  14. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  15. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. static protected $fixturesPath;
  18. static public function setUpBeforeClass()
  19. {
  20. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  21. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  22. require_once self::$fixturesPath.'/includes/ProjectWithXsdExtension.php';
  23. }
  24. public function testLoad()
  25. {
  26. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/ini');
  27. try {
  28. $loader->load('foo.xml');
  29. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  30. } catch (\Exception $e) {
  31. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  32. $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  33. }
  34. }
  35. public function testParseFile()
  36. {
  37. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/ini');
  38. try {
  39. $loader->parseFile(self::$fixturesPath.'/ini/parameters.ini');
  40. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  41. } catch (\Exception $e) {
  42. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  43. $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');
  44. }
  45. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/xml');
  46. try {
  47. $loader->parseFile(self::$fixturesPath.'/xml/nonvalid.xml');
  48. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  49. } catch (\Exception $e) {
  50. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  51. $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');
  52. }
  53. $xml = $loader->parseFile(self::$fixturesPath.'/xml/services1.xml');
  54. $this->assertEquals('Symfony\\Components\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
  55. }
  56. public function testLoadParameters()
  57. {
  58. $container = new ContainerBuilder();
  59. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  60. $loader->load('services2.xml');
  61. $actual = $container->getParameterBag()->all();
  62. $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'));
  63. $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
  64. }
  65. public function testLoadImports()
  66. {
  67. $container = new ContainerBuilder();
  68. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  69. $loader->load('services4.xml');
  70. $actual = $container->getParameterBag()->all();
  71. $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);
  72. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  73. }
  74. public function testLoadAnonymousServices()
  75. {
  76. $container = new ContainerBuilder();
  77. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  78. $loader->load('services5.xml');
  79. $services = $container->getDefinitions();
  80. $this->assertEquals(3, count($services), '->load() attributes unique ids to anonymous services');
  81. $args = $services['foo']->getArguments();
  82. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  83. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  84. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  85. $inner = $services[(string) $args[0]];
  86. $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  87. $args = $inner->getArguments();
  88. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  89. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  90. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  91. $inner = $services[(string) $args[0]];
  92. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  93. }
  94. public function testLoadServices()
  95. {
  96. $container = new ContainerBuilder();
  97. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  98. $loader->load('services6.xml');
  99. $services = $container->getDefinitions();
  100. $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
  101. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts <service> element to Definition instances');
  102. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  103. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  104. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  105. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory-method attribute');
  106. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  107. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  108. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  109. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  110. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  111. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  112. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  113. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  114. $aliases = $container->getAliases();
  115. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
  116. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  117. }
  118. public function testConvertDomElementToArray()
  119. {
  120. $doc = new \DOMDocument("1.0");
  121. $doc->loadXML('<foo>bar</foo>');
  122. $this->assertEquals('bar', ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  123. $doc = new \DOMDocument("1.0");
  124. $doc->loadXML('<foo foo="bar" />');
  125. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  126. $doc = new \DOMDocument("1.0");
  127. $doc->loadXML('<foo><foo>bar</foo></foo>');
  128. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  129. $doc = new \DOMDocument("1.0");
  130. $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
  131. $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  132. $doc = new \DOMDocument("1.0");
  133. $doc->loadXML('<foo><foo></foo></foo>');
  134. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  135. $doc = new \DOMDocument("1.0");
  136. $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
  137. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  138. $doc = new \DOMDocument("1.0");
  139. $doc->loadXML('<foo><foo foo="bar"/><foo foo="bar"/></foo>');
  140. $this->assertEquals(array('foo' => array(array('foo' => 'bar'), array('foo' => 'bar'))), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  141. }
  142. public function testExtensions()
  143. {
  144. $container = new ContainerBuilder();
  145. $container->registerExtension(new \ProjectExtension());
  146. $container->registerExtension(new \ProjectWithXsdExtension());
  147. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  148. // extension without an XSD
  149. $loader->load('extensions/services1.xml');
  150. $container->freeze();
  151. $services = $container->getDefinitions();
  152. $parameters = $container->getParameterBag()->all();
  153. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  154. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  155. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  156. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  157. // extension with an XSD
  158. $container = new ContainerBuilder();
  159. $container->registerExtension(new \ProjectExtension());
  160. $container->registerExtension(new \ProjectWithXsdExtension());
  161. $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml');
  162. $loader->load('extensions/services2.xml');
  163. $container->freeze();
  164. $services = $container->getDefinitions();
  165. $parameters = $container->getParameterBag()->all();
  166. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  167. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  168. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  169. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  170. $loader = new ProjectLoader2(new ContainerBuilder(), self::$fixturesPath.'/xml');
  171. // extension with an XSD (does not validate)
  172. try {
  173. $loader->load('extensions/services3.xml');
  174. $this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  175. } catch (\Exception $e) {
  176. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  177. $this->assertRegexp('/The attribute \'bar\' is not allowed/', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  178. }
  179. // non-registered extension
  180. try {
  181. $loader->load('extensions/services4.xml');
  182. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  183. } catch (\Exception $e) {
  184. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  185. $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');
  186. }
  187. // non-existent tag for a known extension
  188. try {
  189. $loader->load('extensions/services5.xml');
  190. $this->fail('->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
  191. } catch (\Exception $e) {
  192. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
  193. $this->assertStringStartsWith('The tag "projectwithxsd:foobar" is not defined in the "projectwithxsd" extension.', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
  194. }
  195. }
  196. }
  197. class ProjectLoader2 extends XmlFileLoader
  198. {
  199. public function parseFile($file)
  200. {
  201. return parent::parseFile($file);
  202. }
  203. }