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 = self::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
|
|
View Code Duplication |
public function testSaveDataToCache() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$data = [0, 1, 2, 3]; |
72
|
|
|
$dataProviderMock = self::createMock(DataProviderInterface::class); |
73
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
74
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
75
|
|
|
|
76
|
|
|
$cacheMock = self::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
|
|
View Code Duplication |
public function testSaveDataToCacheThrowError() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$data = [0, 1, 2, 3]; |
101
|
|
|
$dataProviderMock = self::createMock(DataProviderInterface::class); |
102
|
|
|
$dataProviderMock->method('getData')->willReturn($data); |
103
|
|
|
$dataProviderMock->expects(self::once())->method('getData'); |
104
|
|
|
|
105
|
|
|
$cacheMock = self::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 = self::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
|
|
View Code Duplication |
public function testDataIteratorWithoutView() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
156
|
|
|
$dataProviderMock = self::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 = self::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
|
|
View Code Duplication |
public function testDataIteratorWithBadView() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$data = ['a' => 0, 'b' => 1, 'c' => 2]; |
222
|
|
|
$dataProviderMock = self::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 = self::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 = self::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
|
|
|
class InvalidArgException extends \Exception implements InvalidArgumentException |
|
|
|
|
303
|
|
|
{ |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|