YamlFileLoaderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(25);
  16. $fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  17. require_once $fixturesPath.'/includes/ProjectExtension.php';
  18. class ProjectLoader extends YamlFileLoader
  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.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->loadFile('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->loadFile($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. // ->load() # parameters
  60. $t->diag('->load() # parameters');
  61. $loader = new ProjectLoader($fixturesPath.'/yaml');
  62. $config = $loader->load('services2.yml');
  63. $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');
  64. // ->load() # imports
  65. $t->diag('->load() # imports');
  66. $config = $loader->load('services4.yml');
  67. $t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'imported_from_xml' => true, 'imported_from_ini' => true), '->load() imports and merges imported files');
  68. // ->load() # services
  69. $t->diag('->load() # services');
  70. $config = $loader->load('services6.yml');
  71. $services = $config->getDefinitions();
  72. $t->ok(isset($services['foo']), '->load() parses service elements');
  73. $t->is(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts service element to Definition instances');
  74. $t->is($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute');
  75. $t->ok($services['shared']->isShared(), '->load() parses the shared attribute');
  76. $t->ok(!$services['non_shared']->isShared(), '->load() parses the shared attribute');
  77. $t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute');
  78. $t->is($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag');
  79. $t->is($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags');
  80. $t->is($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag');
  81. $t->is($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag');
  82. $t->is($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag');
  83. $t->is($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag');
  84. $t->is($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag');
  85. $aliases = $config->getAliases();
  86. $t->ok(isset($aliases['alias_for_foo']), '->load() parses aliases');
  87. $t->is($aliases['alias_for_foo'], 'foo', '->load() parses aliases');
  88. // extensions
  89. $t->diag('extensions');
  90. Loader::registerExtension(new ProjectExtension());
  91. $loader = new ProjectLoader($fixturesPath.'/yaml');
  92. $config = $loader->load('services10.yml');
  93. $services = $config->getDefinitions();
  94. $parameters = $config->getParameters();
  95. $t->ok(isset($services['project.service.bar']), '->load() parses extension elements');
  96. $t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  97. try
  98. {
  99. $config = $loader->load('services11.yml');
  100. $t->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  101. }
  102. catch (InvalidArgumentException $e)
  103. {
  104. $t->pass('->load() throws an InvalidArgumentException if the tag is not valid');
  105. }
  106. try
  107. {
  108. $config = $loader->load('services12.yml');
  109. $t->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  110. }
  111. catch (InvalidArgumentException $e)
  112. {
  113. $t->pass('->load() throws an InvalidArgumentException if an extension is not loaded');
  114. }