XmlFileLoaderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\Builder;
  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/Symfony/Components/DependencyInjection/');
  21. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  22. }
  23. public function testLoad()
  24. {
  25. $loader = new ProjectLoader2(self::$fixturesPath.'/ini');
  26. try {
  27. $loader->load('foo.xml');
  28. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  29. } catch (\Exception $e) {
  30. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  31. $this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  32. }
  33. }
  34. public function testParseFile()
  35. {
  36. $loader = new ProjectLoader2(self::$fixturesPath.'/ini');
  37. try {
  38. $loader->parseFile(self::$fixturesPath.'/ini/parameters.ini');
  39. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  40. } catch (\Exception $e) {
  41. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  42. $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');
  43. }
  44. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  45. try {
  46. $loader->parseFile(self::$fixturesPath.'/xml/nonvalid.xml');
  47. $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  48. } catch (\Exception $e) {
  49. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  50. $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');
  51. }
  52. $xml = $loader->parseFile(self::$fixturesPath.'/xml/services1.xml');
  53. $this->assertEquals('Symfony\\Components\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
  54. }
  55. public function testLoadParameters()
  56. {
  57. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  58. $config = $loader->load('services2.xml');
  59. $actual = $config->getParameters();
  60. $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'));
  61. $this->assertEquals($expected, $actual, '->load() converts XML values to PHP ones');
  62. }
  63. public function testLoadImports()
  64. {
  65. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  66. $config = $loader->load('services4.xml');
  67. $actual = $config->getParameters();
  68. $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);
  69. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  70. }
  71. public function testLoadAnonymousServices()
  72. {
  73. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  74. $config = $loader->load('services5.xml');
  75. $services = $config->getDefinitions();
  76. $this->assertEquals(3, count($services), '->load() attributes unique ids to anonymous services');
  77. $args = $services['foo']->getArguments();
  78. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  79. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  80. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  81. $inner = $services[(string) $args[0]];
  82. $this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  83. $args = $inner->getArguments();
  84. $this->assertEquals(1, count($args), '->load() references anonymous services as "normal" ones');
  85. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Reference', get_class($args[0]), '->load() converts anonymous services to references to "normal" services');
  86. $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
  87. $inner = $services[(string) $args[0]];
  88. $this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
  89. }
  90. public function testLoadServices()
  91. {
  92. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  93. $config = $loader->load('services6.xml');
  94. $services = $config->getDefinitions();
  95. $this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
  96. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts <service> element to Definition instances');
  97. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  98. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  99. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  100. $this->assertEquals('getInstance', $services['constructor']->getConstructor(), '->load() parses the constructor attribute');
  101. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  102. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  103. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  104. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  105. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  106. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  107. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  108. $aliases = $config->getAliases();
  109. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
  110. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  111. }
  112. public function testConvertDomElementToArray()
  113. {
  114. $doc = new \DOMDocument("1.0");
  115. $doc->loadXML('<foo>bar</foo>');
  116. $this->assertEquals('bar', ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  117. $doc = new \DOMDocument("1.0");
  118. $doc->loadXML('<foo foo="bar" />');
  119. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  120. $doc = new \DOMDocument("1.0");
  121. $doc->loadXML('<foo><foo>bar</foo></foo>');
  122. $this->assertEquals(array('foo' => 'bar'), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  123. $doc = new \DOMDocument("1.0");
  124. $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>');
  125. $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  126. $doc = new \DOMDocument("1.0");
  127. $doc->loadXML('<foo><foo></foo></foo>');
  128. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  129. $doc = new \DOMDocument("1.0");
  130. $doc->loadXML('<foo><foo><!-- foo --></foo></foo>');
  131. $this->assertEquals(array('foo' => null), ProjectLoader2::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \DomElement to an array');
  132. }
  133. public function testExtensions()
  134. {
  135. Loader::registerExtension(new \ProjectExtension());
  136. $loader = new ProjectLoader2(self::$fixturesPath.'/xml');
  137. $config = $loader->load('services10.xml');
  138. $services = $config->getDefinitions();
  139. $parameters = $config->getParameters();
  140. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  141. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  142. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  143. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  144. try {
  145. $config = $loader->load('services11.xml');
  146. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  147. } catch (\Exception $e) {
  148. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  149. $this->assertStringStartsWith('There is no extension able to load the configuration for "foobar:foobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  150. }
  151. try {
  152. $config = $loader->load('services12.xml');
  153. $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  154. } catch (\Exception $e) {
  155. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if an extension is not loaded');
  156. $this->assertStringStartsWith('The "foobar" tag is not valid (in', $e->getMessage(), '->load() throws an InvalidArgumentException if an extension is not loaded');
  157. }
  158. }
  159. }
  160. class ProjectLoader2 extends XmlFileLoader
  161. {
  162. public function parseFile($file)
  163. {
  164. return parent::parseFile($file);
  165. }
  166. }