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\Resource\FileResource;
16 use Symfony\Component\Config\ResourceCheckerConfigCache;
17 use Symfony\Component\Config\Tests\Resource\ResourceStub;
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 testCacheIsFreshIfEmptyCheckerIteratorProvided()
70 $cache = new ResourceCheckerConfigCache($this->cacheFile, new \ArrayIterator(array()));
71 $this->assertTrue($cache->isFresh());
74 public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
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()));
80 $this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
83 public function testIsFreshWithchecker()
85 $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
87 $checker->expects($this->once())
91 $checker->expects($this->once())
95 $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
96 $cache->write('', array(new ResourceStub()));
98 $this->assertTrue($cache->isFresh());
101 public function testIsNotFreshWithchecker()
103 $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
105 $checker->expects($this->once())
109 $checker->expects($this->once())
113 $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
114 $cache->write('', array(new ResourceStub()));
116 $this->assertFalse($cache->isFresh());
119 public function testCacheIsNotFreshWhenUnserializeFails()
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__)));
125 $metaFile = "{$this->cacheFile}.meta";
126 file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
128 $this->assertFalse($cache->isFresh());
131 public function testCacheKeepsContent()
133 $cache = new ResourceCheckerConfigCache($this->cacheFile);
134 $cache->write('FOOBAR');
136 $this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
139 public function testCacheIsNotFreshIfNotExistsMetaFile()
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__)));
145 $metaFile = "{$this->cacheFile}.meta";
148 $this->assertFalse($cache->isFresh());