4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Loader;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Loader\FileLoader;
16 use Symfony\Component\Config\Loader\LoaderResolver;
18 class FileLoaderTest extends TestCase
20 public function testImportWithFileLocatorDelegation()
22 $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
24 $locatorMockForAdditionalLoader = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
25 $locatorMockForAdditionalLoader->expects($this->any())->method('locate')->will($this->onConsecutiveCalls(
26 array('path/to/file1'), // Default
27 array('path/to/file1', 'path/to/file2'), // First is imported
28 array('path/to/file1', 'path/to/file2'), // Second is imported
29 array('path/to/file1'), // Exception
30 array('path/to/file1', 'path/to/file2') // Exception
33 $fileLoader = new TestFileLoader($locatorMock);
34 $fileLoader->setSupports(false);
35 $fileLoader->setCurrentDir('.');
37 $additionalLoader = new TestFileLoader($locatorMockForAdditionalLoader);
38 $additionalLoader->setCurrentDir('.');
40 $fileLoader->setResolver($loaderResolver = new LoaderResolver(array($fileLoader, $additionalLoader)));
43 $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
45 // Check first file is imported if not already loading
46 $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
48 // Check second file is imported if first is already loading
49 $fileLoader->addLoading('path/to/file1');
50 $this->assertSame('path/to/file2', $fileLoader->import('my_resource'));
52 // Check exception throws if first (and only available) file is already loading
54 $fileLoader->import('my_resource');
55 $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
56 } catch (\Exception $e) {
57 $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
60 // Check exception throws if all files are already loading
62 $fileLoader->addLoading('path/to/file2');
63 $fileLoader->import('my_resource');
64 $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
65 } catch (\Exception $e) {
66 $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
71 class TestFileLoader extends FileLoader
73 private $supports = true;
75 public function load($resource, $type = null)
80 public function supports($resource, $type = null)
82 return $this->supports;
85 public function addLoading($resource)
87 self::$loading[$resource] = true;
90 public function removeLoading($resource)
92 unset(self::$loading[$resource]);
95 public function clearLoading()
97 self::$loading = array();
100 public function setSupports($supports)
102 $this->supports = $supports;