|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 OpenSky Project Inc |
|
|
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 Assetic\Test\Cache; |
|
|
13 |
|
|
|
14 |
use Assetic\Cache\ExpiringCache; |
|
|
15 |
|
|
|
16 |
class ExpiringCacheTest extends \PHPUnit_Framework_TestCase |
|
|
17 |
{ |
|
|
18 |
private $inner; |
|
|
19 |
private $lifetime; |
|
|
20 |
private $cache; |
|
|
21 |
|
|
|
22 |
protected function setUp() |
|
|
23 |
{ |
|
|
24 |
$this->inner = $this->getMock('Assetic\\Cache\\CacheInterface'); |
|
|
25 |
$this->lifetime = 3600; |
|
|
26 |
$this->cache = new ExpiringCache($this->inner, $this->lifetime); |
|
|
27 |
} |
|
|
28 |
|
|
|
29 |
public function testHasExpired() |
|
|
30 |
{ |
|
|
31 |
$key = 'asdf'; |
|
|
32 |
$expiresKey = 'asdf.expires'; |
|
|
33 |
$thePast = 0; |
|
|
34 |
|
|
|
35 |
$this->inner->expects($this->once()) |
|
|
36 |
->method('has') |
|
|
37 |
->with($key) |
|
|
38 |
->will($this->returnValue(true)); |
|
|
39 |
$this->inner->expects($this->once()) |
|
|
40 |
->method('get') |
|
|
41 |
->with($expiresKey) |
|
|
42 |
->will($this->returnValue($thePast)); |
|
|
43 |
$this->inner->expects($this->at(2)) |
|
|
44 |
->method('remove') |
|
|
45 |
->with($expiresKey); |
|
|
46 |
$this->inner->expects($this->at(3)) |
|
|
47 |
->method('remove') |
|
|
48 |
->with($key); |
|
|
49 |
|
|
|
50 |
$this->assertFalse($this->cache->has($key), '->has() returns false if an expired value exists'); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
public function testHasNotExpired() |
|
|
54 |
{ |
|
|
55 |
$key = 'asdf'; |
|
|
56 |
$expiresKey = 'asdf.expires'; |
|
|
57 |
$theFuture = time() * 2; |
|
|
58 |
|
|
|
59 |
$this->inner->expects($this->once()) |
|
|
60 |
->method('has') |
|
|
61 |
->with($key) |
|
|
62 |
->will($this->returnValue(true)); |
|
|
63 |
$this->inner->expects($this->once()) |
|
|
64 |
->method('get') |
|
|
65 |
->with($expiresKey) |
|
|
66 |
->will($this->returnValue($theFuture)); |
|
|
67 |
|
|
|
68 |
$this->assertTrue($this->cache->has($key), '->has() returns true if a value the not expired'); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
public function testSetLifetime() |
|
|
72 |
{ |
|
|
73 |
$key = 'asdf'; |
|
|
74 |
$expiresKey = 'asdf.expires'; |
|
|
75 |
$value = 'qwerty'; |
|
|
76 |
|
|
|
77 |
$this->inner->expects($this->at(0)) |
|
|
78 |
->method('set') |
|
|
79 |
->with($expiresKey, $this->greaterThanOrEqual(time() + $this->lifetime)); |
|
|
80 |
$this->inner->expects($this->at(1)) |
|
|
81 |
->method('set') |
|
|
82 |
->with($key, $value); |
|
|
83 |
|
|
|
84 |
$this->cache->set($key, $value); |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
public function testRemove() |
|
|
88 |
{ |
|
|
89 |
$key = 'asdf'; |
|
|
90 |
$expiresKey = 'asdf.expires'; |
|
|
91 |
|
|
|
92 |
$this->inner->expects($this->at(0)) |
|
|
93 |
->method('remove') |
|
|
94 |
->with($expiresKey); |
|
|
95 |
$this->inner->expects($this->at(1)) |
|
|
96 |
->method('remove') |
|
|
97 |
->with($key); |
|
|
98 |
|
|
|
99 |
$this->cache->remove($key); |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
public function testGet() |
|
|
103 |
{ |
|
|
104 |
$this->inner->expects($this->once()) |
|
|
105 |
->method('get') |
|
|
106 |
->with('foo') |
|
|
107 |
->will($this->returnValue('bar')); |
|
|
108 |
|
|
|
109 |
$this->assertEquals('bar', $this->cache->get('foo'), '->get() returns the cached value'); |
|
|
110 |
} |
|
|
111 |
} |