YamlFileLoaderTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection\Loader;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Loader\Loader;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  17. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
  19. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  20. {
  21. static protected $fixturesPath;
  22. static public function setUpBeforeClass()
  23. {
  24. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  25. require_once self::$fixturesPath.'/includes/foo.php';
  26. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  27. }
  28. public function testLoadFile()
  29. {
  30. $loader = new ProjectLoader3(new ContainerBuilder(), self::$fixturesPath.'/ini');
  31. try {
  32. $loader->loadFile('foo.yml');
  33. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  34. } catch (\Exception $e) {
  35. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  36. $this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  37. }
  38. try {
  39. $loader->loadFile('parameters.ini');
  40. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  41. } catch (\Exception $e) {
  42. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  43. $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');
  44. }
  45. $loader = new ProjectLoader3(new ContainerBuilder(), self::$fixturesPath.'/yaml');
  46. foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
  47. try {
  48. $loader->loadFile($fixture.'.yml');
  49. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  50. } catch (\Exception $e) {
  51. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
  52. $this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
  53. }
  54. }
  55. }
  56. public function testLoadParameters()
  57. {
  58. $container = new ContainerBuilder();
  59. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  60. $loader->load('services2.yml');
  61. $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');
  62. }
  63. public function testLoadImports()
  64. {
  65. $container = new ContainerBuilder();
  66. $resolver = new LoaderResolver(array(
  67. new IniFileLoader($container, self::$fixturesPath.'/yaml'),
  68. new XmlFileLoader($container, self::$fixturesPath.'/yaml'),
  69. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml'),
  70. ));
  71. $loader->setResolver($resolver);
  72. $loader->load('services4.yml');
  73. $actual = $container->getParameterBag()->all();
  74. $expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'foo_bar' => new Reference('foo_bar'), 'imported_from_ini' => true, 'imported_from_xml' => true);
  75. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  76. }
  77. public function testLoadServices()
  78. {
  79. $container = new ContainerBuilder();
  80. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  81. $loader->load('services6.yml');
  82. $services = $container->getDefinitions();
  83. $this->assertTrue(isset($services['foo']), '->load() parses service elements');
  84. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts service element to Definition instances');
  85. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  86. $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
  87. $this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
  88. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory_method attribute');
  89. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  90. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  91. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  92. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  93. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  94. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  95. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  96. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  97. $aliases = $container->getAliases();
  98. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
  99. $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
  100. $this->assertTrue($aliases['alias_for_foo']->isPublic());
  101. $this->assertTrue(isset($aliases['another_alias_for_foo']));
  102. $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
  103. $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
  104. }
  105. public function testExtensions()
  106. {
  107. $container = new ContainerBuilder();
  108. $container->registerExtension(new \ProjectExtension());
  109. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  110. $loader->load('services10.yml');
  111. $container->compile();
  112. $services = $container->getDefinitions();
  113. $parameters = $container->getParameterBag()->all();
  114. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  115. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  116. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  117. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  118. try {
  119. $loader->load('services11.yml');
  120. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  121. } catch (\Exception $e) {
  122. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  123. $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');
  124. }
  125. try {
  126. $loader->load('services12.yml');
  127. $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded');
  128. } catch (\Exception $e) {
  129. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if an extension is not loaded');
  130. $this->assertStringStartsWith('The "foobar" tag is not valid (in', $e->getMessage(), '->load() throws an InvalidArgumentException if an extension is not loaded');
  131. }
  132. }
  133. /**
  134. * @covers Symfony\Component\DependencyInjection\Loader\YamlFileLoader::supports
  135. */
  136. public function testSupports()
  137. {
  138. $loader = new YamlFileLoader(new ContainerBuilder());
  139. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  140. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  141. }
  142. public function testLoadInterfaceInjectors()
  143. {
  144. $container = new ContainerBuilder();
  145. $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml');
  146. $loader->load('interfaces1.yml');
  147. $interfaces = $container->getInterfaceInjectors('FooClass');
  148. $this->assertEquals(1, count($interfaces), '->load() parses interfaces');
  149. $interface = $interfaces['FooClass'];
  150. $this->assertTrue($interface->hasMethodCall('setBar'), '->load() parses interfaces elements');
  151. }
  152. }
  153. class ProjectLoader3 extends YamlFileLoader
  154. {
  155. public function loadFile($file)
  156. {
  157. return parent::loadFile($file);
  158. }
  159. }