PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

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