1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
4
|
|
|
use Cmp\Cache\Backend\RedisCache; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FeatureContext |
8
|
|
|
*/ |
9
|
|
|
class FeatureContext implements SnippetAcceptingContext |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Redis |
13
|
|
|
*/ |
14
|
|
|
private $redis; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var RedisCache |
18
|
|
|
*/ |
19
|
|
|
private $backend; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $result; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* FeatureContext constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->redis = new \Redis(); |
32
|
|
|
$this->redis->connect($_SERVER['REDIS_HOST'], 6379); |
33
|
|
|
$this->backend = new RedisCache($this->redis); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @BeforeScenario |
38
|
|
|
*/ |
39
|
|
|
public function reset() |
40
|
|
|
{ |
41
|
|
|
$this->redis->flushDB(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Given I store a an item in the cache |
46
|
|
|
*/ |
47
|
|
|
public function iStoreAAnItemInTheCache() |
48
|
|
|
{ |
49
|
|
|
$this->backend->set('foo', 'bar'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @When I retrieve it |
54
|
|
|
*/ |
55
|
|
|
public function whenIRetrieved() |
56
|
|
|
{ |
57
|
|
|
$this->result = $this->backend->demand('foo'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @Then I should get the same item |
62
|
|
|
*/ |
63
|
|
|
public function iShouldGetTheSameItem() |
64
|
|
|
{ |
65
|
|
|
if ($this->result !== 'bar') { |
66
|
|
|
throw new \RuntimeException("The retrieve item is not the same"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $timeToLive |
72
|
|
|
* |
73
|
|
|
* @Given I store a an item in the cache for :timeToLive second |
74
|
|
|
*/ |
75
|
|
|
public function iStoreAAnItemInTheCacheForSecond($timeToLive) |
76
|
|
|
{ |
77
|
|
|
$this->backend->set('foo', 'bar', $timeToLive); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $seconds |
82
|
|
|
* |
83
|
|
|
* @When I wait :seconds seconds |
84
|
|
|
*/ |
85
|
|
|
public function iWaitSeconds($seconds) |
86
|
|
|
{ |
87
|
|
|
sleep($seconds); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @Then I should not be able to retrieve it |
92
|
|
|
*/ |
93
|
|
|
public function iShouldNotBeAbleToRetrieveIt() |
94
|
|
|
{ |
95
|
|
|
if ($this->backend->has('foo')) { |
96
|
|
|
throw new \RuntimeException("Redis still has the item stored"); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @When I delete the item from the cache |
102
|
|
|
*/ |
103
|
|
|
public function iDeleteTheItemFromTheCache() |
104
|
|
|
{ |
105
|
|
|
$this->backend->delete('foo'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Given I store two items in the cache |
110
|
|
|
*/ |
111
|
|
|
public function iStoreTwoItemsInTheCache() |
112
|
|
|
{ |
113
|
|
|
$this->backend->set('foo', 'bar'); |
114
|
|
|
$this->backend->set('bar', 'foo'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @When I delete the two items from the cache |
119
|
|
|
*/ |
120
|
|
|
public function iDeleteTheTwoItemsFromTheCache() |
121
|
|
|
{ |
122
|
|
|
$this->backend->deleteItems(['foo', 'bar']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @Then I should not be able to retrieve any of them |
127
|
|
|
*/ |
128
|
|
|
public function iShouldNotBeAbleToRetrieveAnyOfThem() |
129
|
|
|
{ |
130
|
|
|
$items = $this->backend->getItems(['foo', 'bar']); |
131
|
|
|
|
132
|
|
|
if ($items['foo'] !== null || $items['bar'] !== null) { |
133
|
|
|
throw new RuntimeException("There should not be any item on the cache"); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @When I flush all the items item from the cache |
139
|
|
|
*/ |
140
|
|
|
public function iFlushAllTheItemsItemFromTheCache() |
141
|
|
|
{ |
142
|
|
|
$this->backend->flush(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @When I request a new redis cache instance to the factory |
147
|
|
|
*/ |
148
|
|
|
public function iRequestANewRedisCacheInstanceToTheFactory() |
149
|
|
|
{ |
150
|
|
|
$redis = (new \Cmp\Cache\Factory\CacheBuilder()) |
151
|
|
|
->withRedisCacheFromParams($_SERVER['REDIS_HOST'], 6379, 1) |
152
|
|
|
->build(); |
153
|
|
|
|
154
|
|
|
if (!$redis instanceof RedisCache) { |
155
|
|
|
throw new RuntimeException("The factory could not create a RedisCache"); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @Given I have the connection parameters for redis |
161
|
|
|
* @Then I should receive a redis cache already connected |
162
|
|
|
*/ |
163
|
|
|
public function doNothing() |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|