PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php

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