|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Cmp\Cache\Backend; |
|
4
|
|
|
|
|
5
|
|
|
use Cmp\Cache\Exceptions\ExpiredException; |
|
6
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
|
7
|
|
|
use PhpSpec\ObjectBehavior; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ArrayCacheSpec |
|
11
|
|
|
* |
|
12
|
|
|
* @package spec\Cmp\Cache\Infrastructure\Backend |
|
13
|
|
|
* @mixin \Cmp\Cache\Backend\ArrayCache |
|
14
|
|
|
*/ |
|
15
|
|
|
class ArrayCacheSpec extends ObjectBehavior |
|
16
|
|
|
{ |
|
17
|
|
|
function it_is_initializable() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->shouldHaveType('Cmp\Cache\Backend\ArrayCache'); |
|
20
|
|
|
$this->shouldHaveType('Cmp\Cache\Cache'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
function it_can_store_items() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->has('foo')->shouldReturn(false); |
|
26
|
|
|
|
|
27
|
|
|
$this->set('foo', 'bar')->shouldReturn(true); |
|
28
|
|
|
|
|
29
|
|
|
$this->has('foo')->shouldReturn(true); |
|
30
|
|
|
$this->demand('foo')->shouldReturn('bar'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
function it_can_store_items_for_a_limited_period_of_time() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->has('foo')->shouldReturn(false); |
|
36
|
|
|
$this->set('foo', 'bar', 1); |
|
37
|
|
|
|
|
38
|
|
|
sleep(2); |
|
39
|
|
|
$this->shouldThrow(new ExpiredException('foo'))->duringDemand('foo'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function it_can_store_multiple_items() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->has('foo')->shouldReturn(false); |
|
45
|
|
|
$this->has('bar')->shouldReturn(false); |
|
46
|
|
|
|
|
47
|
|
|
$this->setItems(['foo' => 1, 'bar' => 2])->shouldReturn(true); |
|
48
|
|
|
|
|
49
|
|
|
$this->has('foo')->shouldReturn(true); |
|
50
|
|
|
$this->has('bar')->shouldReturn(true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
function it_can_get_multiple_items() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setItems(['foo' => 1, 'bar' => 2])->shouldReturn(true); |
|
56
|
|
|
|
|
57
|
|
|
$this->getItems(['bar', 'foo'])->shouldReturn(['bar' => 2, 'foo' => 1]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function it_can_delete_multiple_items() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->setItems(['foo' => 1, 'bar' => 2])->shouldReturn(true); |
|
63
|
|
|
|
|
64
|
|
|
$this->deleteItems(['foo', 'bar'])->shouldReturn(true); |
|
65
|
|
|
$this->has('foo')->shouldReturn(false); |
|
66
|
|
|
$this->has('bar')->shouldReturn(false); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
function it_checking_an_expired_item_forces_a_delete() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->has('foo')->shouldReturn(false); |
|
72
|
|
|
$this->set('foo', 'bar', 1); |
|
73
|
|
|
|
|
74
|
|
|
sleep(2); |
|
75
|
|
|
$this->has('foo')->shouldBe(false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
function it_throws_an_exception_when_trying_to_demand_a_non_set_item() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->shouldThrow(new NotFoundException('foo'))->duringDemand('foo'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function it_can_return_a_default_value_when_trying_a_non_set_item() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->get('foo', 'bar')->shouldReturn('bar'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
function it_can_empty_the_cache() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->set('foo', 'bar'); |
|
91
|
|
|
$this->has('foo')->shouldReturn(true); |
|
92
|
|
|
|
|
93
|
|
|
$this->flush(); |
|
94
|
|
|
$this->has('foo')->shouldReturn(false); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function it_can_get_the_remaining_time_to_live() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->set('foo', 'bar', 10); |
|
100
|
|
|
$this->getTimeToLive('foo')->shouldBe(10); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
function it_can_return_null_for_the_time_to_live_of_infinite_items() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->set('foo', 'bar'); |
|
106
|
|
|
$this->getTimeToLive('foo')->shouldBe(null); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function it_can_return_null_for_the_time_to_live_for_expired_items() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->set('foo', 'bar', 1); |
|
112
|
|
|
|
|
113
|
|
|
sleep(2); |
|
114
|
|
|
$this->getTimeToLive('foo')->shouldBe(null); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|