XmlFileLoaderTest.php 8.4 KB

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