YamlFileLoaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Config\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\Config\Loader\LoaderResolver;
  19. use Symfony\Component\Config\FileLocator;
  20. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  21. {
  22. static protected $fixturesPath;
  23. static public function setUpBeforeClass()
  24. {
  25. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  26. require_once self::$fixturesPath.'/includes/foo.php';
  27. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  28. }
  29. public function testLoadFile()
  30. {
  31. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
  32. $r = new \ReflectionObject($loader);
  33. $m = $r->getMethod('loadFile');
  34. $m->setAccessible(true);
  35. try {
  36. $m->invoke($loader, 'foo.yml');
  37. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  38. } catch (\Exception $e) {
  39. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  40. $this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  41. }
  42. try {
  43. $m->invoke($loader, 'parameters.ini');
  44. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  45. } catch (\Exception $e) {
  46. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  47. $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');
  48. }
  49. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  50. foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
  51. try {
  52. $m->invoke($loader, $fixture.'.yml');
  53. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  54. } catch (\Exception $e) {
  55. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
  56. $this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
  57. }
  58. }
  59. }
  60. public function testLoadParameters()
  61. {
  62. $container = new ContainerBuilder();
  63. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  64. $loader->load('services2.yml');
  65. $this->assertEquals(array('foo' => 'bar', 'mixedcase' => array('MixedCaseKey' => 'value'), 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), $container->getParameterBag()->all(), '->load() converts YAML keys to lowercase');
  66. }
  67. public function testLoadImports()
  68. {
  69. $container = new ContainerBuilder();
  70. $resolver = new LoaderResolver(array(
  71. new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  72. new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  73. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  74. ));
  75. $loader->setResolver($resolver);
  76. $loader->load('services4.yml');
  77. $actual = $container->getParameterBag()->all();
  78. $expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
  79. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  80. // Bad import throws no exception due to ignore_errors value.
  81. $loader->load('services4_bad_import.yml');
  82. }
  83. public function testLoadServices()
  84. {
  85. $container = new ContainerBuilder();
  86. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  87. $loader->load('services6.yml');
  88. $services = $container->getDefinitions();
  89. $this->assertTrue(isset($services['foo']), '->load() parses service elements');
  90. $this->assertEquals('Symfony\\Component\\DependencyInjection\\Definition', get_class($services['foo']), '->load() converts service element to Definition instances');
  91. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  92. $this->assertEquals('container', $services['scope.container']->getScope());
  93. $this->assertEquals('custom', $services['scope.custom']->getScope());
  94. $this->assertEquals('prototype', $services['scope.prototype']->getScope());
  95. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory_method attribute');
  96. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  97. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  98. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  99. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  100. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  101. $this->assertEquals(array(array('setBar', array())), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  102. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  103. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  104. $aliases = $container->getAliases();
  105. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
  106. $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
  107. $this->assertTrue($aliases['alias_for_foo']->isPublic());
  108. $this->assertTrue(isset($aliases['another_alias_for_foo']));
  109. $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
  110. $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
  111. }
  112. public function testExtensions()
  113. {
  114. $container = new ContainerBuilder();
  115. $container->registerExtension(new \ProjectExtension());
  116. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  117. $loader->load('services10.yml');
  118. $container->compile();
  119. $services = $container->getDefinitions();
  120. $parameters = $container->getParameterBag()->all();
  121. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  122. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  123. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  124. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  125. try {
  126. $loader->load('services11.yml');
  127. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  128. } catch (\Exception $e) {
  129. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  130. $this->assertStringStartsWith('There is no extension able to load the configuration for "foobarfoobar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  131. }
  132. }
  133. /**
  134. * @covers Symfony\Component\DependencyInjection\Loader\YamlFileLoader::supports
  135. */
  136. public function testSupports()
  137. {
  138. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
  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 YamlFileLoader($container, new FileLocator(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. public function testNonArrayTagThrowsException()
  153. {
  154. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  155. try {
  156. $loader->load('badtag1.yml');
  157. $this->fail('->load() should throw an exception when the tags key of a service is not an array');
  158. } catch (\Exception $e) {
  159. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array');
  160. $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array');
  161. }
  162. }
  163. public function testTagWithoutNameThrowsException()
  164. {
  165. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  166. try {
  167. $loader->load('badtag2.yml');
  168. $this->fail('->load() should throw an exception when a tag is missing the name key');
  169. } catch (\Exception $e) {
  170. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key');
  171. $this->assertStringStartsWith('A "tags" entry is missing a "name" key must be an array for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is missing the name key');
  172. }
  173. }
  174. }