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