Passed
Push — master ( 20aba6...87e717 )
by Alexander
01:37
created

IterableDataReaderTest::testGeneratorAsDataSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Data\Tests\Reader;
5
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Data\Reader\IterableDataReader;
8
use Yiisoft\Data\Reader\Filter\All;
9
use Yiisoft\Data\Reader\Filter\Any;
10
use Yiisoft\Data\Reader\Filter\Equals;
11
use Yiisoft\Data\Reader\Filter\GreaterThan;
12
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
13
use Yiisoft\Data\Reader\Filter\In;
14
use Yiisoft\Data\Reader\Filter\LessThan;
15
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
16
use Yiisoft\Data\Reader\Filter\Like;
17
use Yiisoft\Data\Reader\Filter\Not;
18
use Yiisoft\Data\Reader\Sort;
19
20
final class IterableDataReaderTest extends TestCase
21
{
22
    private function getDataSet(): array
23
    {
24
        return [
25
            [
26
                'id' => 1,
27
                'name' => 'Codename Boris',
28
            ],
29
            [
30
                'id' => 2,
31
                'name' => 'Codename Doris',
32
            ],
33
            [
34
                'id' => 3,
35
                'name' => 'Agent K',
36
            ],
37
            [
38
                'id' => 5,
39
                'name' => 'Agent J',
40
            ],
41
            [
42
                'id' => 6,
43
                'name' => '007',
44
            ],
45
        ];
46
    }
47
48
    private function getDataSetSortedByName(): array
49
    {
50
        return [
51
            [
52
                'id' => 6,
53
                'name' => '007',
54
            ],
55
            [
56
                'id' => 5,
57
                'name' => 'Agent J',
58
            ],
59
            [
60
                'id' => 3,
61
                'name' => 'Agent K',
62
            ],
63
            [
64
                'id' => 1,
65
                'name' => 'Codename Boris',
66
            ],
67
            [
68
                'id' => 2,
69
                'name' => 'Codename Doris',
70
            ],
71
        ];
72
    }
73
74
    public function testWithLimitIsImmutable(): void
75
    {
76
        $reader = new IterableDataReader([]);
77
78
        $this->assertNotSame($reader, $reader->withLimit(1));
79
    }
80
81
    public function testLimitIsApplied(): void
82
    {
83
        $reader = (new IterableDataReader($this->getDataSet()))
84
            ->withLimit(5);
85
86
        $data = $reader->read();
87
88
        $this->assertCount(5, $data);
89
        $this->assertSame(array_slice($this->getDataSet(), 0, 5), $data);
90
    }
91
92
    public function testOffsetIsApplied(): void
93
    {
94
        $reader = (new IterableDataReader($this->getDataSet()))
95
            ->withOffset(2);
96
97
        $data = $reader->read();
98
99
        $this->assertCount(3, $data);
100
        $this->assertSame(array_slice($this->getDataSet(), 2, 3), $data);
101
    }
102
103
    public function testsWithSortIsImmutable(): void
104
    {
105
        $reader = new IterableDataReader([]);
106
107
        $this->assertNotSame($reader, $reader->withSort(null));
108
    }
109
110
    public function testSorting(): void
111
    {
112
        $sorting = new Sort([
113
            'id',
114
            'name'
115
        ]);
116
117
        $sorting = $sorting->withOrder(['name' => 'asc']);
118
119
        $reader = (new IterableDataReader($this->getDataSet()))
120
            ->withSort($sorting);
121
122
        $data = $reader->read();
123
124
        $this->assertSame($this->getDataSetSortedByName(), $data);
125
    }
126
127
    public function testCounting(): void
128
    {
129
        $reader = new IterableDataReader($this->getDataSet());
130
        $this->assertSame(5, $reader->count());
131
        $this->assertCount(5, $reader);
132
    }
133
134
    public function testsWithFilterIsImmutable(): void
135
    {
136
        $reader = new IterableDataReader([]);
137
138
        $this->assertNotSame($reader, $reader->withFilter(null));
139
    }
140
141
    public function testEqualsFiltering(): void
142
    {
143
        $filter = new Equals('id', 3);
144
        $reader = (new IterableDataReader($this->getDataSet()))
145
            ->withFilter($filter);
146
147
        $this->assertSame([
148
            [
149
                'id' => 3,
150
                'name' => 'Agent K',
151
            ],
152
        ], $reader->read());
153
    }
154
155
    public function testGreaterThanFiltering(): void
156
    {
157
        $filter = new GreaterThan('id', 3);
158
        $reader = (new IterableDataReader($this->getDataSet()))
159
            ->withFilter($filter);
160
161
        $this->assertSame([
162
            [
163
                'id' => 5,
164
                'name' => 'Agent J',
165
            ],
166
            [
167
                'id' => 6,
168
                'name' => '007',
169
            ],
170
        ], $reader->read());
171
    }
172
173
    public function testGreaterThanOrEqualFiltering(): void
174
    {
175
        $filter = new GreaterThanOrEqual('id', 3);
176
        $reader = (new IterableDataReader($this->getDataSet()))
177
            ->withFilter($filter);
178
179
        $this->assertSame([
180
            [
181
                'id' => 3,
182
                'name' => 'Agent K',
183
            ],
184
            [
185
                'id' => 5,
186
                'name' => 'Agent J',
187
            ],
188
            [
189
                'id' => 6,
190
                'name' => '007',
191
            ],
192
        ], $reader->read());
193
    }
194
195
    public function testLessThanFiltering(): void
196
    {
197
        $filter = new LessThan('id', 3);
198
        $reader = (new IterableDataReader($this->getDataSet()))
199
            ->withFilter($filter);
200
201
        $this->assertSame([
202
            [
203
                'id' => 1,
204
                'name' => 'Codename Boris',
205
            ],
206
            [
207
                'id' => 2,
208
                'name' => 'Codename Doris',
209
            ],
210
        ], $reader->read());
211
    }
212
213
    public function testLessThanOrEqualFiltering(): void
214
    {
215
        $filter = new LessThanOrEqual('id', 3);
216
        $reader = (new IterableDataReader($this->getDataSet()))
217
            ->withFilter($filter);
218
219
        $this->assertSame([
220
            [
221
                'id' => 1,
222
                'name' => 'Codename Boris',
223
            ],
224
            [
225
                'id' => 2,
226
                'name' => 'Codename Doris',
227
            ],
228
            [
229
                'id' => 3,
230
                'name' => 'Agent K',
231
            ],
232
        ], $reader->read());
233
    }
234
235
    public function testInFiltering(): void
236
    {
237
        $filter = new In('id', [1, 2]);
238
        $reader = (new IterableDataReader($this->getDataSet()))
239
            ->withFilter($filter);
240
241
        $this->assertSame([
242
            [
243
                'id' => 1,
244
                'name' => 'Codename Boris',
245
            ],
246
            [
247
                'id' => 2,
248
                'name' => 'Codename Doris',
249
            ],
250
        ], $reader->read());
251
    }
252
253
    public function testLikeFiltering(): void
254
    {
255
        $filter = new Like('name', 'agent');
256
        $reader = (new IterableDataReader($this->getDataSet()))
257
            ->withFilter($filter);
258
259
        $this->assertSame([
260
            [
261
                'id' => 3,
262
                'name' => 'Agent K',
263
            ],
264
            [
265
                'id' => 5,
266
                'name' => 'Agent J',
267
            ],
268
        ], $reader->read());
269
    }
270
271
    public function testNotFiltering(): void
272
    {
273
        $filter = new Not(new Equals('id', 1));
274
        $reader = (new IterableDataReader($this->getDataSet()))
275
            ->withFilter($filter);
276
277
        $this->assertSame([
278
            [
279
                'id' => 2,
280
                'name' => 'Codename Doris',
281
            ],
282
            [
283
                'id' => 3,
284
                'name' => 'Agent K',
285
            ],
286
            [
287
                'id' => 5,
288
                'name' => 'Agent J',
289
            ],
290
            [
291
                'id' => 6,
292
                'name' => '007',
293
            ],
294
        ], $reader->read());
295
    }
296
297
    public function testAnyFiltering(): void
298
    {
299
        $filter = new Any(
300
            new Equals('id', 1),
301
            new Equals('id', 2)
302
        );
303
        $reader = (new IterableDataReader($this->getDataSet()))
304
            ->withFilter($filter);
305
306
        $this->assertSame([
307
            [
308
                'id' => 1,
309
                'name' => 'Codename Boris',
310
            ],
311
            [
312
                'id' => 2,
313
                'name' => 'Codename Doris',
314
            ],
315
        ], $reader->read());
316
    }
317
318
    public function testAllFiltering(): void
319
    {
320
        $filter = new All(
321
            new GreaterThan('id', 3),
322
            new Like('name', 'agent')
323
        );
324
        $reader = (new IterableDataReader($this->getDataSet()))
325
            ->withFilter($filter);
326
327
        $this->assertSame([
328
            [
329
                'id' => 5,
330
                'name' => 'Agent J',
331
            ],
332
        ], $reader->read());
333
    }
334
335
    public function testLimitedSort(): void
336
    {
337
        $readerMin = (new IterableDataReader($this->getDataSet()))
338
            ->withSort(
339
                (new Sort(['id']))->withOrder(['id' => 'asc'])
340
            )
341
            ->withLimit(1);
342
        $min = $readerMin->read()[0]['id'];
343
        $this->assertSame(1, $min, 'Wrong min value found');
344
345
        $readerMax = (new IterableDataReader($this->getDataSet()))
346
            ->withSort(
347
                (new Sort(['id']))->withOrder(['id' => 'desc'])
348
            )
349
            ->withLimit(1);
350
        $max = $readerMax->read()[0]['id'];
351
        $this->assertSame(6, $max, 'Wrong max value found');
352
    }
353
354
    public function testFilteredCount(): void
355
    {
356
        $reader = new IterableDataReader($this->getDataSet());
357
        $total = count($reader);
358
359
        $this->assertSame(5, $total, 'Wrong count of elements');
360
361
        $reader = $reader->withFilter(new Like('name', 'agent'));
362
        $totalAgents = count($reader);
363
        $this->assertSame(2, $totalAgents, 'Wrong count of filtered elements');
364
    }
365
366
    public function testIteratorIteratorAsDataSet(): void
367
    {
368
        $reader = new IterableDataReader(new \ArrayIterator($this->getDataSet()));
369
        $sorting = new Sort([
370
            'id',
371
            'name'
372
        ]);
373
        $sorting = $sorting->withOrder(['name' => 'asc']);
374
        $this->assertSame($this->getDataSetSortedByName(), $reader->withSort($sorting)->read());
375
    }
376
377
    private function getDataSetAsGenerator(): \Generator
378
    {
379
        yield from $this->getDataSet();
380
    }
381
382
    public function testGeneratorAsDataSet(): void
383
    {
384
        $reader = new IterableDataReader($this->getDataSetAsGenerator());
385
        $sorting = new Sort([
386
            'id',
387
            'name'
388
        ]);
389
        $sorting = $sorting->withOrder(['name' => 'asc']);
390
        $this->assertSame($this->getDataSetSortedByName(), $reader->withSort($sorting)->read());
391
    }
392
}
393