1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\Psr\SimpleCache; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use DateTime; |
7
|
|
|
use SubjectivePHP\Psr\SimpleCache\InMemoryCache; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @coversDefaultClass \SubjectivePHP\Psr\SimpleCache\InMemoryCache |
11
|
|
|
* @covers ::__construct |
12
|
|
|
* @covers ::<private> |
13
|
|
|
*/ |
14
|
|
|
final class InMemoryCacheTest extends \PHPUnit\Framework\TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
* @covers ::get |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function get() |
23
|
|
|
{ |
24
|
|
|
$container = new ArrayObject(); |
25
|
|
|
$cache = new InMemoryCache($container); |
26
|
|
|
$dateTime = new DateTime(); |
27
|
|
|
$container['foo'] = ['data' => $dateTime, 'expires' => PHP_INT_MAX]; |
28
|
|
|
$this->assertEquals($dateTime, $cache->get('foo')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
* @covers ::get |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function getKeyNotFound() |
38
|
|
|
{ |
39
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
40
|
|
|
$default = new \StdClass(); |
41
|
|
|
$this->assertSame($default, $cache->get('foo', $default)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
* @covers ::get |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function getExpired() |
51
|
|
|
{ |
52
|
|
|
$container = new ArrayObject(); |
53
|
|
|
$container['foo'] = ['data' => new DateTime(), 'expires' => -10]; |
54
|
|
|
$cache = new InMemoryCache($container); |
55
|
|
|
$this->assertNull($cache->get('foo')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
* @covers ::getMultiple |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function getMultple() |
65
|
|
|
{ |
66
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
67
|
|
|
$default = new \StdClass(); |
68
|
|
|
$dateTime = new \DateTime(); |
69
|
|
|
$exception = new \RuntimeException(); |
70
|
|
|
$cache->set('foo', $dateTime); |
71
|
|
|
$cache->set('bar', $exception); |
72
|
|
|
$actual = $cache->getMultiple(['foo', 'baz', 'bar'], $default); |
73
|
|
|
$this->assertEquals($dateTime, $actual['foo']); |
74
|
|
|
$this->assertSame($default, $actual['baz']); |
75
|
|
|
$this->assertEquals($exception, $actual['bar']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
* @covers ::set |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function setWithIntegerTTL() |
85
|
|
|
{ |
86
|
|
|
$container = new ArrayObject(); |
87
|
|
|
$cache = new InMemoryCache($container); |
88
|
|
|
$dateTime = new \DateTime(); |
89
|
|
|
$this->assertTrue($cache->set('foo', $dateTime, 3600)); |
90
|
|
|
$this->assertSame( |
91
|
|
|
[ |
92
|
|
|
'foo' => [ |
93
|
|
|
'data' => $dateTime, |
94
|
|
|
'expires' => time() + 3600, |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
$container->getArrayCopy() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
* @covers ::set |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setWithNullTTL() |
108
|
|
|
{ |
109
|
|
|
$dateTime = new \DateTime(); |
110
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
111
|
|
|
$this->assertTrue($cache->set('foo', $dateTime)); |
112
|
|
|
$this->assertEquals($dateTime, $cache->get('foo')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
* @covers ::setMultiple |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function setMultple() |
122
|
|
|
{ |
123
|
|
|
$ttl = \DateInterval::createFromDateString('1 day'); |
124
|
|
|
$dateTime = new \DateTime(); |
125
|
|
|
$exception = new \RuntimeException(); |
126
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
127
|
|
|
$this->assertTrue($cache->setMultiple(['foo' => $dateTime, 'bar' => $exception], $ttl)); |
128
|
|
|
$this->assertEquals($dateTime, $cache->get('foo')); |
129
|
|
|
$this->assertEquals($exception, $cache->get('bar')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
* @covers ::delete |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function delete() |
139
|
|
|
{ |
140
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
141
|
|
|
$dateTime = new DateTime(); |
142
|
|
|
$cache->set('foo', $dateTime); |
143
|
|
|
$cache->delete('foo'); |
144
|
|
|
$this->assertNull($cache->get('foo')); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @test |
149
|
|
|
* @covers ::deleteMultiple |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function deleteMultiple() |
154
|
|
|
{ |
155
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
156
|
|
|
$cache->set('foo', 'foo'); |
157
|
|
|
$cache->set('bar', 'bar'); |
158
|
|
|
$cache->set('baz', 'baz'); |
159
|
|
|
|
160
|
|
|
$cache->deleteMultiple(['foo', 'bar']); |
161
|
|
|
$this->assertNull($cache->get('foo')); |
162
|
|
|
$this->assertNull($cache->get('bar')); |
163
|
|
|
$this->assertSame('baz', $cache->get('baz')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @test |
168
|
|
|
* @covers ::clear |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function clear() |
173
|
|
|
{ |
174
|
|
|
$container = new ArrayObject(); |
175
|
|
|
$cache = new InMemoryCache($container); |
176
|
|
|
$cache->set('foo', 'foo'); |
177
|
|
|
$cache->set('bar', 'bar'); |
178
|
|
|
$cache->set('baz', 'baz'); |
179
|
|
|
|
180
|
|
|
$cache->clear(); |
181
|
|
|
|
182
|
|
|
$this->assertSame([], $container->getArrayCopy()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @test |
187
|
|
|
* @covers ::has |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function has() |
192
|
|
|
{ |
193
|
|
|
$cache = new InMemoryCache(new ArrayObject()); |
194
|
|
|
$cache->set('foo', 'foo'); |
195
|
|
|
$this->assertTrue($cache->has('foo')); |
196
|
|
|
$this->assertFalse($cache->has('bar')); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|