YamlFileLoaderTest.php 5.8 KB

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