XmlFileLoaderTest.php 9.4 KB

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