123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace Symfony\Component\Config\Tests\Loader;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Config\Loader\FileLoader;
- use Symfony\Component\Config\Loader\LoaderResolver;
- class FileLoaderTest extends TestCase
- {
- public function testImportWithFileLocatorDelegation()
- {
- $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
- $locatorMockForAdditionalLoader = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
- $locatorMockForAdditionalLoader->expects($this->any())->method('locate')->will($this->onConsecutiveCalls(
- array('path/to/file1'),
- array('path/to/file1', 'path/to/file2'),
- array('path/to/file1', 'path/to/file2'),
- array('path/to/file1'),
- array('path/to/file1', 'path/to/file2')
- ));
- $fileLoader = new TestFileLoader($locatorMock);
- $fileLoader->setSupports(false);
- $fileLoader->setCurrentDir('.');
- $additionalLoader = new TestFileLoader($locatorMockForAdditionalLoader);
- $additionalLoader->setCurrentDir('.');
- $fileLoader->setResolver($loaderResolver = new LoaderResolver(array($fileLoader, $additionalLoader)));
-
- $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
-
- $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
-
- $fileLoader->addLoading('path/to/file1');
- $this->assertSame('path/to/file2', $fileLoader->import('my_resource'));
-
- try {
- $fileLoader->import('my_resource');
- $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
- }
-
- try {
- $fileLoader->addLoading('path/to/file2');
- $fileLoader->import('my_resource');
- $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
- }
- }
- }
- class TestFileLoader extends FileLoader
- {
- private $supports = true;
- public function load($resource, $type = null)
- {
- return $resource;
- }
- public function supports($resource, $type = null)
- {
- return $this->supports;
- }
- public function addLoading($resource)
- {
- self::$loading[$resource] = true;
- }
- public function removeLoading($resource)
- {
- unset(self::$loading[$resource]);
- }
- public function clearLoading()
- {
- self::$loading = array();
- }
- public function setSupports($supports)
- {
- $this->supports = $supports;
- }
- }
|