-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathIterableDataReaderTest.php
More file actions
530 lines (445 loc) · 14.6 KB
/
IterableDataReaderTest.php
File metadata and controls
530 lines (445 loc) · 14.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
declare(strict_types=1);
namespace Yiisoft\Data\Tests\Reader\Iterable;
use ArrayIterator;
use Generator;
use InvalidArgumentException;
use LogicException;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\AndX;
use Yiisoft\Data\Reader\Filter\OrX;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Filter\GreaterThan;
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
use Yiisoft\Data\Reader\Filter\In;
use Yiisoft\Data\Reader\Filter\LessThan;
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
use Yiisoft\Data\Reader\Filter\Like;
use Yiisoft\Data\Reader\Filter\Not;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Data\Tests\Support\CustomFilter\Digital;
use Yiisoft\Data\Tests\Support\CustomFilter\DigitalHandler;
use Yiisoft\Data\Tests\Support\CustomFilter\FilterWithoutHandler;
use Yiisoft\Data\Tests\TestCase;
use function array_slice;
use function array_values;
use function count;
final class IterableDataReaderTest extends TestCase
{
private const ITEM_1 = [
'id' => 1,
'name' => 'Codename Boris',
];
private const ITEM_2 = [
'id' => 2,
'name' => 'Codename Doris',
];
private const ITEM_3 = [
'id' => 3,
'name' => 'Agent K',
];
private const ITEM_4 = [
'id' => 5,
'name' => 'Agent J',
];
private const ITEM_5 = [
'id' => 6,
'name' => '007',
];
private const DEFAULT_DATASET = [
0 => self::ITEM_1,
1 => self::ITEM_2,
2 => self::ITEM_3,
3 => self::ITEM_4,
4 => self::ITEM_5,
];
public function testImmutability(): void
{
$reader = new IterableDataReader([]);
$this->assertNotSame($reader, $reader->withFilter(new All()));
$this->assertNotSame($reader, $reader->withSort(null));
$this->assertNotSame($reader, $reader->withOffset(1));
$this->assertNotSame($reader, $reader->withLimit(1));
}
public function testWithLimitFailForNegativeValues(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The limit must not be less than 0.');
(new IterableDataReader([]))->withLimit(-1);
}
public function testLimitIsApplied(): void
{
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withLimit(5);
$data = $reader->read();
$this->assertCount(5, $data);
$this->assertSame(array_slice(self::DEFAULT_DATASET, 0, 5), $data);
}
public function testOffsetIsApplied(): void
{
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withOffset(2);
$data = $reader->read();
$this->assertCount(3, $data);
$this->assertSame(
[
2 => self::ITEM_3,
3 => self::ITEM_4,
4 => self::ITEM_5,
],
$data,
);
$this->assertSame(2, $reader->getOffset());
}
public function testAscSorting(): void
{
$sorting = Sort::only([
'id',
'name',
]);
$sorting = $sorting->withOrder(['name' => 'asc']);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withSort($sorting);
$data = $reader->read();
$this->assertSame($this->getDataSetAscSortedByName(), $data);
}
public function testDescSorting(): void
{
$sorting = Sort::only([
'id',
'name',
]);
$sorting = $sorting->withOrder(['name' => 'desc']);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withSort($sorting);
$data = $reader->read();
$this->assertSame($this->getDataSetDescSortedByName(), $data);
}
public function testCounting(): void
{
$reader = new IterableDataReader(self::DEFAULT_DATASET);
$this->assertSame(5, $reader->count());
$this->assertCount(5, $reader);
}
public function testCountWithLimit(): void
{
$reader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(2);
$this->assertSame(5, $reader->count());
$this->assertCount(5, $reader);
}
public function testCountWithOffset(): void
{
$reader = (new IterableDataReader(self::DEFAULT_DATASET))->withOffset(3);
$this->assertSame(5, $reader->count());
$this->assertCount(5, $reader);
}
public function testReadOne(): void
{
$data = self::DEFAULT_DATASET;
$reader = new IterableDataReader($data);
$this->assertSame($data[0], $reader->readOne());
}
public function testReadOneWithSortingAndOffset(): void
{
$sorting = Sort::only(['id', 'name'])->withOrder(['name' => 'asc']);
$data = self::DEFAULT_DATASET;
$reader = (new IterableDataReader($data))
->withSort($sorting)
->withOffset(2);
$this->assertSame($this->getDataSetAscSortedByName()[2], $reader->readOne());
}
public function testEqualsFiltering(): void
{
$filter = new Equals('id', 3);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
2 => self::ITEM_3,
], $reader->read());
}
public function testGreaterThanFiltering(): void
{
$filter = new GreaterThan('id', 3);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
3 => self::ITEM_4,
4 => self::ITEM_5,
], $reader->read());
}
public function testGreaterThanOrEqualFiltering(): void
{
$filter = new GreaterThanOrEqual('id', 3);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
2 => self::ITEM_3,
3 => self::ITEM_4,
4 => self::ITEM_5,
], $reader->read());
}
public function testLessThanFiltering(): void
{
$filter = new LessThan('id', 3);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
0 => self::ITEM_1,
1 => self::ITEM_2,
], $reader->read());
}
public function testLessThanOrEqualFiltering(): void
{
$filter = new LessThanOrEqual('id', 3);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
0 => self::ITEM_1,
1 => self::ITEM_2,
2 => self::ITEM_3,
], $reader->read());
}
public function testInFiltering(): void
{
$filter = new In('id', [1, 2]);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
0 => self::ITEM_1,
1 => self::ITEM_2,
], $reader->read());
}
public function testLikeFiltering(): void
{
$filter = new Like('name', 'Agent');
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
2 => self::ITEM_3,
3 => self::ITEM_4,
], $reader->read());
}
public function testNotFiltering(): void
{
$filter = new Not(new Equals('id', 1));
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
1 => self::ITEM_2,
2 => self::ITEM_3,
3 => self::ITEM_4,
4 => self::ITEM_5,
], $reader->read());
}
public function testOrXFiltering(): void
{
$filter = new OrX(
new Equals('id', 1),
new Equals('id', 2),
);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
0 => self::ITEM_1,
1 => self::ITEM_2,
], $reader->read());
}
public function testAndXFiltering(): void
{
$filter = new AndX(
new GreaterThan('id', 3),
new Like('name', 'Agent'),
);
$reader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter($filter);
$this->assertSame([
3 => self::ITEM_4,
], $reader->read());
}
public function testLimitedSort(): void
{
$readerMin = (new IterableDataReader(self::DEFAULT_DATASET))
->withSort(
Sort::only(['id'])->withOrder(['id' => 'asc']),
)
->withLimit(1);
$min = $readerMin->read()[0]['id'];
$this->assertSame(1, $min, 'Wrong min value found');
$readerMax = (new IterableDataReader(self::DEFAULT_DATASET))
->withSort(
Sort::only(['id'])->withOrder(['id' => 'desc']),
)
->withLimit(1);
$max = $readerMax->readOne()['id'];
$this->assertSame(6, $max, 'Wrong max value found');
}
public function testFilteredCount(): void
{
$reader = new IterableDataReader(self::DEFAULT_DATASET);
$total = count($reader);
$this->assertSame(5, $total, 'Wrong count of elements');
$reader = $reader->withFilter(new Like('name', 'Agent'));
$totalAgents = count($reader);
$this->assertSame(2, $totalAgents, 'Wrong count of filtered elements');
}
public function testIteratorIteratorAsDataSet(): void
{
$reader = new IterableDataReader(new ArrayIterator(self::DEFAULT_DATASET));
$sorting = Sort::only([
'id',
'name',
]);
$sorting = $sorting->withOrder(['name' => 'asc']);
$this->assertSame(
$this->getDataSetAscSortedByName(),
$reader
->withSort($sorting)
->read(),
);
}
public function testGeneratorAsDataSet(): void
{
$reader = new IterableDataReader($this->getDataSetAsGenerator());
$sorting = Sort::only([
'id',
'name',
]);
$sorting = $sorting->withOrder(['name' => 'asc']);
$this->assertSame(
$this->getDataSetAscSortedByName(),
$reader
->withSort($sorting)
->read(),
);
}
public function testCustomFilter(): void
{
$filter = new AndX(new GreaterThan('id', 0), new Digital('name'));
$reader = (new IterableDataReader(self::DEFAULT_DATASET, addFilterHandlers: [new DigitalHandler()]))
->withFilter($filter);
$filtered = $reader->read();
$this->assertSame([4 => self::ITEM_5], $filtered);
$this->assertSame($filter, $reader->getFilter());
}
public function testCustomEqualsProcessor(): void
{
$sort = Sort::only(['id', 'name'])->withOrderString('id');
$customEquals = new class implements IterableFilterHandlerInterface {
public function getFilterClass(): string
{
return Equals::class;
}
public function match(
array|object $item,
FilterInterface $filter,
Context $context,
): bool {
/** @var Equals $filter */
return $item[$filter->field] === 2;
}
};
$dataReader = (new IterableDataReader(self::DEFAULT_DATASET, addFilterHandlers: [$customEquals]))
->withSort($sort);
$dataReader = $dataReader->withFilter(new Equals('id', 100));
$expected = [self::ITEM_2];
$this->assertSame($expected, array_values($this->iterableToArray($dataReader->read())));
}
public function testNotSupportedFilter(): void
{
$dataReader = (new IterableDataReader(self::DEFAULT_DATASET))
->withFilter(new FilterWithoutHandler());
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Filter "' . FilterWithoutHandler::class . '" is not supported.');
$dataReader->read();
}
public function testArrayOfObjects(): void
{
$data = [
'one' => new class {
public int $a = 1;
},
'two' => new class {
public int $a = 2;
},
'three' => new class {
public int $a = 3;
},
];
$reader = new IterableDataReader($data);
$rows = $reader->withFilter(new In('a', [2, 3]))->read();
$this->assertSame(['two', 'three'], array_keys($rows));
$this->assertSame(2, $rows['two']->a);
$this->assertSame(3, $rows['three']->a);
}
public function testSortingWithSameValues(): void
{
$data = [
0 => ['value' => 1],
1 => ['value' => 2],
2 => ['value' => 3],
3 => ['value' => 2],
];
$reader = (new IterableDataReader($data))
->withSort(
Sort::any()->withOrder(['value' => 'asc']),
);
$this->assertSame(
[
0 => ['value' => 1],
1 => ['value' => 2],
3 => ['value' => 2],
2 => ['value' => 3],
],
$reader->read(),
);
}
public function testWithLimitZero(): void
{
$data = [
0 => ['value' => 1],
1 => ['value' => 2],
2 => ['value' => 3],
3 => ['value' => 2],
];
$reader = (new IterableDataReader($data))
->withLimit(2)
->withLimit(0);
$this->assertSame(
[
0 => ['value' => 1],
1 => ['value' => 2],
2 => ['value' => 3],
3 => ['value' => 2],
],
$reader->read(),
);
}
private function getDataSetAsGenerator(): Generator
{
yield from self::DEFAULT_DATASET;
}
private function getDataSetAscSortedByName(): array
{
return [
4 => self::ITEM_5,
3 => self::ITEM_4,
2 => self::ITEM_3,
0 => self::ITEM_1,
1 => self::ITEM_2,
];
}
private function getDataSetDescSortedByName(): array
{
return [
1 => self::ITEM_2,
0 => self::ITEM_1,
2 => self::ITEM_3,
3 => self::ITEM_4,
4 => self::ITEM_5,
];
}
}