-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheTest.php
More file actions
240 lines (212 loc) · 6.31 KB
/
CacheTest.php
File metadata and controls
240 lines (212 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Gt\FileCache\Test;
use Gt\FileCache\Cache;
use Gt\FileCache\FileAccess;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
use SplFixedArray;
use stdClass;
use TypeError;
class CacheTest extends TestCase {
public function tearDown():void {
exec("rm -rf " . sys_get_temp_dir() . "/phpgt-filecache");
}
public function testGet_expectedValueReturned():void {
$sut = $this->getSut();
$value = "test-value";
$result = $sut->get("test", fn() => $value);
self::assertSame($value, $result);
}
public function testGet_expectedValueReturnedOnMultipleCalls():void {
$sut = $this->getSut();
$value = "test-value";
$result = $sut->get("test", fn() => $value);
self::assertSame($value, $result);
$result = $sut->get("test", fn() => $value);
self::assertSame($value, $result);
}
public function testGet_multipleCallsDoesNotCallbackMultipleTimes():void {
$sut = $this->getSut();
$name = uniqid("test-");
$value = "test-value-123";
$count = 0;
$callback = function()use($value, &$count):string {
$count++;
return $value;
};
$result = $sut->get($name, $callback);
self::assertSame($value, $result);
self::assertSame(1, $count);
$result = $sut->get($name, $callback);
self::assertSame($value, $result);
self::assertSame(1, $count);
}
public function testGetString():void {
$value = uniqid();
$sut = $this->getSut([
"test" => $value,
]);
self::assertSame($value, $sut->getString("test", fn() => "new"));
}
public function testGetInt():void {
$value = (string)rand(100,999);
$sut = $this->getSut([
"test" => $value,
]);
self::assertSame((int)$value, $sut->getInt("test", fn() => 2));
}
public function testGetFoat():void {
$value = (string)(rand(100,999) * 3.14159);
$sut = $this->getSut([
"test" => $value,
]);
self::assertSame((float)$value, $sut->getFloat("test", fn() => 3.141));
}
public function testGetBool():void {
$value = "yes";
$sut = $this->getSut([
"test" => $value,
]);
self::assertTrue($sut->getBool("test", fn() => false));
}
public function testGetDateTime():void {
$value = "1988-04-05";
$sut = $this->getSut([
"test" => $value,
]);
self::assertSame($value, $sut->getDateTime("test", fn() => 123)->format("Y-m-d"));
}
public function testGetInstance():void {
$value = new StdClass();
$value->name = uniqid();
$sut = $this->getSut([
"test" => $value,
]);
$class = $sut->getInstance("test", StdClass::class, fn() => false);
self::assertSame($value->name, $class->name);
}
public function testGetInstance_error():void {
$value = new StdClass();
$value->name = uniqid();
$sut = $this->getSut([
"test" => $value,
]);
self::expectException(TypeError::class);
self::expectExceptionMessage("Value is not an instance of SplFileInfo");
$sut->getInstance("test", SplFileInfo::class, fn() => false);
}
public function testGetArray():void {
$value = [1, 2, 3];
$sut = $this->getSut([
"numbers" => $value,
]);
self::assertSame($value, $sut->getArray("numbers", fn() => []));
}
public function testGetArray_notArray():void {
$value = (object)[1, 2, 3];
$sut = $this->getSut([
"numbers" => $value,
]);
self::expectException(TypeError::class);
self::expectExceptionMessage("Data 'numbers' is not an array");
$sut->getArray("numbers", fn() => []);
}
public function testGetTypedArray_notArray():void {
$value = (object)[1, 2, 3];
$sut = $this->getSut([
"numbers" => $value,
]);
self::expectException(TypeError::class);
self::expectExceptionMessage("Data 'numbers' is not an array");
$sut->getTypedArray("numbers", "int", fn() => []);
}
public function testGetTypedArray_int():void {
$value = [1, "2", 3.000];
$sut = $this->getSut([
"numbers" => $value,
]);
$typedArray = $sut->getTypedArray("numbers", "int", fn() => []);
foreach($typedArray as $value) {
self::assertIsInt($value);
}
}
public function testGetTypedArray_intFailure():void {
$value = [1, "2", 3.000, "four"];
$sut = $this->getSut([
"numbers" => $value,
]);
self::expectException(TypeError::class);
$sut->getTypedArray("numbers", "int", fn() => []);
}
public function testGetTypedArray_float():void {
$value = [1, "2", 3.000];
$sut = $this->getSut([
"numbers" => $value,
]);
$typedArray = $sut->getTypedArray("numbers", "float", fn() => []);
foreach($typedArray as $value) {
self::assertIsFloat($value);
}
}
public function testGetTypedArray_floatFailure():void {
$value = [1, "2", 3.000, "four"];
$sut = $this->getSut([
"numbers" => $value,
]);
self::expectException(TypeError::class);
$sut->getTypedArray("numbers", "float", fn() => []);
}
public function testGetTypedArray_string():void {
$value = [1, "2", 3.000, "four"];
$sut = $this->getSut([
"numbers" => $value,
]);
$typedArray= $sut->getTypedArray("numbers", "string", fn() => []);
foreach($typedArray as $value) {
self::assertIsString($value);
}
}
public function testGetTypedArray_bool():void {
$value = [0, "1", false, true, [], new StdClass()];
$sut = $this->getSut([
"booleans" => $value,
]);
$typedArray= $sut->getTypedArray("booleans", "bool", fn() => []);
foreach($typedArray as $i => $value) {
self::assertSame((bool)($i % 2), $value, $i);
}
}
public function testGetTypedArray_class():void {
$value = [new SplFileInfo(__FILE__), new SplFileInfo(__DIR__)];
$sut = $this->getSut([
"files" => $value,
]);
$typedArray= $sut->getTypedArray("files", SplFileInfo::class, fn() => []);
foreach($typedArray as $value) {
self::assertInstanceOf(SplFileInfo::class, $value);
}
}
public function testGetTypedArray_classError():void {
$value = [new SplFileInfo(__FILE__), new SplFixedArray(), new SplFileInfo(__DIR__)];
$sut = $this->getSut([
"files" => $value,
]);
self::expectExceptionMessage("Array value at key '1' is not an instance of SplFileInfo");
self::expectException(TypeError::class);
$sut->getTypedArray("files", SplFileInfo::class, fn() => []);
}
private function getSut(array $mockFiles = []):Cache {
$mockFileAccess = null;
if(!empty($mockFiles)) {
$mockFileAccess = self::createMock(FileAccess::class);
foreach($mockFiles as $key => $value) {
$mockFileAccess->method("getData")
->with($key)->willReturn($value);
}
}
return new Cache(
sys_get_temp_dir() . "/phpgt-filecache",
fileAccess: $mockFileAccess,
);
}
}