YamlFileLoaderTest.php 5.8 KB

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