1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Cmp\Cache\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
6
|
|
|
use PhpSpec\ObjectBehavior; |
7
|
|
|
use Redis; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RedisCacheSpec |
11
|
|
|
* |
12
|
|
|
* @package spec\Cmp\Cache\Infrastructure\Backend |
13
|
|
|
* @mixin \Cmp\Cache\Backend\RedisCache |
14
|
|
|
*/ |
15
|
|
|
class RedisCacheSpec extends ObjectBehavior |
16
|
|
|
{ |
17
|
|
|
function let(Redis $redis) |
18
|
|
|
{ |
19
|
|
|
$this->beConstructedWith($redis); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function it_is_initializable() |
23
|
|
|
{ |
24
|
|
|
$this->shouldHaveType('Cmp\Cache\Backend\RedisCache'); |
25
|
|
|
$this->shouldHaveType('Cmp\Cache\Cache'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function it_can_store_items(Redis $redis) |
29
|
|
|
{ |
30
|
|
|
$this->set('foo', 'bar'); |
31
|
|
|
|
32
|
|
|
$redis->set('foo', 'bar')->shouldHaveBeenCalled(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_can_store_multiple_items(Redis $redis) |
36
|
|
|
{ |
37
|
|
|
$redis->mset(['foo' => 1, 'bar' => 2])->willReturn(true); |
38
|
|
|
|
39
|
|
|
$this->setItems(['foo' => 1, 'bar' => 2])->shouldReturn(true); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function it_can_store_items_for_a_limited_period_of_time(Redis $redis) |
43
|
|
|
{ |
44
|
|
|
$this->set('foo', 'bar', 1); |
45
|
|
|
|
46
|
|
|
$redis->setex('foo', 1, 'bar')->shouldHaveBeenCalled(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_can_store_multiple_items_for_a_limited_period_of_time(Redis $redis) |
50
|
|
|
{ |
51
|
|
|
$redis->setex('foo', 300, 1)->willReturn(true); |
52
|
|
|
$redis->setex('bar', 300, 2)->willReturn(true); |
53
|
|
|
|
54
|
|
|
$this->setItems(['foo' => 1, 'bar' => 2], 300)->shouldReturn(true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function it_delete_an_item(Redis $redis) |
58
|
|
|
{ |
59
|
|
|
$redis->delete('foo')->willReturn(true); |
60
|
|
|
|
61
|
|
|
$this->delete('foo')->shouldReturn(true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function it_delete_multiple_items_at_once(Redis $redis) |
65
|
|
|
{ |
66
|
|
|
$redis->delete(['foo', 'bar'])->willReturn(true); |
67
|
|
|
|
68
|
|
|
$this->delete(['foo', 'bar'])->shouldReturn(true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function it_can_check_the_existence_of_an_item_in_the_cache(Redis $redis) |
72
|
|
|
{ |
73
|
|
|
$redis->exists('foo')->willReturn(true); |
74
|
|
|
|
75
|
|
|
$this->has('foo')->shouldReturn(true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function it_throws_an_exception_when_trying_to_demand_a_non_set_item(Redis $redis) |
79
|
|
|
{ |
80
|
|
|
$redis->get('foo')->willReturn(null); |
81
|
|
|
$redis->exists('foo')->willReturn(false); |
82
|
|
|
|
83
|
|
|
$this->shouldThrow(new NotFoundException('foo'))->duringDemand('foo'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function it_can_return_a_default_value_when_trying_to_get_a_non_set_item(Redis $redis) |
87
|
|
|
{ |
88
|
|
|
$redis->get('foo')->willReturn(null); |
89
|
|
|
$redis->exists('foo')->willReturn(false); |
90
|
|
|
|
91
|
|
|
$this->get('foo', 'bar')->shouldReturn('bar'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
function it_can_empty_the_cache(Redis $redis) |
95
|
|
|
{ |
96
|
|
|
$this->flush(); |
97
|
|
|
|
98
|
|
|
$redis->flushDB()->shouldHaveBeenCalled(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function it_gets_multiple_items_from_cache(Redis $redis) |
102
|
|
|
{ |
103
|
|
|
$redis->mget(['foo', 'bar'])->willReturn([0 => 1, 1 => false]); |
104
|
|
|
|
105
|
|
|
$this->getItems(['foo', 'bar'])->shouldReturn(['foo' => 1, 'bar' => null]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
function it_deletes_multiple_items_from_cache(Redis $redis) |
109
|
|
|
{ |
110
|
|
|
$redis->delete('foo', 'bar')->willReturn(2); |
111
|
|
|
|
112
|
|
|
$this->deleteItems(['foo', 'bar'])->shouldReturn(true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
function it_can_get_the_time_to_live(Redis $redis) |
116
|
|
|
{ |
117
|
|
|
$redis->ttl('foo')->willReturn(false); |
118
|
|
|
$redis->ttl('bar')->willReturn(-1); |
119
|
|
|
$redis->ttl('foobar')->willReturn(15); |
120
|
|
|
|
121
|
|
|
$this->getTimeToLive('foo')->shouldReturn(null); |
122
|
|
|
$this->getTimeToLive('bar')->shouldReturn(null); |
123
|
|
|
$this->getTimeToLive('foobar')->shouldReturn(15); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|