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