Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / config / Tests / ResourceCheckerConfigCacheTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Config\ResourceCheckerConfigCache;
17 use Symfony\Component\Config\Tests\Resource\ResourceStub;
18
19 class ResourceCheckerConfigCacheTest extends TestCase
20 {
21     private $cacheFile = null;
22
23     protected function setUp()
24     {
25         $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
26     }
27
28     protected function tearDown()
29     {
30         $files = array($this->cacheFile, "{$this->cacheFile}.meta");
31
32         foreach ($files as $file) {
33             if (file_exists($file)) {
34                 unlink($file);
35             }
36         }
37     }
38
39     public function testGetPath()
40     {
41         $cache = new ResourceCheckerConfigCache($this->cacheFile);
42
43         $this->assertSame($this->cacheFile, $cache->getPath());
44     }
45
46     public function testCacheIsNotFreshIfEmpty()
47     {
48         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock()
49             ->expects($this->never())->method('supports');
50
51         /* If there is nothing in the cache, it needs to be filled (and thus it's not fresh).
52             It does not matter if you provide checkers or not. */
53
54         unlink($this->cacheFile); // remove tempnam() side effect
55         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
56
57         $this->assertFalse($cache->isFresh());
58     }
59
60     public function testCacheIsFreshIfNoCheckerProvided()
61     {
62         /* For example in prod mode, you may choose not to run any checkers
63            at all. In that case, the cache should always be considered fresh. */
64         $cache = new ResourceCheckerConfigCache($this->cacheFile);
65         $this->assertTrue($cache->isFresh());
66     }
67
68     public function testCacheIsFreshIfEmptyCheckerIteratorProvided()
69     {
70         $cache = new ResourceCheckerConfigCache($this->cacheFile, new \ArrayIterator(array()));
71         $this->assertTrue($cache->isFresh());
72     }
73
74     public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
75     {
76         /* As in the previous test, but this time we have a resource. */
77         $cache = new ResourceCheckerConfigCache($this->cacheFile);
78         $cache->write('', array(new ResourceStub()));
79
80         $this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
81     }
82
83     public function testIsFreshWithchecker()
84     {
85         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
86
87         $checker->expects($this->once())
88                   ->method('supports')
89                   ->willReturn(true);
90
91         $checker->expects($this->once())
92                   ->method('isFresh')
93                   ->willReturn(true);
94
95         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
96         $cache->write('', array(new ResourceStub()));
97
98         $this->assertTrue($cache->isFresh());
99     }
100
101     public function testIsNotFreshWithchecker()
102     {
103         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
104
105         $checker->expects($this->once())
106                   ->method('supports')
107                   ->willReturn(true);
108
109         $checker->expects($this->once())
110                   ->method('isFresh')
111                   ->willReturn(false);
112
113         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
114         $cache->write('', array(new ResourceStub()));
115
116         $this->assertFalse($cache->isFresh());
117     }
118
119     public function testCacheIsNotFreshWhenUnserializeFails()
120     {
121         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
122         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
123         $cache->write('foo', array(new FileResource(__FILE__)));
124
125         $metaFile = "{$this->cacheFile}.meta";
126         file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
127
128         $this->assertFalse($cache->isFresh());
129     }
130
131     public function testCacheKeepsContent()
132     {
133         $cache = new ResourceCheckerConfigCache($this->cacheFile);
134         $cache->write('FOOBAR');
135
136         $this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
137     }
138
139     public function testCacheIsNotFreshIfNotExistsMetaFile()
140     {
141         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
142         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
143         $cache->write('foo', array(new FileResource(__FILE__)));
144
145         $metaFile = "{$this->cacheFile}.meta";
146         unlink($metaFile);
147
148         $this->assertFalse($cache->isFresh());
149     }
150 }