PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 243 lines | 192 code | 31 blank | 20 comment | 0 complexity | a13cdeea01abd57f6d166682c175c9dd 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\Config\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\PhpFileLoader;
  18. use Symfony\Component\Config\Loader\LoaderResolver;
  19. use Symfony\Component\Config\FileLocator;
  20. use Symfony\Component\ExpressionLanguage\Expression;
  21. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  22. {
  23. protected static $fixturesPath;
  24. public static function setUpBeforeClass()
  25. {
  26. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  27. require_once self::$fixturesPath.'/includes/foo.php';
  28. require_once self::$fixturesPath.'/includes/ProjectExtension.php';
  29. }
  30. public function testLoadFile()
  31. {
  32. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
  33. $r = new \ReflectionObject($loader);
  34. $m = $r->getMethod('loadFile');
  35. $m->setAccessible(true);
  36. try {
  37. $m->invoke($loader, 'foo.yml');
  38. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  39. } catch (\Exception $e) {
  40. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  41. $this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  42. }
  43. try {
  44. $m->invoke($loader, 'parameters.ini');
  45. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  46. } catch (\Exception $e) {
  47. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
  48. $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');
  49. }
  50. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  51. foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
  52. try {
  53. $m->invoke($loader, $fixture.'.yml');
  54. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
  55. } catch (\Exception $e) {
  56. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
  57. $this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
  58. }
  59. }
  60. }
  61. /**
  62. * @dataProvider provideInvalidFiles
  63. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  64. */
  65. public function testLoadInvalidFile($file)
  66. {
  67. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  68. $loader->load($file.'.yml');
  69. }
  70. public function provideInvalidFiles()
  71. {
  72. return array(
  73. array('bad_parameters'),
  74. array('bad_imports'),
  75. array('bad_import'),
  76. array('bad_services'),
  77. array('bad_service'),
  78. array('bad_calls'),
  79. );
  80. }
  81. public function testLoadParameters()
  82. {
  83. $container = new ContainerBuilder();
  84. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  85. $loader->load('services2.yml');
  86. $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');
  87. }
  88. public function testLoadImports()
  89. {
  90. $container = new ContainerBuilder();
  91. $resolver = new LoaderResolver(array(
  92. new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  93. new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  94. new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
  95. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  96. ));
  97. $loader->setResolver($resolver);
  98. $loader->load('services4.yml');
  99. $actual = $container->getParameterBag()->all();
  100. $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);
  101. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  102. // Bad import throws no exception due to ignore_errors value.
  103. $loader->load('services4_bad_import.yml');
  104. }
  105. public function testLoadServices()
  106. {
  107. $container = new ContainerBuilder();
  108. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  109. $loader->load('services6.yml');
  110. $services = $container->getDefinitions();
  111. $this->assertTrue(isset($services['foo']), '->load() parses service elements');
  112. $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts service element to Definition instances');
  113. $this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
  114. $this->assertEquals('container', $services['scope.container']->getScope());
  115. $this->assertEquals('custom', $services['scope.custom']->getScope());
  116. $this->assertEquals('prototype', $services['scope.prototype']->getScope());
  117. $this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory_method attribute');
  118. $this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
  119. $this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
  120. $this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
  121. $this->assertEquals(array(new Reference('baz'), 'configure'), $services['configurator2']->getConfigurator(), '->load() parses the configurator tag');
  122. $this->assertEquals(array('BazClass', 'configureStatic'), $services['configurator3']->getConfigurator(), '->load() parses the configurator tag');
  123. $this->assertEquals(array(array('setBar', array()), array('setBar', array()), array('setBar', array(new Expression('service("foo").foo() ~ (container.hasparameter("foo") ? parameter("foo") : "default")')))), $services['method_call1']->getMethodCalls(), '->load() parses the method_call tag');
  124. $this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
  125. $this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());
  126. $this->assertTrue($services['request']->isSynthetic(), '->load() parses the synthetic flag');
  127. $this->assertTrue($services['request']->isSynchronized(), '->load() parses the synchronized flag');
  128. $this->assertTrue($services['request']->isLazy(), '->load() parses the lazy flag');
  129. $aliases = $container->getAliases();
  130. $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
  131. $this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
  132. $this->assertTrue($aliases['alias_for_foo']->isPublic());
  133. $this->assertTrue(isset($aliases['another_alias_for_foo']));
  134. $this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
  135. $this->assertFalse($aliases['another_alias_for_foo']->isPublic());
  136. $this->assertNull($services['request']->getDecoratedService());
  137. $this->assertEquals(array('decorated', null), $services['decorator_service']->getDecoratedService());
  138. $this->assertEquals(array('decorated', 'decorated.pif-pouf'), $services['decorator_service_with_name']->getDecoratedService());
  139. }
  140. public function testExtensions()
  141. {
  142. $container = new ContainerBuilder();
  143. $container->registerExtension(new \ProjectExtension());
  144. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  145. $loader->load('services10.yml');
  146. $container->compile();
  147. $services = $container->getDefinitions();
  148. $parameters = $container->getParameterBag()->all();
  149. $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  150. $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  151. $this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
  152. $this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  153. try {
  154. $loader->load('services11.yml');
  155. $this->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  156. } catch (\Exception $e) {
  157. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
  158. $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');
  159. }
  160. }
  161. /**
  162. * @covers Symfony\Component\DependencyInjection\Loader\YamlFileLoader::supports
  163. */
  164. public function testSupports()
  165. {
  166. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
  167. $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
  168. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  169. }
  170. public function testNonArrayTagsThrowsException()
  171. {
  172. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  173. try {
  174. $loader->load('badtag1.yml');
  175. $this->fail('->load() should throw an exception when the tags key of a service is not an array');
  176. } catch (\Exception $e) {
  177. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array');
  178. $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array');
  179. }
  180. }
  181. /**
  182. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  183. * @expectedExceptionMessage A "tags" entry must be an array for service
  184. */
  185. public function testNonArrayTagThrowsException()
  186. {
  187. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  188. $loader->load('badtag4.yml');
  189. }
  190. public function testTagWithoutNameThrowsException()
  191. {
  192. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  193. try {
  194. $loader->load('badtag2.yml');
  195. $this->fail('->load() should throw an exception when a tag is missing the name key');
  196. } catch (\Exception $e) {
  197. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key');
  198. $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');
  199. }
  200. }
  201. public function testTagWithAttributeArrayThrowsException()
  202. {
  203. $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
  204. try {
  205. $loader->load('badtag3.yml');
  206. $this->fail('->load() should throw an exception when a tag-attribute is not a scalar');
  207. } catch (\Exception $e) {
  208. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
  209. $this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service "foo_service", tag "foo", attribute "bar"', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
  210. }
  211. }
  212. }