YamlFileLoaderTest.php 7.3 KB

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