XmlFileLoaderTest.php 9.2 KB

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