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