1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
4
|
|
|
use Cmp\Cache\Application\CacheFactory; |
5
|
|
|
use Cmp\Cache\Application\TestCacheDecorator; |
6
|
|
|
use Cmp\Cache\Infrastructure\ArrayCache; |
7
|
|
|
use Cmp\Cache\Infrastructure\Provider\PimpleCacheProvider; |
8
|
|
|
use Cmp\Cache\Infrastructure\RedisCache; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FeatureContext |
13
|
|
|
*/ |
14
|
|
|
class FeatureContext implements SnippetAcceptingContext |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Redis |
18
|
|
|
*/ |
19
|
|
|
private $redis; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var RedisCache |
23
|
|
|
*/ |
24
|
|
|
private $backend; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $result; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Container |
33
|
|
|
*/ |
34
|
|
|
private $pimple; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FeatureContext constructor. |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->redis = new \Redis(); |
42
|
|
|
$this->redis->connect($_SERVER['REDIS_HOST'], 6379); |
43
|
|
|
$this->backend = new RedisCache($this->redis); |
44
|
|
|
$this->theContainerIsEmpty(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @BeforeScenario |
49
|
|
|
*/ |
50
|
|
|
public function reset() |
51
|
|
|
{ |
52
|
|
|
$this->redis->flushDB(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Given The cache is empty |
57
|
|
|
*/ |
58
|
|
|
public function theCacheIsEmpty() |
59
|
|
|
{ |
60
|
|
|
$this->redis->flushDB(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Given I store a an item in the cache |
65
|
|
|
*/ |
66
|
|
|
public function iStoreAAnItemInTheCache() |
67
|
|
|
{ |
68
|
|
|
$this->backend->set('foo', 'bar'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @When I retrieve it |
73
|
|
|
*/ |
74
|
|
|
public function whenIRetrieved() |
75
|
|
|
{ |
76
|
|
|
$this->result = $this->backend->demand('foo'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @Then I should get the same item |
81
|
|
|
*/ |
82
|
|
|
public function iShouldGetTheSameItem() |
83
|
|
|
{ |
84
|
|
|
if ($this->result !== 'bar') { |
85
|
|
|
throw new \RuntimeException("The retrieve item is not the same"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $timeToLive |
91
|
|
|
* |
92
|
|
|
* @Given I store a an item in the cache for :timeToLive second |
93
|
|
|
*/ |
94
|
|
|
public function iStoreAAnItemInTheCacheForSecond($timeToLive) |
95
|
|
|
{ |
96
|
|
|
$this->backend->set('foo', 'bar', $timeToLive); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $seconds |
101
|
|
|
* |
102
|
|
|
* @When I wait :seconds seconds |
103
|
|
|
*/ |
104
|
|
|
public function iWaitSeconds($seconds) |
105
|
|
|
{ |
106
|
|
|
sleep($seconds); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @Then I should not be able to retrieve it |
111
|
|
|
*/ |
112
|
|
|
public function iShouldNotBeAbleToRetrieveIt() |
113
|
|
|
{ |
114
|
|
|
if ($this->backend->has('foo')) { |
115
|
|
|
throw new \RuntimeException("Redis still has the item stored"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @When I delete the item from the cache |
121
|
|
|
*/ |
122
|
|
|
public function iDeleteTheItemFromTheCache() |
123
|
|
|
{ |
124
|
|
|
$this->backend->delete('foo'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @When I flush all the items item from the cache |
129
|
|
|
*/ |
130
|
|
|
public function iFlushAllTheItemsItemFromTheCache() |
131
|
|
|
{ |
132
|
|
|
$this->backend->flush(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @When I request a new redis cache instance to the factory |
137
|
|
|
*/ |
138
|
|
|
public function iRequestANewRedisCacheInstanceToTheFactory() |
139
|
|
|
{ |
140
|
|
|
$redis = CacheFactory::redisFromParams($_SERVER['REDIS_HOST'], 6379, 1); |
141
|
|
|
if (!$redis instanceof RedisCache) { |
142
|
|
|
throw new RuntimeException("The factory could not create a RedisCache"); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @Given I have the connection parameters for redis |
148
|
|
|
* @Then I should receive a redis cache already connected |
149
|
|
|
*/ |
150
|
|
|
public function doNothing() |
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @Given The container is empty |
157
|
|
|
*/ |
158
|
|
|
public function theContainerIsEmpty() |
159
|
|
|
{ |
160
|
|
|
$this->pimple = new Container(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @When I pass the options to build the array access to the provider |
165
|
|
|
*/ |
166
|
|
|
public function iPassTheOptionsToBuildTheArrayAccessToTheProvider() |
167
|
|
|
{ |
168
|
|
|
$this->pimple->register(new PimpleCacheProvider(), ['cache.backend' => 'array']); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @When I register the provider without any option |
173
|
|
|
*/ |
174
|
|
|
public function iRegisterTheProviderWithoutAnyOption() |
175
|
|
|
{ |
176
|
|
|
$this->pimple->register(new PimpleCacheProvider()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @When I pass the options to build the redis cache from parameters to the provider |
181
|
|
|
*/ |
182
|
|
|
public function iPassTheOptionsToBuildTheRedisCacheFromParametersToTheProvider() |
183
|
|
|
{ |
184
|
|
|
$this->pimple->register(new PimpleCacheProvider(), ['cache.backend' => ['redis' => [ |
185
|
|
|
'host' => $_SERVER['REDIS_HOST'], |
186
|
|
|
'port' => 6379, |
187
|
|
|
'db' => 2, |
188
|
|
|
'timeout' => 0.5, |
189
|
|
|
]]]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @When I pass the options to build the redis cache from an open connection to the provider |
194
|
|
|
*/ |
195
|
|
|
public function iPassTheOptionsToBuildTheRedisCacheFromAnOpenConnectionToTheProvider() |
196
|
|
|
{ |
197
|
|
|
$this->pimple->register(new PimpleCacheProvider(), ['cache.backend' => ['redis' => $this->redis]]); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @When I register the provider with the debug flag set to true |
202
|
|
|
*/ |
203
|
|
|
public function iRegisterTheProviderWithTheDebugFlagSetToTrue() |
204
|
|
|
{ |
205
|
|
|
$this->pimple->register(new PimpleCacheProvider(), ['cache.debug' => true]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @Then I should retrieve the test decorated cache |
210
|
|
|
*/ |
211
|
|
|
public function iShouldRetrieveTheTestDecoratedCache() |
212
|
|
|
{ |
213
|
|
|
if (!$this->pimple['cache'] instanceof TestCacheDecorator) { |
214
|
|
|
throw new RuntimeException("The cache has not been properly decorated"); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @Then I should retrieve the redis cache object |
220
|
|
|
*/ |
221
|
|
|
public function iShouldRetrieveTheRedisCacheObject() |
222
|
|
|
{ |
223
|
|
|
if (!$this->pimple['cache'] instanceof RedisCache) { |
224
|
|
|
throw new RuntimeException("The redis cache has not been registered correctly"); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @Then I should retrieve the array cache object |
230
|
|
|
*/ |
231
|
|
|
public function iShouldRetrieveTheArrayCacheObject() |
232
|
|
|
{ |
233
|
|
|
if (!$this->pimple['cache'] instanceof ArrayCache) { |
234
|
|
|
throw new RuntimeException("The array cache has not been registered correctly"); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |