YamlFileLoaderTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Component\DependencyInjection\Loader;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Loader\Loader;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  16. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  17. use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
  18. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. static protected $fixturesPath;
  21. static public function setUpBeforeClass()
  22. {
  23. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  24. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  25. }
  26. public function testLoadFile()
  27. {
  28. $loader = new ProjectLoader3(new ContainerBuilder(), self::$fixturesPath.'/ini');
  29. try {
  30. $loader->loadFile('foo.yml');
  31. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  32. } catch (\Exception $e) {
  33. $this->assertInstanceOf('\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. $loader->loadFile('parameters.ini');
  38. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  39. } catch (\Exception $e) {
  40. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  41. $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');
  42. }
  43. $loader = new ProjectLoader3(new ContainerBuilder(), self::$fixturesPath.'/yaml');
  44. foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
  45. try {
  46. $loader->loadFile($fixture.'.yml');
  47. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  48. } catch (\Exception $e) {
  49. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
  50. $this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
  51. }
  52. }
  53. }
  54. public function testLoadParameters()
  55. {
  56. $container = new ContainerBuilder();
  57. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  58. $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')), $container->getParameterBag()->all(), '->load() converts YAML keys to lowercase');
  60. }
  61. public function testLoadImports()
  62. {
  63. $container = new ContainerBuilder();
  64. $resolver = new LoaderResolver(array(
  65. new IniFileLoader($container, self::$fixturesPath.'/yaml'),
  66. new XmlFileLoader($container, self::$fixturesPath.'/yaml'),
  67. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml'),
  68. ));
  69. $loader->setResolver($resolver);
  70. $loader->load('services4.yml');
  71. $actual = $container->getParameterBag()->all();
  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. $container = new ContainerBuilder();
  78. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  79. $loader->load('services6.yml');
  80. $services = $container->getDefinitions();
  81. $this->assertTrue(isset($services['foo']), '->load() parses service elements');
  82. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts service element to Definition instances');
  83. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  84. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  85. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  86. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory_method attribute');
  87. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  88. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  89. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  90. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  91. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  92. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  93. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  94. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  95. $aliases = $container->getAliases();
  96. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
  97. $this->assertEquals('foo', $aliases['alias_for_foo'], '->load() parses aliases');
  98. }
  99. public function testExtensions()
  100. {
  101. $container = new ContainerBuilder();
  102. $container->registerExtension(new \ProjectExtension());
  103. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  104. $loader->load('services10.yml');
  105. $container->freeze();
  106. $services = $container->getDefinitions();
  107. $parameters = $container->getParameterBag()->all();
  108. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  109. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  110. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  111. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  112. try {
  113. $loader->load('services11.yml');
  114. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  115. } catch (\Exception $e) {
  116. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  117. $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');
  118. }
  119. try {
  120. $loader->load('services12.yml');
  121. $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  122. } catch (\Exception $e) {
  123. $this->assertInstanceOf('\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. * @covers Symfony\Component\DependencyInjection\Loader\YamlFileLoader::supports
  129. */
  130. public function testSupports()
  131. {
  132. $loader = new YamlFileLoader(new ContainerBuilder());
  133. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  134. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  135. }
  136. }
  137. class ProjectLoader3 extends YamlFileLoader
  138. {
  139. public function loadFile($file)
  140. {
  141. return parent::loadFile($file);
  142. }
  143. }