1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Ds\Cache; |
4
|
|
|
|
5
|
|
|
use Ds\Cache\Cache; |
6
|
|
|
|
7
|
|
|
class CacheTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public $cache; |
11
|
|
|
public $storageMock; |
12
|
|
|
|
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
$this->storageMock = $this->getMockBuilder('\Ds\Cache\Interfaces\CacheStorageInterface') |
17
|
|
|
->getMock(); |
18
|
|
|
|
19
|
|
|
$this->cache = new Cache($this->storageMock,99); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testConstructNullStorage(){ |
23
|
|
|
$cache = new Cache(); |
24
|
|
|
$storage = $cache->getCacheStorage(); |
25
|
|
|
$this->assertInstanceOf('\Ds\Cache\NullStorage',$storage); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testConstructWithStorage(){ |
29
|
|
|
$cache = new Cache($this->storageMock); |
30
|
|
|
$storage = $cache->getCacheStorage(); |
31
|
|
|
$this->assertSame($this->storageMock,$storage); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
$key = 'key'; |
38
|
|
|
|
39
|
|
|
$this->storageMock->expects($this->once()) |
40
|
|
|
->method('has') |
41
|
|
|
->with( |
42
|
|
|
$this->equalTo($key) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->cache->has($key); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testSet() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$key = 'key'; |
52
|
|
|
$value = 'value'; |
53
|
|
|
$expires = 10; |
54
|
|
|
|
55
|
|
|
$this->storageMock->expects($this->once()) |
56
|
|
|
->method('set') |
57
|
|
|
->with( |
58
|
|
|
$this->equalTo($key), |
59
|
|
|
$this->equalTo($value), |
60
|
|
|
$this->equalTo($expires) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->cache->set($key, $value, $expires); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSetDefaultExpires() |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
$key = 'key'; |
70
|
|
|
$value = 'value'; |
71
|
|
|
|
72
|
|
|
$this->storageMock->expects($this->once()) |
73
|
|
|
->method('set') |
74
|
|
|
->with( |
75
|
|
|
$this->equalTo($key), |
76
|
|
|
$this->equalTo($value), |
77
|
|
|
$this->equalTo(99) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->cache->set($key, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testSetFromExpires() |
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
$key = 'key'; |
87
|
|
|
$value = 'value'; |
88
|
|
|
|
89
|
|
|
$this->cache = $this->cache->setExpires(60); |
90
|
|
|
|
91
|
|
|
$this->storageMock->expects($this->once()) |
92
|
|
|
->method('set') |
93
|
|
|
->with( |
94
|
|
|
$this->equalTo($key), |
95
|
|
|
$this->equalTo($value), |
96
|
|
|
$this->equalTo(60) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->cache->set($key, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testGet() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$key = 'key'; |
105
|
|
|
|
106
|
|
|
$this->storageMock->expects($this->once()) |
107
|
|
|
->method('get') |
108
|
|
|
->with( |
109
|
|
|
$this->equalTo($key) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->cache->get($key); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$key = 'key'; |
118
|
|
|
|
119
|
|
|
$this->storageMock->expects($this->once()) |
120
|
|
|
->method('delete') |
121
|
|
|
->with( |
122
|
|
|
$this->equalTo($key) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->cache->delete($key); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testWithCacheStorage() |
129
|
|
|
{ |
130
|
|
|
$newStorageMock = $this->getMockBuilder('\Ds\Cache\Interfaces\CacheStorageInterface') |
131
|
|
|
->getMock(); |
132
|
|
|
|
133
|
|
|
$newCache = $this->cache->withCacheStorage($newStorageMock); |
134
|
|
|
$this->assertEquals($newStorageMock, $newCache->getCacheStorage()); |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testGetCacheStorage() |
139
|
|
|
{ |
140
|
|
|
$expected = $this->storageMock; |
141
|
|
|
$actual = $this->cache->getCacheStorage(); |
142
|
|
|
$this->assertEquals($expected, $actual); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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.