XmlFileLoaderTest.php 9.3 KB

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