YamlFileLoaderTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace Symfony\Tests\Components\DependencyInjection\Loader;
  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. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. static protected $fixturesPath;
  18. static public function setUpBeforeClass()
  19. {
  20. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  21. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  22. }
  23. public function testLoadFile()
  24. {
  25. $loader = new ProjectLoader3(self::$fixturesPath.'/ini');
  26. try {
  27. $loader->loadFile('foo.yml');
  28. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  29. } catch (\Exception $e) {
  30. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  31. $this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  32. }
  33. try {
  34. $loader->loadFile('parameters.ini');
  35. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  36. } catch (\Exception $e) {
  37. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  38. $this->assertEquals('The service file "parameters.ini" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  39. }
  40. $loader = new ProjectLoader3(self::$fixturesPath.'/yaml');
  41. foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
  42. try {
  43. $loader->loadFile($fixture.'.yml');
  44. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  45. } catch (\Exception $e) {
  46. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
  47. $this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
  48. }
  49. }
  50. }
  51. public function testLoadParameters()
  52. {
  53. $loader = new ProjectLoader3(self::$fixturesPath.'/yaml');
  54. $config = $loader->load('services2.yml');
  55. $this->assertEquals(array('foo' => 'bar', 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), $config->getParameters(), '->load() converts YAML keys to lowercase');
  56. }
  57. public function testLoadImports()
  58. {
  59. $loader = new ProjectLoader3(self::$fixturesPath.'/yaml');
  60. $config = $loader->load('services4.yml');
  61. $actual = $config->getParameters();
  62. $expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'foo_bar' => new Reference('foo_bar'), 'imported_from_ini' => true, 'imported_from_xml' => true);
  63. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  64. }
  65. public function testLoadServices()
  66. {
  67. $loader = new ProjectLoader3(self::$fixturesPath.'/yaml');
  68. $config = $loader->load('services6.yml');
  69. $services = $config->getDefinitions();
  70. $this->assertTrue(isset($services['foo']), '->load() parses service elements');
  71. $this->assertEquals('Symfony\\Components\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts service element to Definition instances');
  72. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  73. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  74. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  75. $this->assertEquals('getInstance', $services['constructor']->getConstructor(), '->load() parses the constructor attribute');
  76. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  77. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  78. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  79. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  80. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  81. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  82. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  83. $aliases = $config->getAliases();
  84. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
  85. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  86. }
  87. public function testExtensions()
  88. {
  89. Loader::registerExtension(new \ProjectExtension());
  90. $loader = new ProjectLoader3(self::$fixturesPath.'/yaml');
  91. $config = $loader->load('services10.yml');
  92. $services = $config->getDefinitions();
  93. $parameters = $config->getParameters();
  94. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  95. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  96. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  97. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  98. try {
  99. $config = $loader->load('services11.yml');
  100. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  101. } catch (\Exception $e) {
  102. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  103. $this->assertStringStartsWith('There is no extension able to load the configuration for "foobar.foobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  104. }
  105. try {
  106. $config = $loader->load('services12.yml');
  107. $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  108. } catch (\Exception $e) {
  109. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if an extension is not loaded');
  110. $this->assertStringStartsWith('The "foobar" tag is not valid (in', $e->getMessage(), '->load() throws an InvalidArgumentException if an extension is not loaded');
  111. }
  112. }
  113. }
  114. class ProjectLoader3 extends YamlFileLoader
  115. {
  116. public function loadFile($file)
  117. {
  118. return parent::loadFile($file);
  119. }
  120. }