XmlFileLoaderTest.php 8.2 KB

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