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;
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;
19 class ResourceCheckerConfigCacheTest extends TestCase
21 private $cacheFile = null;
23 protected function setUp()
25 $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
28 protected function tearDown()
30 $files = array($this->cacheFile, "{$this->cacheFile}.meta");
32 foreach ($files as $file) {
33 if (file_exists($file)) {
39 public function testGetPath()
41 $cache = new ResourceCheckerConfigCache($this->cacheFile);
43 $this->assertSame($this->cacheFile, $cache->getPath());
46 public function testCacheIsNotFreshIfEmpty()
48 $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock()
49 ->expects($this->never())->method('supports');
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. */
54 unlink($this->cacheFile); // remove tempnam() side effect
55 $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
57 $this->assertFalse($cache->isFresh());
60 public function testCacheIsFreshIfNocheckerProvided()
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());
68 public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
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()));
74 $this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
77 public function testIsFreshWithchecker()
79 $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
81 $checker->expects($this->once())
85 $checker->expects($this->once())
89 $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
90 $cache->write('', array(new ResourceStub()));
92 $this->assertTrue($cache->isFresh());
95 public function testIsNotFreshWithchecker()
97 $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
99 $checker->expects($this->once())
103 $checker->expects($this->once())
107 $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
108 $cache->write('', array(new ResourceStub()));
110 $this->assertFalse($cache->isFresh());
113 public function testCacheIsNotFreshWhenUnserializeFails()
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__)));
119 $metaFile = "{$this->cacheFile}.meta";
120 file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
122 $this->assertFalse($cache->isFresh());
125 public function testCacheKeepsContent()
127 $cache = new ResourceCheckerConfigCache($this->cacheFile);
128 $cache->write('FOOBAR');
130 $this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
133 public function testCacheIsNotFreshIfNotExistsMetaFile()
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__)));
139 $metaFile = "{$this->cacheFile}.meta";
142 $this->assertFalse($cache->isFresh());