1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Cmp\Cache\Infrastructure; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Domain\Exceptions\NotFoundException; |
6
|
|
|
use PhpSpec\ObjectBehavior; |
7
|
|
|
use Redis; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RedisCacheSpec |
11
|
|
|
* |
12
|
|
|
* @package spec\Cmp\Cache\Infrastructure |
13
|
|
|
* @mixin \Cmp\Cache\Infrastructure\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\Infrastructure\RedisCache'); |
25
|
|
|
$this->shouldHaveType('Cmp\Cache\Domain\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_items_for_a_limited_period_of_time(Redis $redis) |
36
|
|
|
{ |
37
|
|
|
$this->set('foo', 'bar', 1); |
38
|
|
|
|
39
|
|
|
$redis->setex('foo', 1, 'bar')->shouldHaveBeenCalled(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function it_can_check_the_existence_of_an_item_in_the_cache(Redis $redis) |
43
|
|
|
{ |
44
|
|
|
$redis->exists('foo')->willReturn(true); |
45
|
|
|
|
46
|
|
|
$this->has('foo')->shouldReturn(true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_throws_an_exception_when_trying_to_get_a_non_set_item(Redis $redis) |
50
|
|
|
{ |
51
|
|
|
$redis->get('foo')->willReturn(null); |
52
|
|
|
$redis->exists('foo')->willReturn(false); |
53
|
|
|
|
54
|
|
|
$this->shouldThrow(new NotFoundException('foo'))->duringGet('foo'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function it_can_return_a_default_value_when_pulling_a_non_set_item(Redis $redis) |
58
|
|
|
{ |
59
|
|
|
$redis->get('foo')->willReturn(null); |
60
|
|
|
$redis->exists('foo')->willReturn(false); |
61
|
|
|
|
62
|
|
|
$this->pull('foo', 'bar')->shouldReturn('bar'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function it_can_empty_the_cache(Redis $redis) |
66
|
|
|
{ |
67
|
|
|
$this->flush(); |
68
|
|
|
|
69
|
|
|
$redis->flushDB()->shouldHaveBeenCalled(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|