FileLoaderTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Config\Loader\LoaderResolver;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  17. use Symfony\Component\DependencyInjection\Loader\FileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  19. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  20. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  21. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  22. use Symfony\Component\DependencyInjection\Reference;
  23. use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\AnotherSub\DeeperBaz;
  24. use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Baz;
  25. use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar;
  26. class FileLoaderTest extends TestCase
  27. {
  28. protected static $fixturesPath;
  29. public static function setUpBeforeClass()
  30. {
  31. self::$fixturesPath = realpath(__DIR__.'/../');
  32. }
  33. public function testImportWithGlobPattern()
  34. {
  35. $container = new ContainerBuilder();
  36. $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath));
  37. $resolver = new LoaderResolver(array(
  38. new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
  39. new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
  40. new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
  41. new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
  42. ));
  43. $loader->setResolver($resolver);
  44. $loader->import('{F}ixtures/{xml,yaml}/services2.{yml,xml}');
  45. $actual = $container->getParameterBag()->all();
  46. $expected = array(
  47. 'a string',
  48. 'foo' => 'bar',
  49. 'values' => array(
  50. 0,
  51. 'integer' => 4,
  52. 100 => null,
  53. 'true',
  54. true,
  55. false,
  56. 'on',
  57. 'off',
  58. 'float' => 1.3,
  59. 1000.3,
  60. 'a string',
  61. array('foo', 'bar'),
  62. ),
  63. 'mixedcase' => array('MixedCaseKey' => 'value'),
  64. 'constant' => PHP_EOL,
  65. 'bar' => '%foo%',
  66. 'escape' => '@escapeme',
  67. 'foo_bar' => new Reference('foo_bar'),
  68. );
  69. $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
  70. }
  71. public function testRegisterClasses()
  72. {
  73. $container = new ContainerBuilder();
  74. $container->setParameter('sub_dir', 'Sub');
  75. $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
  76. $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
  77. $this->assertEquals(
  78. array('service_container', Bar::class),
  79. array_keys($container->getDefinitions())
  80. );
  81. }
  82. public function testRegisterClassesWithExclude()
  83. {
  84. $container = new ContainerBuilder();
  85. $container->setParameter('other_dir', 'OtherDir');
  86. $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
  87. $loader->registerClasses(
  88. new Definition(),
  89. 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
  90. 'Prototype/*',
  91. // load everything, except OtherDir/AnotherSub & Foo.php
  92. 'Prototype/{%other_dir%/AnotherSub,Foo.php}'
  93. );
  94. $this->assertTrue($container->has(Bar::class));
  95. $this->assertTrue($container->has(Baz::class));
  96. $this->assertFalse($container->has(Foo::class));
  97. $this->assertFalse($container->has(DeeperBaz::class));
  98. }
  99. /**
  100. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  101. * @expectedExceptionMessageRegExp /Expected to find class "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Prototype\\Bar" in file ".+" while importing services from resource "Prototype\/Sub\/\*", but it was not found\! Check the namespace prefix used with the resource/
  102. */
  103. public function testRegisterClassesWithBadPrefix()
  104. {
  105. $container = new ContainerBuilder();
  106. $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
  107. // the Sub is missing from namespace prefix
  108. $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*');
  109. }
  110. /**
  111. * @dataProvider getIncompatibleExcludeTests
  112. */
  113. public function testRegisterClassesWithIncompatibleExclude($resourcePattern, $excludePattern)
  114. {
  115. $container = new ContainerBuilder();
  116. $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
  117. try {
  118. $loader->registerClasses(
  119. new Definition(),
  120. 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
  121. $resourcePattern,
  122. $excludePattern
  123. );
  124. } catch (InvalidArgumentException $e) {
  125. $this->assertEquals(
  126. sprintf('Invalid "exclude" pattern when importing classes for "Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s)', $excludePattern, $resourcePattern),
  127. $e->getMessage()
  128. );
  129. }
  130. }
  131. public function getIncompatibleExcludeTests()
  132. {
  133. yield array('Prototype/*', 'yaml/*', false);
  134. yield array('Prototype/OtherDir/*', 'Prototype/*', false);
  135. }
  136. }
  137. class TestFileLoader extends FileLoader
  138. {
  139. public function load($resource, $type = null)
  140. {
  141. return $resource;
  142. }
  143. public function supports($resource, $type = null)
  144. {
  145. return false;
  146. }
  147. }