DirectoryLoaderTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Loader\DirectoryLoader;
  16. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  17. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  19. class DirectoryLoaderTest extends TestCase
  20. {
  21. private static $fixturesPath;
  22. private $container;
  23. private $loader;
  24. public static function setUpBeforeClass()
  25. {
  26. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  27. }
  28. protected function setUp()
  29. {
  30. $locator = new FileLocator(self::$fixturesPath);
  31. $this->container = new ContainerBuilder();
  32. $this->loader = new DirectoryLoader($this->container, $locator);
  33. $resolver = new LoaderResolver(array(
  34. new PhpFileLoader($this->container, $locator),
  35. new IniFileLoader($this->container, $locator),
  36. new YamlFileLoader($this->container, $locator),
  37. $this->loader,
  38. ));
  39. $this->loader->setResolver($resolver);
  40. }
  41. public function testDirectoryCanBeLoadedRecursively()
  42. {
  43. $this->loader->load('directory/');
  44. $this->assertEquals(array('ini' => 'ini', 'yaml' => 'yaml', 'php' => 'php'), $this->container->getParameterBag()->all(), '->load() takes a single directory');
  45. }
  46. public function testImports()
  47. {
  48. $this->loader->resolve('directory/import/import.yml')->load('directory/import/import.yml');
  49. $this->assertEquals(array('ini' => 'ini', 'yaml' => 'yaml'), $this->container->getParameterBag()->all(), '->load() takes a single file that imports a directory');
  50. }
  51. /**
  52. * @expectedException \InvalidArgumentException
  53. * @expectedExceptionMessage The file "foo" does not exist (in:
  54. */
  55. public function testExceptionIsRaisedWhenDirectoryDoesNotExist()
  56. {
  57. $this->loader->load('foo/');
  58. }
  59. public function testSupports()
  60. {
  61. $loader = new DirectoryLoader(new ContainerBuilder(), new FileLocator());
  62. $this->assertTrue($loader->supports('directory/'), '->supports("directory/") returns true');
  63. $this->assertTrue($loader->supports('directory/', 'directory'), '->supports("directory/", "directory") returns true');
  64. $this->assertFalse($loader->supports('directory'), '->supports("directory") returns false');
  65. $this->assertTrue($loader->supports('directory', 'directory'), '->supports("directory", "directory") returns true');
  66. $this->assertFalse($loader->supports('directory', 'foo'), '->supports("directory", "foo") returns false');
  67. }
  68. }