Passed
Push — master ( 6137ca...01151b )
by Kevin
02:21
created

MatchableRepositoryTest   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 490
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 166
c 1
b 0
f 1
dl 0
loc 490
rs 9.6
wmc 35

35 Methods

Rating   Name   Duplication   Size   Complexity  
A not_found_exception_found_for_match_one_if_no_result() 0 7 1
A match_not_like_wildcard() 0 7 1
A match_greater_than() 0 9 1
A match_less_than_equal() 0 9 1
A match_not_in_string() 0 8 1
A match_not_in_numeric_string() 0 8 1
A match_not_equal() 0 9 1
A match_not_in_int() 0 8 1
A match_not_in_mixed_int_field() 0 8 1
A match_one_for_single_comparison() 0 7 1
A match_not_in_mixed_str_field() 0 9 1
A match_like_wildcard() 0 10 1
A match_equal() 0 8 1
A match_not_contains() 0 7 1
A match_not_ends_with() 0 9 1
A match_in_string() 0 9 1
A match_ends_with() 0 8 1
A match_and_x_composite() 0 13 1
A match_in_mixed_str_field() 0 8 1
A match_not_beginning_with() 0 7 1
A match_sort_asc() 0 9 1
A match_or_x_composite() 0 14 1
A match_in_mixed_int_field() 0 9 1
A match_like() 0 8 1
A match_in_numeric_string() 0 9 1
A match_not_like() 0 9 1
A match_composite_order_by() 0 14 1
A match_begins_with() 0 10 1
A match_is_null() 0 7 1
A match_sort_desc() 0 9 1
A match_contains() 0 10 1
A match_less_than() 0 9 1
A match_in_int() 0 9 1
A match_greater_than_equal() 0 9 1
A match_is_not_null() 0 7 1
1
<?php
2
3
namespace Zenstruck\Porpaginas\Tests\Doctrine\Repository;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\Porpaginas\Exception\NotFound;
7
use Zenstruck\Porpaginas\Matchable;
8
use Zenstruck\Porpaginas\Spec;
9
use Zenstruck\Porpaginas\Tests\Doctrine\HasEntityManager;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
abstract class MatchableRepositoryTest extends TestCase
15
{
16
    use HasEntityManager;
17
18
    /**
19
     * @test
20
     */
21
    public function match_and_x_composite(): void
22
    {
23
        $this->persistEntities(3);
24
25
        $objects = $this->createRepository()->match(
26
            Spec::andX(
27
                Spec::gt('id', 1),
28
                Spec::lt('id', 3)
29
            )
30
        );
31
32
        $this->assertCount(1, $objects);
33
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function match_or_x_composite(): void
40
    {
41
        $this->persistEntities(3);
42
43
        $objects = $this->createRepository()->match(
44
            Spec::orX(
45
                Spec::lt('id', 2),
46
                Spec::gt('id', 2)
47
            )
48
        );
49
50
        $this->assertCount(2, $objects);
51
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
52
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function match_like(): void
59
    {
60
        $this->persistEntities(3);
61
62
        $objects = $this->createRepository()->match(Spec::like('value', 'value 2'));
63
64
        $this->assertCount(1, $objects);
65
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function match_like_wildcard(): void
72
    {
73
        $this->persistEntities(3);
74
75
        $objects = $this->createRepository()->match(Spec::like('value', 'value *')->allowWildcard());
76
77
        $this->assertCount(3, $objects);
78
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
79
        $this->assertSame('value 2', \iterator_to_array($objects)[1]->value);
80
        $this->assertSame('value 3', \iterator_to_array($objects)[2]->value);
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function match_contains(): void
87
    {
88
        $this->persistEntities(3);
89
90
        $objects = $this->createRepository()->match(Spec::contains('value', 'value'));
91
92
        $this->assertCount(3, $objects);
93
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
94
        $this->assertSame('value 2', \iterator_to_array($objects)[1]->value);
95
        $this->assertSame('value 3', \iterator_to_array($objects)[2]->value);
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function match_begins_with(): void
102
    {
103
        $this->persistEntities(3);
104
105
        $objects = $this->createRepository()->match(Spec::beginsWith('value', 'v'));
106
107
        $this->assertCount(3, $objects);
108
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
109
        $this->assertSame('value 2', \iterator_to_array($objects)[1]->value);
110
        $this->assertSame('value 3', \iterator_to_array($objects)[2]->value);
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function match_ends_with(): void
117
    {
118
        $this->persistEntities(3);
119
120
        $objects = $this->createRepository()->match(Spec::endsWith('value', '2'));
121
122
        $this->assertCount(1, $objects);
123
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
124
    }
125
126
    /**
127
     * @test
128
     */
129
    public function match_not_like(): void
130
    {
131
        $this->persistEntities(3);
132
133
        $objects = $this->createRepository()->match(Spec::notLike('value', 'value 2'));
134
135
        $this->assertCount(2, $objects);
136
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
137
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
138
    }
139
140
    /**
141
     * @test
142
     */
143
    public function match_not_like_wildcard(): void
144
    {
145
        $this->persistEntities(3);
146
147
        $objects = $this->createRepository()->match(Spec::notLike('value', 'value *')->allowWildcard());
148
149
        $this->assertEmpty($objects);
150
    }
151
152
    /**
153
     * @test
154
     */
155
    public function match_not_contains(): void
156
    {
157
        $this->persistEntities(3);
158
159
        $objects = $this->createRepository()->match(Spec::notContains('value', 'value'));
160
161
        $this->assertEmpty($objects);
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function match_not_beginning_with(): void
168
    {
169
        $this->persistEntities(3);
170
171
        $objects = $this->createRepository()->match(Spec::notBeginningWith('value', 'value'));
172
173
        $this->assertEmpty($objects);
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function match_not_ends_with(): void
180
    {
181
        $this->persistEntities(3);
182
183
        $objects = $this->createRepository()->match(Spec::notEndingWith('value', '2'));
184
185
        $this->assertCount(2, $objects);
186
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
187
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
188
    }
189
190
    /**
191
     * @test
192
     */
193
    public function match_equal(): void
194
    {
195
        $this->persistEntities(3);
196
197
        $objects = $this->createRepository()->match(Spec::eq('value', 'value 2'));
198
199
        $this->assertCount(1, $objects);
200
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
201
    }
202
203
    /**
204
     * @test
205
     */
206
    public function match_not_equal(): void
207
    {
208
        $this->persistEntities(3);
209
210
        $objects = $this->createRepository()->match(Spec::neq('value', 'value 2'));
211
212
        $this->assertCount(2, $objects);
213
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
214
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
215
    }
216
217
    /**
218
     * @test
219
     */
220
    public function match_is_null(): void
221
    {
222
        $this->persistEntities(3);
223
224
        $objects = $this->createRepository()->match(Spec::isNull('value'));
225
226
        $this->assertEmpty($objects);
227
    }
228
229
    /**
230
     * @test
231
     */
232
    public function match_is_not_null(): void
233
    {
234
        $this->persistEntities(3);
235
236
        $objects = $this->createRepository()->match(Spec::isNotNull('value'));
237
238
        $this->assertCount(3, $objects);
239
    }
240
241
    /**
242
     * @test
243
     */
244
    public function match_in_string(): void
245
    {
246
        $this->persistEntities(3);
247
248
        $objects = $this->createRepository()->match(Spec::in('value', ['value 1', 'value 3']));
249
250
        $this->assertCount(2, $objects);
251
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
252
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
253
    }
254
255
    /**
256
     * @test
257
     */
258
    public function match_in_int(): void
259
    {
260
        $this->persistEntities(3);
261
262
        $objects = $this->createRepository()->match(Spec::in('id', [1, 3]));
263
264
        $this->assertCount(2, $objects);
265
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
266
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
267
    }
268
269
    /**
270
     * @test
271
     */
272
    public function match_in_numeric_string(): void
273
    {
274
        $this->persistEntities(3);
275
276
        $objects = $this->createRepository()->match(Spec::in('id', ['1', '3']));
277
278
        $this->assertCount(2, $objects);
279
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
280
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
281
    }
282
283
    /**
284
     * @test
285
     */
286
    public function match_in_mixed_str_field(): void
287
    {
288
        $this->persistEntities(3);
289
290
        $objects = $this->createRepository()->match(Spec::in('value', ['1', 'value 2', 3]));
291
292
        $this->assertCount(1, $objects);
293
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
294
    }
295
296
    /**
297
     * @test
298
     */
299
    public function match_in_mixed_int_field(): void
300
    {
301
        $this->persistEntities(3);
302
303
        $objects = $this->createRepository()->match(Spec::in('id', ['1', 'value 2', 3]));
304
305
        $this->assertCount(2, $objects);
306
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
307
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
308
    }
309
310
    /**
311
     * @test
312
     */
313
    public function match_not_in_string(): void
314
    {
315
        $this->persistEntities(3);
316
317
        $objects = $this->createRepository()->match(Spec::notIn('value', ['value 1', 'value 3']));
318
319
        $this->assertCount(1, $objects);
320
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
321
    }
322
323
    /**
324
     * @test
325
     */
326
    public function match_not_in_int(): void
327
    {
328
        $this->persistEntities(3);
329
330
        $objects = $this->createRepository()->match(Spec::notIn('id', [1, 3]));
331
332
        $this->assertCount(1, $objects);
333
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
334
    }
335
336
    /**
337
     * @test
338
     */
339
    public function match_not_in_numeric_string(): void
340
    {
341
        $this->persistEntities(3);
342
343
        $objects = $this->createRepository()->match(Spec::notIn('id', ['1', '3']));
344
345
        $this->assertCount(1, $objects);
346
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
347
    }
348
349
    /**
350
     * @test
351
     */
352
    public function match_not_in_mixed_str_field(): void
353
    {
354
        $this->persistEntities(3);
355
356
        $objects = $this->createRepository()->match(Spec::notIn('value', ['1', 'value 2', 3]));
357
358
        $this->assertCount(2, $objects);
359
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
360
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
361
    }
362
363
    /**
364
     * @test
365
     */
366
    public function match_not_in_mixed_int_field(): void
367
    {
368
        $this->persistEntities(3);
369
370
        $objects = $this->createRepository()->match(Spec::notIn('id', ['1', 'value 2', 3]));
371
372
        $this->assertCount(1, $objects);
373
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
374
    }
375
376
    /**
377
     * @test
378
     */
379
    public function match_less_than(): void
380
    {
381
        $this->persistEntities(3);
382
383
        $objects = $this->createRepository()->match(Spec::lt('id', 3));
384
385
        $this->assertCount(2, $objects);
386
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
387
        $this->assertSame('value 2', \iterator_to_array($objects)[1]->value);
388
    }
389
390
    /**
391
     * @test
392
     */
393
    public function match_less_than_equal(): void
394
    {
395
        $this->persistEntities(3);
396
397
        $objects = $this->createRepository()->match(Spec::lte('id', 2));
398
399
        $this->assertCount(2, $objects);
400
        $this->assertSame('value 1', \iterator_to_array($objects)[0]->value);
401
        $this->assertSame('value 2', \iterator_to_array($objects)[1]->value);
402
    }
403
404
    /**
405
     * @test
406
     */
407
    public function match_greater_than(): void
408
    {
409
        $this->persistEntities(3);
410
411
        $objects = $this->createRepository()->match(Spec::gt('id', 1));
412
413
        $this->assertCount(2, $objects);
414
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
415
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
416
    }
417
418
    /**
419
     * @test
420
     */
421
    public function match_greater_than_equal(): void
422
    {
423
        $this->persistEntities(3);
424
425
        $objects = $this->createRepository()->match(Spec::gte('id', 2));
426
427
        $this->assertCount(2, $objects);
428
        $this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
429
        $this->assertSame('value 3', \iterator_to_array($objects)[1]->value);
430
    }
431
432
    /**
433
     * @test
434
     */
435
    public function match_sort_desc(): void
436
    {
437
        $this->persistEntities(3);
438
439
        $objects = \iterator_to_array($this->createRepository()->match(Spec::sortDesc('value')));
440
441
        $this->assertSame('value 3', $objects[0]->value);
442
        $this->assertSame('value 2', $objects[1]->value);
443
        $this->assertSame('value 1', $objects[2]->value);
444
    }
445
446
    /**
447
     * @test
448
     */
449
    public function match_sort_asc(): void
450
    {
451
        $this->persistEntities(3);
452
453
        $objects = \iterator_to_array($this->createRepository()->match(Spec::sortAsc('value')));
454
455
        $this->assertSame('value 1', $objects[0]->value);
456
        $this->assertSame('value 2', $objects[1]->value);
457
        $this->assertSame('value 3', $objects[2]->value);
458
    }
459
460
    /**
461
     * @test
462
     */
463
    public function match_composite_order_by(): void
464
    {
465
        $this->persistEntities(3);
466
467
        $objects = \iterator_to_array($this->createRepository()->match(
468
            Spec::andX(
469
                Spec::gt('id', 1),
470
                Spec::sortDesc('id')
471
            )
472
        ));
473
474
        $this->assertCount(2, $objects);
475
        $this->assertSame('value 3', $objects[0]->value);
476
        $this->assertSame('value 2', $objects[1]->value);
477
    }
478
479
    /**
480
     * @test
481
     */
482
    public function match_one_for_single_comparison(): void
483
    {
484
        $this->persistEntities(3);
485
486
        $object = $this->createRepository()->matchOne(Spec::eq('value', 'value 2'));
487
488
        $this->assertSame('value 2', $object->value);
489
    }
490
491
    /**
492
     * @test
493
     */
494
    public function not_found_exception_found_for_match_one_if_no_result(): void
495
    {
496
        $this->persistEntities(3);
497
498
        $this->expectException(NotFound::class);
499
500
        $this->createRepository()->matchOne(Spec::eq('value', 'value 6'));
501
    }
502
503
    abstract protected function createRepository(): Matchable;
504
}
505