1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyDictionary\Dictionary; |
4
|
|
|
|
5
|
|
|
use EasyDictionary\Interfaces\DataProviderInterface; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @coversDefaultClass \EasyDictionary\Dictionary\Simple |
12
|
|
|
*/ |
13
|
|
|
class SimpleTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers \EasyDictionary\Dictionary\Simple |
17
|
|
|
*/ |
18
|
|
|
public function testClassExistence() |
19
|
|
|
{ |
20
|
|
|
self::assertTrue(class_exists('\EasyDictionary\Dictionary\Simple')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::setName |
26
|
|
|
* @covers ::setDataProvider |
27
|
|
|
* @covers ::getDataProvider |
28
|
|
|
* @covers ::getCache |
29
|
|
|
* @covers ::getData |
30
|
|
|
* @covers ::loadData |
31
|
|
|
*/ |
32
|
|
|
public function testGetDataWithDataProvider() |
33
|
|
|
{ |
34
|
|
|
$data = [0, 1, 2, 3]; |
35
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
36
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
37
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
38
|
|
|
|
39
|
|
|
$dictionary = new Simple(); |
40
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
41
|
|
|
self::assertEquals($data, $dictionary->getData()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers ::__construct |
46
|
|
|
* @covers ::setName |
47
|
|
|
* @covers ::getDataProvider |
48
|
|
|
* @covers ::getCache |
49
|
|
|
* @covers ::getData |
50
|
|
|
* @covers ::loadData |
51
|
|
|
*/ |
52
|
|
|
public function testGetDataWithoutDataProvider() |
53
|
|
|
{ |
54
|
|
|
$dictionary = new Simple(); |
55
|
|
|
self::assertEquals([], $dictionary->getData()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @covers ::__construct |
60
|
|
|
* @covers ::setName |
61
|
|
|
* @covers ::getName |
62
|
|
|
* @covers ::setCache |
63
|
|
|
* @covers ::setDataProvider |
64
|
|
|
* @covers ::getDataProvider |
65
|
|
|
* @covers ::getCache |
66
|
|
|
* @covers ::getData |
67
|
|
|
* @covers ::loadData |
68
|
|
|
*/ |
69
|
|
|
public function testSaveDataToCache() |
70
|
|
|
{ |
71
|
|
|
$data = [0, 1, 2, 3]; |
72
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
73
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
74
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
75
|
|
|
|
76
|
|
|
$cacheMock = $this->createMock(CacheInterface::class); |
77
|
|
|
$cacheMock->expects(self::once())->method('get'); |
78
|
|
|
$cacheMock->expects(self::once())->method('set')->with(Simple::class . '_test', $data, 3600); |
79
|
|
|
|
80
|
|
|
$dictionary = new Simple('test'); |
81
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
82
|
|
|
$dictionary->setCache($cacheMock); |
83
|
|
|
|
84
|
|
|
self::assertEquals($data, $dictionary->getData()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @covers ::__construct |
89
|
|
|
* @covers ::setName |
90
|
|
|
* @covers ::getName |
91
|
|
|
* @covers ::setCache |
92
|
|
|
* @covers ::setDataProvider |
93
|
|
|
* @covers ::getDataProvider |
94
|
|
|
* @covers ::getCache |
95
|
|
|
* @covers ::getData |
96
|
|
|
* @covers ::loadData |
97
|
|
|
*/ |
98
|
|
|
public function testSaveDataToCacheThrowError() |
99
|
|
|
{ |
100
|
|
|
$data = [0, 1, 2, 3]; |
101
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
102
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
103
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
104
|
|
|
|
105
|
|
|
$cacheMock = $this->createMock(CacheInterface::class); |
106
|
|
|
$cacheMock->expects(self::once())->method('get'); |
107
|
|
|
$cacheMock->expects(self::once())->method('set') |
108
|
|
|
->with(Simple::class . '_test', $data, 3600) |
109
|
|
|
->willThrowException(new InvalidArgException); |
110
|
|
|
|
111
|
|
|
$dictionary = new Simple('test'); |
112
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
113
|
|
|
$dictionary->setCache($cacheMock); |
114
|
|
|
|
115
|
|
|
self::assertEquals([], $dictionary->getData()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @covers ::__construct |
120
|
|
|
* @covers ::setName |
121
|
|
|
* @covers ::count |
122
|
|
|
* @covers ::setDataProvider |
123
|
|
|
* @covers ::getDataProvider |
124
|
|
|
* @covers ::getCache |
125
|
|
|
* @covers ::getData |
126
|
|
|
* @covers ::loadData |
127
|
|
|
*/ |
128
|
|
|
public function testDataCount() |
129
|
|
|
{ |
130
|
|
|
$data = [0, 1, 2, 3]; |
131
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
132
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
133
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
134
|
|
|
|
135
|
|
|
$dictionary = new Simple('test'); |
136
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
137
|
|
|
|
138
|
|
|
self::assertTrue($dictionary instanceof \Countable); |
139
|
|
|
self::assertEquals(4, count($dictionary)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @covers ::__construct |
144
|
|
|
* @covers ::setName |
145
|
|
|
* @covers ::getCache |
146
|
|
|
* @covers ::getData |
147
|
|
|
* @covers ::getDataProvider |
148
|
|
|
* @covers ::getDefaultView |
149
|
|
|
* @covers ::getIterator |
150
|
|
|
* @covers ::setDataProvider |
151
|
|
|
* @covers ::loadData |
152
|
|
|
*/ |
153
|
|
|
public function testDataIteratorWithoutView() |
154
|
|
|
{ |
155
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
156
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
157
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
158
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
159
|
|
|
|
160
|
|
|
$dictionary = new Simple('test'); |
161
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
162
|
|
|
|
163
|
|
|
$loadedData = []; |
164
|
|
|
foreach ($dictionary as $key => $value) { |
165
|
|
|
$loadedData[$key] = $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
self::assertEquals($data, $loadedData); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @covers ::__construct |
173
|
|
|
* @covers ::setName |
174
|
|
|
* @covers ::getCache |
175
|
|
|
* @covers ::getData |
176
|
|
|
* @covers ::getDataProvider |
177
|
|
|
* @covers ::setDataProvider |
178
|
|
|
* @covers ::getDefaultView |
179
|
|
|
* @covers ::setDefaultView |
180
|
|
|
* @covers ::getIterator |
181
|
|
|
* @covers ::loadData |
182
|
|
|
* @covers ::withView |
183
|
|
|
*/ |
184
|
|
|
public function testDataIteratorWithGoodView() |
185
|
|
|
{ |
186
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
187
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
188
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
189
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
190
|
|
|
|
191
|
|
|
$dictionary = new Simple('test'); |
192
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
193
|
|
|
$dictionary->setDefaultView(function ($rows) { |
194
|
|
|
foreach ($rows as $key => $row) { |
195
|
|
|
yield $key . ' ' . $row; |
196
|
|
|
} |
197
|
|
|
}); |
198
|
|
|
|
199
|
|
|
$loadedData = []; |
200
|
|
|
foreach ($dictionary as $value) { |
201
|
|
|
$loadedData[] = $value; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
self::assertEquals(['a 0', 'b 1', 'c 2'], $loadedData); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @covers ::__construct |
209
|
|
|
* @covers ::setName |
210
|
|
|
* @covers ::getCache |
211
|
|
|
* @covers ::getData |
212
|
|
|
* @covers ::getDataProvider |
213
|
|
|
* @covers ::setDataProvider |
214
|
|
|
* @covers ::getDefaultView |
215
|
|
|
* @covers ::getIterator |
216
|
|
|
* @covers ::loadData |
217
|
|
|
* @covers ::withView |
218
|
|
|
*/ |
219
|
|
|
public function testDataIteratorWithBadView() |
220
|
|
|
{ |
221
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
222
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
223
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
224
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
225
|
|
|
|
226
|
|
|
$dictionary = new Simple('test'); |
227
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
228
|
|
|
|
229
|
|
|
$loadedData = []; |
230
|
|
|
foreach ($dictionary->withView(null) as $key => $value) { |
231
|
|
|
$loadedData[$key] = $value; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
self::assertEquals($data, $loadedData); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @covers ::__construct |
239
|
|
|
* @covers ::setName |
240
|
|
|
* @covers ::getCache |
241
|
|
|
* @covers ::getData |
242
|
|
|
* @covers ::getDataProvider |
243
|
|
|
* @covers ::setDataProvider |
244
|
|
|
* @covers ::getDefaultView |
245
|
|
|
* @covers ::getIterator |
246
|
|
|
* @covers ::loadData |
247
|
|
|
* @covers ::setSearchFields |
248
|
|
|
* @covers ::search |
249
|
|
|
* @covers ::getSearchFields |
250
|
|
|
*/ |
251
|
|
|
public function testDataIteratorSearchInFlatData() |
252
|
|
|
{ |
253
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
254
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
255
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
256
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
257
|
|
|
|
258
|
|
|
$dictionary = new Simple('test'); |
259
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
260
|
|
|
|
261
|
|
|
$loadedData = []; |
262
|
|
|
foreach ($dictionary->search('/a|c/') as $key => $value) { |
263
|
|
|
$loadedData[$key] = $value; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
self::assertEquals(['a' => 0, 'c' => 2], $loadedData); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @covers ::__construct |
271
|
|
|
* @covers ::setName |
272
|
|
|
* @covers ::getCache |
273
|
|
|
* @covers ::getData |
274
|
|
|
* @covers ::getDataProvider |
275
|
|
|
* @covers ::setDataProvider |
276
|
|
|
* @covers ::getDefaultView |
277
|
|
|
* @covers ::getIterator |
278
|
|
|
* @covers ::loadData |
279
|
|
|
* @covers ::setSearchFields |
280
|
|
|
* @covers ::search |
281
|
|
|
* @covers ::setSearchFields |
282
|
|
|
* @covers ::getSearchFields |
283
|
|
|
*/ |
284
|
|
|
public function testDataIteratorSearchInArrayData() |
285
|
|
|
{ |
286
|
|
|
$data = ['a' => ['code' => 33, 'code2' => 44], 'b' => ['code' => 44]]; |
287
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
288
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
289
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
290
|
|
|
|
291
|
|
|
$dictionary = new Simple('test'); |
292
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
293
|
|
|
$dictionary->setSearchFields([ |
294
|
|
|
'code' => 1, |
295
|
|
|
'code2' => 0 |
296
|
|
|
]); |
297
|
|
|
|
298
|
|
|
self::assertEquals(['b' => ['code' => 44]], $dictionary->search('/44/')); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @covers ::__construct |
303
|
|
|
* @covers ::setName |
304
|
|
|
* @covers ::getCache |
305
|
|
|
* @covers ::getData |
306
|
|
|
* @covers ::getDataProvider |
307
|
|
|
* @covers ::setDataProvider |
308
|
|
|
* @covers ::getDefaultView |
309
|
|
|
* @covers ::getIterator |
310
|
|
|
* @covers ::loadData |
311
|
|
|
* @covers ::setSearchFields |
312
|
|
|
* @covers ::search |
313
|
|
|
* @covers ::getSearchFields |
314
|
|
|
* @covers ::offsetExists |
315
|
|
|
* @covers ::offsetGet |
316
|
|
|
* @covers ::offsetUnset |
317
|
|
|
* @covers ::offsetSet |
318
|
|
|
* @covers ::toArray |
319
|
|
|
*/ |
320
|
|
|
public function testDataIteratorGetOneKey() |
321
|
|
|
{ |
322
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
323
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
324
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
325
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
326
|
|
|
|
327
|
|
|
$dictionary = new Simple('test'); |
328
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
329
|
|
|
|
330
|
|
|
self::assertEquals(false, isset($dictionary['empty'])); |
331
|
|
|
self::assertEquals(true, empty($dictionary['empty'])); |
332
|
|
|
self::assertEquals(0, $dictionary['a']); |
333
|
|
|
self::assertEquals(1, $dictionary['b']); |
334
|
|
|
self::assertEquals(2, $dictionary['c']); |
335
|
|
|
|
336
|
|
|
unset($dictionary['c']); |
337
|
|
|
self::assertEquals(2, $dictionary['c']); |
338
|
|
|
|
339
|
|
|
$dictionary['c'] = 33; |
340
|
|
|
self::assertEquals(2, $dictionary['c']); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @covers ::toArray |
345
|
|
|
* @covers ::__construct |
346
|
|
|
* @covers ::getCache |
347
|
|
|
* @covers ::getData |
348
|
|
|
* @covers ::getDataProvider |
349
|
|
|
* @covers ::getDefaultView |
350
|
|
|
* @covers ::getIterator |
351
|
|
|
* @covers ::setDataProvider |
352
|
|
|
* @covers ::setName |
353
|
|
|
* @covers ::loadData |
354
|
|
|
* @covers ::withView |
355
|
|
|
*/ |
356
|
|
|
public function testDataIteratorToArray() |
357
|
|
|
{ |
358
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
359
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
360
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
361
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
362
|
|
|
|
363
|
|
|
$dictionary = new Simple('test'); |
364
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
365
|
|
|
|
366
|
|
|
self::assertEquals($data, $dictionary->toArray()); |
367
|
|
|
|
368
|
|
|
self::assertEquals(['a' => 0, 'b' => 2, 'c' => 4], $dictionary->toArray(function ($data) { |
369
|
|
|
foreach ($data as $key => $value) { |
370
|
|
|
yield $key => $value * 2; |
371
|
|
|
} |
372
|
|
|
})); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @covers ::toArray |
377
|
|
|
* @covers ::__construct |
378
|
|
|
* @covers ::getCache |
379
|
|
|
* @covers ::getData |
380
|
|
|
* @covers ::getDataProvider |
381
|
|
|
* @covers ::getDefaultView |
382
|
|
|
* @covers ::getIterator |
383
|
|
|
* @covers ::setDataProvider |
384
|
|
|
* @covers ::setName |
385
|
|
|
* @covers ::loadData |
386
|
|
|
*/ |
387
|
|
|
public function testDataKeys() |
388
|
|
|
{ |
389
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
390
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
391
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
392
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
393
|
|
|
|
394
|
|
|
$dictionary = new Simple('test'); |
395
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
396
|
|
|
|
397
|
|
|
$array = array_keys($dictionary->toArray()); |
398
|
|
|
self::assertEquals(['a', 'b', 'c'], $array); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @covers ::__construct |
403
|
|
|
* @covers ::setName |
404
|
|
|
* @covers ::getCache |
405
|
|
|
* @covers ::getData |
406
|
|
|
* @covers ::getDataProvider |
407
|
|
|
* @covers ::setDataProvider |
408
|
|
|
* @covers ::getDefaultView |
409
|
|
|
* @covers ::getIterator |
410
|
|
|
* @covers ::loadData |
411
|
|
|
* @covers ::setSearchFields |
412
|
|
|
* @covers ::search |
413
|
|
|
* @covers ::getSearchFields |
414
|
|
|
* @covers ::offsetGet |
415
|
|
|
* @covers ::toArray |
416
|
|
|
*/ |
417
|
|
|
public function testDataValues() |
418
|
|
|
{ |
419
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
420
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
421
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
422
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
423
|
|
|
|
424
|
|
|
$dictionary = new Simple('test'); |
425
|
|
|
$dictionary->setDataProvider($dataProviderMock); |
426
|
|
|
|
427
|
|
|
$array = array_values($dictionary->toArray()); |
428
|
|
|
self::assertEquals([0, 1, 2], $array); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
class InvalidArgException extends \Exception implements InvalidArgumentException |
433
|
|
|
{ |
434
|
|
|
|
435
|
|
|
} |
436
|
|
|
|