ResourceCheckerConfigCacheTest.php
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests;
use Symfony\Component\Config\Tests\Resource\ResourceStub;
use Symfony\Component\Config\ResourceCheckerConfigCache;
class ResourceCheckerConfigCacheTest extends \PHPUnit_Framework_TestCase
{
private $cacheFile = null;
protected function setUp()
{
$this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
}
protected function tearDown()
{
$files = array($this->cacheFile, "{$this->cacheFile}.meta");
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function testGetPath()
{
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$this->assertSame($this->cacheFile, $cache->getPath());
}
public function testCacheIsNotFreshIfEmpty()
{
$checker = $this->getMock('\Symfony\Component\Config\ResourceCheckerInterface')
->expects($this->never())->method('supports');
/* If there is nothing in the cache, it needs to be filled (and thus it's not fresh).
It does not matter if you provide checkers or not. */
unlink($this->cacheFile); // remove tempnam() side effect
$cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
$this->assertFalse($cache->isFresh());
}
public function testCacheIsFreshIfNocheckerProvided()
{
/* For example in prod mode, you may choose not to run any checkers
at all. In that case, the cache should always be considered fresh. */
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$this->assertTrue($cache->isFresh());
}
public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
{
/* As in the previous test, but this time we have a resource. */
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$cache->write('', array(new ResourceStub()));
$this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
}
public function testIsFreshWithchecker()
{
$checker = $this->getMock('\Symfony\Component\Config\ResourceCheckerInterface');
$checker->expects($this->once())
->method('supports')
->willReturn(true);
$checker->expects($this->once())
->method('isFresh')
->willReturn(true);
$cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
$cache->write('', array(new ResourceStub()));
$this->assertTrue($cache->isFresh());
}
public function testIsNotFreshWithchecker()
{
$checker = $this->getMock('\Symfony\Component\Config\ResourceCheckerInterface');
$checker->expects($this->once())
->method('supports')
->willReturn(true);
$checker->expects($this->once())
->method('isFresh')
->willReturn(false);
$cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
$cache->write('', array(new ResourceStub()));
$this->assertFalse($cache->isFresh());
}
public function testCacheKeepsContent()
{
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$cache->write('FOOBAR');
$this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
}
}