1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Ds\Cache; |
4
|
|
|
|
5
|
|
|
use Ds\Cache\Cache; |
6
|
|
|
use Ds\Cache\CacheStorageInterface; |
7
|
|
|
use Ds\Cache\NullStorage; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CacheTest |
11
|
|
|
* @package Tests\Ds\Cache |
12
|
|
|
*/ |
13
|
|
|
class CacheTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Cache |
17
|
|
|
*/ |
18
|
|
|
public $cache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
22
|
|
|
*/ |
23
|
|
|
public $storageMock; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->storageMock = $this->getMockBuilder('\Rs\Cache\CacheStorageInterface')->getMock(); |
31
|
|
|
$this->cache = new Cache($this->storageMock); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test that NullStorage Adaptor is called by default. |
36
|
|
|
*/ |
37
|
|
|
public function testConstructDefaultNullStorage(){ |
38
|
|
|
$cache = new Cache(); |
39
|
|
|
$storage = $cache->getCacheStorage(); |
40
|
|
|
$this->assertInstanceOf('\Rs\Cache\NullStorage',$storage); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test that getCacheStorage returns storageMock. |
45
|
|
|
*/ |
46
|
|
|
public function testGetCacheStorage(){ |
47
|
|
|
$storage = $this->cache->getCacheStorage(); |
48
|
|
|
$this->assertSame($this->storageMock, $storage); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test CacheStorage is updated from construct. |
53
|
|
|
*/ |
54
|
|
|
public function testWithCacheStorage(){ |
55
|
|
|
$newStorage = new NullStorage(); |
56
|
|
|
$cache = $this->cache->withCacheStorage($newStorage); |
57
|
|
|
$storage = $cache->getCacheStorage(); |
58
|
|
|
$this->assertSame($newStorage, $storage); |
59
|
|
|
$this->assertNotSame($this->storageMock,$storage); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test that CacheStorageInterface::has() is called |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$key = 'key'; |
68
|
|
|
$this->storageMock->expects($this->once()) |
69
|
|
|
->method('has') |
70
|
|
|
->with( |
71
|
|
|
$this->equalTo($key) |
72
|
|
|
); |
73
|
|
|
$this->cache->has($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test that CacheStorageInterface::set() is called |
78
|
|
|
*/ |
79
|
|
|
public function testSet() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
$key = 'key'; |
83
|
|
|
$value = 'value'; |
84
|
|
|
$expires = 10; |
85
|
|
|
$this->storageMock->expects($this->once()) |
86
|
|
|
->method('set') |
87
|
|
|
->with( |
88
|
|
|
$this->equalTo($key), |
89
|
|
|
$this->equalTo($value), |
90
|
|
|
$this->equalTo($expires) |
91
|
|
|
); |
92
|
|
|
$this->cache->set($key, $value, $expires); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Test that CacheStorageInterface::get() is called |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function testGet() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$key = 'key'; |
101
|
|
|
$this->storageMock->expects($this->once()) |
102
|
|
|
->method('get') |
103
|
|
|
->with( |
104
|
|
|
$this->equalTo($key) |
105
|
|
|
); |
106
|
|
|
$this->cache->get($key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test that CacheStorageInterface::delete() is called |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$key = 'key'; |
115
|
|
|
$this->storageMock->expects($this->once()) |
116
|
|
|
->method('delete') |
117
|
|
|
->with( |
118
|
|
|
$this->equalTo($key) |
119
|
|
|
); |
120
|
|
|
$this->cache->delete($key); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.