Yaffs site version 1.1
[yaffs-website] / vendor / symfony / config / Tests / Loader / FileLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Tests\Loader;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Loader\FileLoader;
16 use Symfony\Component\Config\Loader\LoaderResolver;
17
18 class FileLoaderTest extends TestCase
19 {
20     public function testImportWithFileLocatorDelegation()
21     {
22         $locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
23
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
31                 ));
32
33         $fileLoader = new TestFileLoader($locatorMock);
34         $fileLoader->setSupports(false);
35         $fileLoader->setCurrentDir('.');
36
37         $additionalLoader = new TestFileLoader($locatorMockForAdditionalLoader);
38         $additionalLoader->setCurrentDir('.');
39
40         $fileLoader->setResolver($loaderResolver = new LoaderResolver(array($fileLoader, $additionalLoader)));
41
42         // Default case
43         $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
44
45         // Check first file is imported if not already loading
46         $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
47
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'));
51
52         // Check exception throws if first (and only available) file is already loading
53         try {
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');
58         }
59
60         // Check exception throws if all files are already loading
61         try {
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');
67         }
68     }
69 }
70
71 class TestFileLoader extends FileLoader
72 {
73     private $supports = true;
74
75     public function load($resource, $type = null)
76     {
77         return $resource;
78     }
79
80     public function supports($resource, $type = null)
81     {
82         return $this->supports;
83     }
84
85     public function addLoading($resource)
86     {
87         self::$loading[$resource] = true;
88     }
89
90     public function removeLoading($resource)
91     {
92         unset(self::$loading[$resource]);
93     }
94
95     public function clearLoading()
96     {
97         self::$loading = array();
98     }
99
100     public function setSupports($supports)
101     {
102         $this->supports = $supports;
103     }
104 }