YamlFileLoaderTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\YamlFileLoader;
  15. $t = new LimeTest(29);
  16. $fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  17. require_once $fixturesPath.'/includes/ProjectExtension.php';
  18. class ProjectLoader extends YamlFileLoader
  19. {
  20. public function getFilesAsArray(array $files)
  21. {
  22. return parent::getFilesAsArray($files);
  23. }
  24. }
  25. // ->getFilesAsArray()
  26. $t->diag('->getFilesAsArray()');
  27. $loader = new ProjectLoader($fixturesPath.'/ini');
  28. try
  29. {
  30. $loader->getFilesAsArray(array('foo.yml'));
  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->getFilesAsArray(array('parameters.ini'));
  40. $t->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  41. }
  42. catch (InvalidArgumentException $e)
  43. {
  44. $t->pass('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  45. }
  46. $loader = new ProjectLoader($fixturesPath.'/yaml');
  47. foreach (array('nonvalid1', 'nonvalid2') as $fixture)
  48. {
  49. try
  50. {
  51. $loader->getFilesAsArray(array($fixture.'.yml'));
  52. $t->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  53. }
  54. catch (InvalidArgumentException $e)
  55. {
  56. $t->pass('->load() throws an InvalidArgumentException if the loaded file does not validate');
  57. }
  58. }
  59. $yamls = $loader->getFilesAsArray(array('services1.yml'));
  60. $t->ok(is_array($yamls), '->getFilesAsArray() returns an array');
  61. $t->is(key($yamls), realpath($fixturesPath.'/yaml/services1.yml'), '->getFilesAsArray() returns an array where the keys are absolutes paths to the original YAML file');
  62. // ->load() # parameters
  63. $t->diag('->load() # parameters');
  64. $loader = new ProjectLoader($fixturesPath.'/yaml');
  65. $config = $loader->load(array('services2.yml'));
  66. $t->is($config->getParameters(), array('foo' => 'bar', 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), '->load() converts YAML keys to lowercase');
  67. $loader = new ProjectLoader($fixturesPath.'/yaml');
  68. $config = $loader->load(array('services2.yml', 'services3.yml'));
  69. $t->is($config->getParameters(), array('foo' => 'foo', 'values' => array(true, false), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), '->load() merges the first level of arguments when multiple files are loaded');
  70. // ->load() # imports
  71. $t->diag('->load() # imports');
  72. $config = $loader->load(array('services4.yml'));
  73. $t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar')), '->load() imports and merges imported files');
  74. // ->load() # services
  75. $t->diag('->load() # services');
  76. $config = $loader->load(array('services6.yml'));
  77. $services = $config->getDefinitions();
  78. $t->ok(isset($services['foo']), '->load() parses service elements');
  79. $t->is(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts service element to Definition instances');
  80. $t->is($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute');
  81. $t->ok($services['shared']->isShared(), '->load() parses the shared attribute');
  82. $t->ok(!$services['non_shared']->isShared(), '->load() parses the shared attribute');
  83. $t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute');
  84. $t->is($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag');
  85. $t->is($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags');
  86. $t->is($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag');
  87. $t->is($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag');
  88. $t->is($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag');
  89. $t->is($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag');
  90. $t->is($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag');
  91. $aliases = $config->getAliases();
  92. $t->ok(isset($aliases['alias_for_foo']), '->load() parses aliases');
  93. $t->is($aliases['alias_for_foo'], 'foo', '->load() parses aliases');
  94. $config = $loader->load(array('services6.yml', 'services7.yml'));
  95. $services = $config->getDefinitions();
  96. $t->is($services['foo']->getClass(), 'BarClass', '->load() merges the services when multiple files are loaded');
  97. // extensions
  98. $t->diag('extensions');
  99. Loader::registerExtension(new ProjectExtension());
  100. $loader = new ProjectLoader($fixturesPath.'/yaml');
  101. $config = $loader->load('services10.yml');
  102. $services = $config->getDefinitions();
  103. $parameters = $config->getParameters();
  104. $t->ok(isset($services['project.service.bar']), '->load() parses extension elements');
  105. $t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  106. try
  107. {
  108. $config = $loader->load('services11.yml');
  109. $t->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  110. }
  111. catch (InvalidArgumentException $e)
  112. {
  113. $t->pass('->load() throws an InvalidArgumentException if the tag is not valid');
  114. }
  115. try
  116. {
  117. $config = $loader->load('services12.yml');
  118. $t->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  119. }
  120. catch (InvalidArgumentException $e)
  121. {
  122. $t->pass('->load() throws an InvalidArgumentException if an extension is not loaded');
  123. }