1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Cmp\Cache\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Cache; |
6
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
7
|
|
|
use PhpSpec\ObjectBehavior; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ChainCacheSpec |
11
|
|
|
* |
12
|
|
|
* @package spec\Cmp\Cache\Infrastructure\Backend |
13
|
|
|
* @mixin \Cmp\Cache\Backend\ChainCache |
14
|
|
|
*/ |
15
|
|
|
class ChainCacheSpec extends ObjectBehavior |
16
|
|
|
{ |
17
|
|
|
function let(Cache $cacheOne, Cache $cacheTwo) |
18
|
|
|
{ |
19
|
|
|
$this->pushCache($cacheOne)->pushCache($cacheTwo); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function it_is_initializable() |
23
|
|
|
{ |
24
|
|
|
$this->shouldHaveType('Cmp\Cache\Backend\ChainCache'); |
25
|
|
|
$this->shouldHaveType('Cmp\Cache\Cache'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
function it_executes_set_command_in_all_the_caches(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$cacheOne->set('foo', 'bar', 123)->willReturn(true); |
31
|
|
|
$cacheTwo->set('foo', 'bar', 123)->willReturn(true); |
32
|
|
|
|
33
|
|
|
$this->set('foo', 'bar', 123)->shouldReturn(true); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
function it_returns_false_if_a_cache_fails(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$cacheOne->set('foo', 'bar', 123)->willReturn(true); |
39
|
|
|
$cacheTwo->set('foo', 'bar', 123)->willReturn(false); |
40
|
|
|
|
41
|
|
|
$this->set('foo', 'bar', 123)->shouldReturn(false); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function it_does_not_ask_to_all_caches_if_one_has_the_item(Cache $cacheOne, Cache $cacheTwo) |
45
|
|
|
{ |
46
|
|
|
$cacheOne->has('foo')->willReturn(true); |
47
|
|
|
|
48
|
|
|
$this->has('foo')->shouldReturn(true); |
49
|
|
|
|
50
|
|
|
$cacheTwo->set('foo')->shouldNotHaveBeenCalled(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
function it_tries_all_the_caches_before_failing_to_checking_for_an_item(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$cacheOne->has('foo')->willReturn(false); |
56
|
|
|
$cacheTwo->has('foo')->willReturn(false); |
57
|
|
|
|
58
|
|
|
$this->has('foo')->shouldReturn(false); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function it_does_not_ask_to_all_caches_if_one_get_the_item(Cache $cacheOne, Cache $cacheTwo) |
62
|
|
|
{ |
63
|
|
|
$cacheOne->get('foo')->willReturn('item'); |
64
|
|
|
$cacheOne->getTimeToLive('foo')->willReturn(null); |
65
|
|
|
|
66
|
|
|
$this->get('foo')->shouldReturn('item'); |
67
|
|
|
|
68
|
|
|
$cacheTwo->get('foo')->shouldNotHaveBeenCalled(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function it_populates_previous_caches_if_they_have_failed_to_get_the_item(Cache $cacheOne, Cache $cacheTwo) |
72
|
|
|
{ |
73
|
|
|
$cacheOne->get('foo')->willReturn(false); |
74
|
|
|
$cacheTwo->get('foo')->willReturn('item'); |
75
|
|
|
$cacheTwo->getTimeToLive('foo')->willReturn(10); |
76
|
|
|
$cacheOne->set('foo', 'item', 10)->shouldBeCalled(); |
77
|
|
|
|
78
|
|
|
$this->get('foo')->shouldReturn('item'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
function it_tries_all_the_caches_before_failing_to_get_an_item(Cache $cacheOne, Cache $cacheTwo) |
82
|
|
|
{ |
83
|
|
|
$cacheOne->get('foo')->willReturn(false); |
84
|
|
|
$cacheTwo->get('foo')->willReturn(false); |
85
|
|
|
|
86
|
|
|
$this->get('foo', 'default')->shouldReturn('default'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function it_can_demand_an_item(Cache $cacheOne, Cache $cacheTwo) |
90
|
|
|
{ |
91
|
|
|
$cacheOne->get('foo')->willReturn(false); |
92
|
|
|
$cacheTwo->get('foo')->willReturn('bar'); |
93
|
|
|
$cacheTwo->getTimeToLive('foo')->willReturn(null); |
94
|
|
|
$cacheOne->set('foo', 'bar', null)->shouldBeCalled(); |
95
|
|
|
|
96
|
|
|
$this->demand('foo')->shouldReturn('bar'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function it_tries_all_the_caches_before_failing_to_demand_an_item(Cache $cacheOne, Cache $cacheTwo) |
100
|
|
|
{ |
101
|
|
|
$cacheOne->get('foo')->willReturn(false); |
102
|
|
|
$cacheTwo->get('foo')->willReturn(false); |
103
|
|
|
|
104
|
|
|
$this->shouldThrow(new NotFoundException('foo'))->duringDemand('foo'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
function it_deletes_the_item_in_all_caches(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$cacheOne->delete('foo')->willReturn(true); |
110
|
|
|
$cacheTwo->delete('foo')->willReturn(true); |
111
|
|
|
|
112
|
|
|
$this->delete('foo')->shouldReturn(true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
function it_flushes_all_caches(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$cacheOne->flush()->willReturn(true); |
118
|
|
|
$cacheTwo->flush()->willReturn(true); |
119
|
|
|
|
120
|
|
|
$this->flush()->shouldReturn(true); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
function it_can_retrieve_the_time_to_live(Cache $cacheOne, Cache $cacheTwo) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$cacheOne->getTimeToLive('foo')->willReturn(null); |
126
|
|
|
$cacheTwo->getTimeToLive('foo')->willReturn(100); |
127
|
|
|
|
128
|
|
|
$cacheOne->getTimeToLive('bar')->willReturn(null); |
129
|
|
|
$cacheTwo->getTimeToLive('bar')->willReturn(null); |
130
|
|
|
|
131
|
|
|
$this->getTimeToLive('foo')->shouldReturn(100); |
132
|
|
|
$this->getTimeToLive('bar')->shouldReturn(null); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.