FileLoaderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Config\Tests\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Config\Loader\FileLoader;
  14. use Symfony\Component\Config\Loader\LoaderResolver;
  15. class FileLoaderTest extends TestCase
  16. {
  17. public function testImportWithFileLocatorDelegation()
  18. {
  19. $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
  20. $locatorMockForAdditionalLoader = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
  21. $locatorMockForAdditionalLoader->expects($this->any())->method('locate')->will($this->onConsecutiveCalls(
  22. ['path/to/file1'], // Default
  23. ['path/to/file1', 'path/to/file2'], // First is imported
  24. ['path/to/file1', 'path/to/file2'], // Second is imported
  25. ['path/to/file1'], // Exception
  26. ['path/to/file1', 'path/to/file2'] // Exception
  27. ));
  28. $fileLoader = new TestFileLoader($locatorMock);
  29. $fileLoader->setSupports(false);
  30. $fileLoader->setCurrentDir('.');
  31. $additionalLoader = new TestFileLoader($locatorMockForAdditionalLoader);
  32. $additionalLoader->setCurrentDir('.');
  33. $fileLoader->setResolver($loaderResolver = new LoaderResolver([$fileLoader, $additionalLoader]));
  34. // Default case
  35. $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
  36. // Check first file is imported if not already loading
  37. $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
  38. // Check second file is imported if first is already loading
  39. $fileLoader->addLoading('path/to/file1');
  40. $this->assertSame('path/to/file2', $fileLoader->import('my_resource'));
  41. // Check exception throws if first (and only available) file is already loading
  42. try {
  43. $fileLoader->import('my_resource');
  44. $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  45. } catch (\Exception $e) {
  46. $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  47. }
  48. // Check exception throws if all files are already loading
  49. try {
  50. $fileLoader->addLoading('path/to/file2');
  51. $fileLoader->import('my_resource');
  52. $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  53. } catch (\Exception $e) {
  54. $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  55. }
  56. }
  57. public function testImportWithGlobLikeResource()
  58. {
  59. $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
  60. $loader = new TestFileLoader($locatorMock);
  61. $this->assertSame('[foo]', $loader->import('[foo]'));
  62. }
  63. public function testImportWithNoGlobMatch()
  64. {
  65. $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
  66. $loader = new TestFileLoader($locatorMock);
  67. $this->assertNull($loader->import('./*.abc'));
  68. }
  69. public function testImportWithSimpleGlob()
  70. {
  71. $loader = new TestFileLoader(new FileLocator(__DIR__));
  72. $this->assertSame(__FILE__, strtr($loader->import('FileLoaderTest.*'), '/', \DIRECTORY_SEPARATOR));
  73. }
  74. }
  75. class TestFileLoader extends FileLoader
  76. {
  77. private $supports = true;
  78. public function load($resource, $type = null)
  79. {
  80. return $resource;
  81. }
  82. public function supports($resource, $type = null)
  83. {
  84. return $this->supports;
  85. }
  86. public function addLoading($resource)
  87. {
  88. self::$loading[$resource] = true;
  89. }
  90. public function removeLoading($resource)
  91. {
  92. unset(self::$loading[$resource]);
  93. }
  94. public function clearLoading()
  95. {
  96. self::$loading = [];
  97. }
  98. public function setSupports($supports)
  99. {
  100. $this->supports = $supports;
  101. }
  102. }