Yaffs site version 1.1
[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\Tests\Resource\ResourceStub;
16 use Symfony\Component\Config\Resource\FileResource;
17 use Symfony\Component\Config\ResourceCheckerConfigCache;
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 testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
69     {
70         /* As in the previous test, but this time we have a resource. */
71         $cache = new ResourceCheckerConfigCache($this->cacheFile);
72         $cache->write('', array(new ResourceStub()));
73
74         $this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
75     }
76
77     public function testIsFreshWithchecker()
78     {
79         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
80
81         $checker->expects($this->once())
82                   ->method('supports')
83                   ->willReturn(true);
84
85         $checker->expects($this->once())
86                   ->method('isFresh')
87                   ->willReturn(true);
88
89         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
90         $cache->write('', array(new ResourceStub()));
91
92         $this->assertTrue($cache->isFresh());
93     }
94
95     public function testIsNotFreshWithchecker()
96     {
97         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
98
99         $checker->expects($this->once())
100                   ->method('supports')
101                   ->willReturn(true);
102
103         $checker->expects($this->once())
104                   ->method('isFresh')
105                   ->willReturn(false);
106
107         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
108         $cache->write('', array(new ResourceStub()));
109
110         $this->assertFalse($cache->isFresh());
111     }
112
113     public function testCacheIsNotFreshWhenUnserializeFails()
114     {
115         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
116         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
117         $cache->write('foo', array(new FileResource(__FILE__)));
118
119         $metaFile = "{$this->cacheFile}.meta";
120         file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
121
122         $this->assertFalse($cache->isFresh());
123     }
124
125     public function testCacheKeepsContent()
126     {
127         $cache = new ResourceCheckerConfigCache($this->cacheFile);
128         $cache->write('FOOBAR');
129
130         $this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
131     }
132
133     public function testCacheIsNotFreshIfNotExistsMetaFile()
134     {
135         $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
136         $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
137         $cache->write('foo', array(new FileResource(__FILE__)));
138
139         $metaFile = "{$this->cacheFile}.meta";
140         unlink($metaFile);
141
142         $this->assertFalse($cache->isFresh());
143     }
144 }