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\FileLocator;
16 use Symfony\Component\Config\Loader\FileLoader;
17 use Symfony\Component\Config\Loader\LoaderResolver;
19 class FileLoaderTest extends TestCase
21 public function testImportWithFileLocatorDelegation()
23 $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
25 $locatorMockForAdditionalLoader = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
26 $locatorMockForAdditionalLoader->expects($this->any())->method('locate')->will($this->onConsecutiveCalls(
27 array('path/to/file1'), // Default
28 array('path/to/file1', 'path/to/file2'), // First is imported
29 array('path/to/file1', 'path/to/file2'), // Second is imported
30 array('path/to/file1'), // Exception
31 array('path/to/file1', 'path/to/file2') // Exception
34 $fileLoader = new TestFileLoader($locatorMock);
35 $fileLoader->setSupports(false);
36 $fileLoader->setCurrentDir('.');
38 $additionalLoader = new TestFileLoader($locatorMockForAdditionalLoader);
39 $additionalLoader->setCurrentDir('.');
41 $fileLoader->setResolver($loaderResolver = new LoaderResolver(array($fileLoader, $additionalLoader)));
44 $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
46 // Check first file is imported if not already loading
47 $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
49 // Check second file is imported if first is already loading
50 $fileLoader->addLoading('path/to/file1');
51 $this->assertSame('path/to/file2', $fileLoader->import('my_resource'));
53 // Check exception throws if first (and only available) file is already loading
55 $fileLoader->import('my_resource');
56 $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
57 } catch (\Exception $e) {
58 $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
61 // Check exception throws if all files are already loading
63 $fileLoader->addLoading('path/to/file2');
64 $fileLoader->import('my_resource');
65 $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
66 } catch (\Exception $e) {
67 $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
71 public function testImportWithGlobLikeResource()
73 $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
74 $loader = new TestFileLoader($locatorMock);
76 $this->assertSame('[foo]', $loader->import('[foo]'));
79 public function testImportWithNoGlobMatch()
81 $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
82 $loader = new TestFileLoader($locatorMock);
84 $this->assertNull($loader->import('./*.abc'));
87 public function testImportWithSimpleGlob()
89 $loader = new TestFileLoader(new FileLocator(__DIR__));
91 $this->assertSame(__FILE__, strtr($loader->import('FileLoaderTest.*'), '/', DIRECTORY_SEPARATOR));
95 class TestFileLoader extends FileLoader
97 private $supports = true;
99 public function load($resource, $type = null)
104 public function supports($resource, $type = null)
106 return $this->supports;
109 public function addLoading($resource)
111 self::$loading[$resource] = true;
114 public function removeLoading($resource)
116 unset(self::$loading[$resource]);
119 public function clearLoading()
121 self::$loading = array();
124 public function setSupports($supports)
126 $this->supports = $supports;