1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EloquentRepositoryCriteriaTests extends AbstractEloquentTests |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function testObjectCriterionPush() |
6
|
|
|
{ |
7
|
|
|
$userRepository = $this->userRepository(); |
8
|
|
|
$criterion = new FirstTestCriterion(); |
9
|
|
|
$userRepository->pushCriterion($criterion); |
10
|
|
|
|
11
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
12
|
|
|
$this->assertAttributeContains($criterion, 'criteria', $userRepository); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function testClosureCriterionPush() |
16
|
|
|
{ |
17
|
|
|
$userRepository = $this->userRepository(); |
18
|
|
|
$criterion = function ($query, $repository) { |
|
|
|
|
19
|
|
|
return $query->where('id', 1); |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
$userRepository->pushCriterion($criterion); |
23
|
|
|
|
24
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
25
|
|
|
$this->assertAttributeContains($criterion, 'criteria', $userRepository); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testClassNameCriterionPush() |
29
|
|
|
{ |
30
|
|
|
$userRepository = $this->userRepository(); |
31
|
|
|
$criterion = FirstTestCriterion::class; |
32
|
|
|
$userRepository->pushCriterion($criterion); |
33
|
|
|
|
34
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
35
|
|
|
$this->assertContainsOnlyInstancesOf($criterion, $this->getObjectAttribute($userRepository, 'criteria')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testPushTwoDifferentCriteria() |
39
|
|
|
{ |
40
|
|
|
$userRepository = $this->userRepository(); |
41
|
|
|
$firstCriterion = FirstTestCriterion::class; |
42
|
|
|
$secondCriterion = SecondTestCriterion::class; |
43
|
|
|
|
44
|
|
|
$userRepository->pushCriterion($firstCriterion); |
45
|
|
|
$userRepository->pushCriterion($secondCriterion); |
46
|
|
|
|
47
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testPushTwoDifferentClosureCriteria() |
51
|
|
|
{ |
52
|
|
|
$userRepository = $this->userRepository(); |
53
|
|
|
$firstCriterion = function ($query, $repository) { |
|
|
|
|
54
|
|
|
return $query->where('id', 1); |
55
|
|
|
}; |
56
|
|
|
$secondCriterion = function ($query, $repository) { |
|
|
|
|
57
|
|
|
return $query->where('id', 2); |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
$userRepository->pushCriterion($firstCriterion); |
61
|
|
|
$userRepository->pushCriterion($secondCriterion); |
62
|
|
|
|
63
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testPushTwoSimilarCriteria() |
67
|
|
|
{ |
68
|
|
|
$userRepository = $this->userRepository(); |
69
|
|
|
$firstCriterion = FirstTestCriterion::class; |
70
|
|
|
|
71
|
|
|
$userRepository->pushCriterion($firstCriterion); |
72
|
|
|
$userRepository->pushCriterion($firstCriterion); |
73
|
|
|
|
74
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testPushCriteria() |
78
|
|
|
{ |
79
|
|
|
$userRepository = $this->userRepository(); |
80
|
|
|
|
81
|
|
|
$firstCriterion = FirstTestCriterion::class; |
82
|
|
|
$secondCriterion = new SecondTestCriterion(); |
83
|
|
|
$thirdCriterion = function ($query, $repository) { |
|
|
|
|
84
|
|
|
return $query->where('id', 1); |
85
|
|
|
}; |
86
|
|
|
|
87
|
|
|
$userRepository->pushCriteria([ |
88
|
|
|
$firstCriterion, |
89
|
|
|
$secondCriterion, |
90
|
|
|
$thirdCriterion, |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$this->assertAttributeCount(3, 'criteria', $userRepository); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testRemoveCriterionWhenPushedByClassName() |
97
|
|
|
{ |
98
|
|
|
$userRepository = $this->userRepository(); |
99
|
|
|
$firstCriterion = FirstTestCriterion::class; |
100
|
|
|
$secondCriterion = SecondTestCriterion::class; |
101
|
|
|
|
102
|
|
|
$userRepository->pushCriterion($firstCriterion); |
103
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
104
|
|
|
|
105
|
|
|
$userRepository->removeCriterion($firstCriterion); |
106
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
107
|
|
|
|
108
|
|
|
$userRepository->pushCriterion($firstCriterion); |
109
|
|
|
$userRepository->pushCriterion($secondCriterion); |
110
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
111
|
|
|
|
112
|
|
|
$userRepository->removeCriterion($firstCriterion); |
113
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
114
|
|
|
$userRepository->removeCriterion($firstCriterion); |
115
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
116
|
|
|
$userRepository->removeCriterion($secondCriterion); |
117
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testRemoveCriterionWhenPushedObject() |
121
|
|
|
{ |
122
|
|
|
$userRepository = $this->userRepository(); |
123
|
|
|
$firstCriterion = new FirstTestCriterion(); |
124
|
|
|
$secondCriterion = new SecondTestCriterion(); |
125
|
|
|
|
126
|
|
|
$userRepository->pushCriterion($firstCriterion); |
127
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
128
|
|
|
|
129
|
|
|
$userRepository->removeCriterion($firstCriterion); |
130
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
131
|
|
|
|
132
|
|
|
$userRepository->pushCriterion($firstCriterion); |
133
|
|
|
$userRepository->pushCriterion($secondCriterion); |
134
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
135
|
|
|
|
136
|
|
|
$userRepository->removeCriterion($firstCriterion); |
137
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
138
|
|
|
$userRepository->removeCriterion($firstCriterion); |
139
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
140
|
|
|
$userRepository->removeCriterion($secondCriterion); |
141
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
142
|
|
|
|
143
|
|
|
$userRepository->pushCriterion($firstCriterion); |
144
|
|
|
$userRepository->removeCriterion(FirstTestCriterion::class); |
145
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testRemoveCriterionWhenPushedClosures() |
149
|
|
|
{ |
150
|
|
|
$userRepository = $this->userRepository(); |
151
|
|
|
$firstCriterion = function ($query, $repository) { |
|
|
|
|
152
|
|
|
return $query->where('id', 1); |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
$secondCriterion = function ($query, $repository) { |
|
|
|
|
156
|
|
|
return $query->where('id', 2); |
157
|
|
|
}; |
158
|
|
|
|
159
|
|
|
$userRepository->pushCriterion($firstCriterion); |
160
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
161
|
|
|
|
162
|
|
|
$userRepository->removeCriterion($firstCriterion); |
163
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
164
|
|
|
|
165
|
|
|
$userRepository->pushCriterion($firstCriterion); |
166
|
|
|
$userRepository->pushCriterion($secondCriterion); |
167
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
168
|
|
|
|
169
|
|
|
$userRepository->removeCriterion($firstCriterion); |
170
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
171
|
|
|
$userRepository->removeCriterion($firstCriterion); |
172
|
|
|
$this->assertAttributeCount(1, 'criteria', $userRepository); |
173
|
|
|
$userRepository->removeCriterion($secondCriterion); |
174
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testRemoveCriteria() |
178
|
|
|
{ |
179
|
|
|
$userRepository = $this->userRepository(); |
180
|
|
|
$firstCriterion = function ($query, $repository) { |
|
|
|
|
181
|
|
|
return $query->where('id', 1); |
182
|
|
|
}; |
183
|
|
|
|
184
|
|
|
$secondCriterion = function ($query, $repository) { |
|
|
|
|
185
|
|
|
return $query->where('id', 2); |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
$userRepository->pushCriterion($firstCriterion); |
189
|
|
|
$userRepository->pushCriterion($secondCriterion); |
190
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
191
|
|
|
|
192
|
|
|
$userRepository->removeCriteria([$firstCriterion, $secondCriterion]); |
193
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
194
|
|
|
|
195
|
|
|
$userRepository->pushCriterion($firstCriterion); |
196
|
|
|
$userRepository->pushCriterion($secondCriterion); |
197
|
|
|
$userRepository->removeCriteria([$firstCriterion]); |
198
|
|
|
$userRepository->removeCriteria([$secondCriterion]); |
199
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testFlushCriteria() |
203
|
|
|
{ |
204
|
|
|
$userRepository = $this->userRepository(); |
205
|
|
|
|
206
|
|
|
$userRepository->pushCriteria([ |
207
|
|
|
new FirstTestCriterion(), |
208
|
|
|
new SecondTestCriterion(), |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
$this->assertAttributeCount(2, 'criteria', $userRepository); |
212
|
|
|
$userRepository->flushCriteria(); |
213
|
|
|
$this->assertAttributeCount(0, 'criteria', $userRepository); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testSetDefaultCriteria() |
217
|
|
|
{ |
218
|
|
|
$userRepository = $this->userRepository(); |
219
|
|
|
|
220
|
|
|
$userRepository->setDefaultCriteria([ |
221
|
|
|
new FirstTestCriterion(), |
222
|
|
|
new SecondTestCriterion(), |
223
|
|
|
]); |
224
|
|
|
|
225
|
|
|
$this->assertAttributeCount(2, 'defaultCriteria', $userRepository); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testGetDefaultCriteria() |
229
|
|
|
{ |
230
|
|
|
$userRepository = $this->userRepository(); |
231
|
|
|
$criteria = [ |
232
|
|
|
new FirstTestCriterion(), |
233
|
|
|
new SecondTestCriterion(), |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
$userRepository->setDefaultCriteria($criteria); |
237
|
|
|
$defaultCriteria = $userRepository->getDefaultCriteria(); |
238
|
|
|
|
239
|
|
|
$this->assertArrayHasKey(FirstTestCriterion::class, $defaultCriteria); |
240
|
|
|
$this->assertArrayHasKey(SecondTestCriterion::class, $defaultCriteria); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testGetCriteria() |
244
|
|
|
{ |
245
|
|
|
$userRepository = $this->userRepository(); |
246
|
|
|
$criteria = [ |
247
|
|
|
new FirstTestCriterion(), |
248
|
|
|
new SecondTestCriterion(), |
249
|
|
|
]; |
250
|
|
|
|
251
|
|
|
$userRepository->pushCriteria($criteria); |
252
|
|
|
|
253
|
|
|
$this->assertArrayHasKey(FirstTestCriterion::class, $userRepository->getCriteria()); |
254
|
|
|
$this->assertArrayHasKey(SecondTestCriterion::class, $userRepository->getCriteria()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testGetCriteriaWithDefault() |
258
|
|
|
{ |
259
|
|
|
$userRepository = $this->userRepository(); |
260
|
|
|
|
261
|
|
|
$criteria = [ |
262
|
|
|
new FirstTestCriterion(), |
263
|
|
|
new SecondTestCriterion(), |
264
|
|
|
]; |
265
|
|
|
|
266
|
|
|
$defaultCriteria = [ |
267
|
|
|
function ($query, $repository) { |
|
|
|
|
268
|
|
|
return $query->where('id', 1); |
269
|
|
|
}, |
270
|
|
|
]; |
271
|
|
|
|
272
|
|
|
$userRepository->setDefaultCriteria($defaultCriteria)->pushCriteria($criteria); |
273
|
|
|
|
274
|
|
|
$this->assertCount(3, $userRepository->getCriteria()); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testGetCriteriaWithSkipCriteria() |
278
|
|
|
{ |
279
|
|
|
$userRepository = $this->userRepository(); |
280
|
|
|
|
281
|
|
|
$criteria = [ |
282
|
|
|
new FirstTestCriterion(), |
283
|
|
|
new SecondTestCriterion(), |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
$userRepository->pushCriteria($criteria)->skipCriteria(); |
287
|
|
|
|
288
|
|
|
$this->assertAttributeEquals(true, 'skipCriteria', $userRepository); |
289
|
|
|
$this->assertEmpty($userRepository->getCriteria()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testGetCriteriaWithDefaultWithSkipCriteria() |
293
|
|
|
{ |
294
|
|
|
$userRepository = $this->userRepository(); |
295
|
|
|
|
296
|
|
|
$criteria = [ |
297
|
|
|
new FirstTestCriterion(), |
298
|
|
|
new SecondTestCriterion(), |
299
|
|
|
]; |
300
|
|
|
$defaultCriteria = [ |
301
|
|
|
function ($query, $repository) { |
|
|
|
|
302
|
|
|
return $query->where('id', 1); |
303
|
|
|
}, |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
$userRepository->setDefaultCriteria($defaultCriteria)->pushCriteria($criteria)->skipCriteria(); |
307
|
|
|
|
308
|
|
|
$this->assertAttributeEquals(true, 'skipCriteria', $userRepository); |
309
|
|
|
$this->assertEmpty($userRepository->getCriteria()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testGetCriteriaWithSkipDefaultCriteria() |
313
|
|
|
{ |
314
|
|
|
$userRepository = $this->userRepository(); |
315
|
|
|
|
316
|
|
|
$criteria = [ |
317
|
|
|
new FirstTestCriterion(), |
318
|
|
|
new SecondTestCriterion(), |
319
|
|
|
]; |
320
|
|
|
$defaultCriteria = [ |
321
|
|
|
function ($query, $repository) { |
|
|
|
|
322
|
|
|
return $query->where('id', 1); |
323
|
|
|
}, |
324
|
|
|
]; |
325
|
|
|
|
326
|
|
|
$userRepository->setDefaultCriteria($defaultCriteria)->pushCriteria($criteria)->skipDefaultCriteria(); |
327
|
|
|
$this->assertAttributeEquals(true, 'skipDefaultCriteria', $userRepository); |
328
|
|
|
$this->assertCount(2, $userRepository->getCriteria()); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function testHasCriterion() |
332
|
|
|
{ |
333
|
|
|
$userRepository = $this->userRepository(); |
334
|
|
|
|
335
|
|
|
$criteria = [ |
336
|
|
|
new FirstTestCriterion(), |
337
|
|
|
new SecondTestCriterion(), |
338
|
|
|
]; |
339
|
|
|
|
340
|
|
|
$userRepository->pushCriteria($criteria); |
341
|
|
|
|
342
|
|
|
$this->assertTrue($userRepository->hasCriterion(FirstTestCriterion::class)); |
343
|
|
|
$this->assertTrue($userRepository->hasCriterion(SecondTestCriterion::class)); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function testHasCriterionByClosure() |
347
|
|
|
{ |
348
|
|
|
$userRepository = $this->userRepository(); |
349
|
|
|
$firstCriteria = function ($query, $repository) { |
|
|
|
|
350
|
|
|
return $query->where('id', 1); |
351
|
|
|
}; |
352
|
|
|
|
353
|
|
|
$secondCriteria = function ($query, $repository) { |
|
|
|
|
354
|
|
|
return $query->where('id', 2); |
355
|
|
|
}; |
356
|
|
|
|
357
|
|
|
$userRepository->pushCriteria([$firstCriteria, $secondCriteria]); |
358
|
|
|
|
359
|
|
|
$this->assertTrue($userRepository->hasCriterion($firstCriteria)); |
360
|
|
|
$this->assertTrue($userRepository->hasCriterion($secondCriteria)); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testGetCriterion() |
364
|
|
|
{ |
365
|
|
|
$userRepository = $this->userRepository(); |
366
|
|
|
$userRepository->pushCriteria([ |
367
|
|
|
FirstTestCriterion::class, |
368
|
|
|
SecondTestCriterion::class, |
369
|
|
|
]); |
370
|
|
|
|
371
|
|
|
$this->assertInstanceOf(FirstTestCriterion::class, $userRepository->getCriterion(FirstTestCriterion::class)); |
372
|
|
|
$this->assertInstanceOf(SecondTestCriterion::class, $userRepository->getCriterion(SecondTestCriterion::class)); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
public function testGetCriterionByClosure() |
376
|
|
|
{ |
377
|
|
|
$userRepository = $this->userRepository(); |
378
|
|
|
$firstCriteria = function ($query, $repository) { |
|
|
|
|
379
|
|
|
return $query->where('id', 1); |
380
|
|
|
}; |
381
|
|
|
|
382
|
|
|
$secondCriteria = function ($query, $repository) { |
|
|
|
|
383
|
|
|
return $query->where('id', 2); |
384
|
|
|
}; |
385
|
|
|
|
386
|
|
|
$userRepository->pushCriteria([$firstCriteria, $secondCriteria]); |
387
|
|
|
$this->assertSame($firstCriteria, $userRepository->getCriterion($firstCriteria)); |
388
|
|
|
$this->assertSame($secondCriteria, $userRepository->getCriterion($secondCriteria)); |
389
|
|
|
$this->assertNotSame($secondCriteria, $userRepository->getCriterion($firstCriteria)); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testApplyCriteria() |
393
|
|
|
{ |
394
|
|
|
$userRepository = $this->userRepository(); |
395
|
|
|
$userRepository->pushCriteria([ |
396
|
|
|
new FirstTestCriterion(), |
397
|
|
|
new SecondTestCriterion(), |
398
|
|
|
]); |
399
|
|
|
|
400
|
|
|
$resultQuery = $userRepository->applyCriteria($userRepository->createModel(), $userRepository); |
401
|
|
|
|
402
|
|
|
$this->assertEquals('select * from "users" where "id" = ? and "id" = ?', $resultQuery->toSql()); |
403
|
|
|
$this->assertEquals([0 => 1, 1 => 2], $resultQuery->getBindings()); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
public function testApplyCriteriaWithClosureCriteria() |
407
|
|
|
{ |
408
|
|
|
$userRepository = $this->userRepository(); |
409
|
|
|
$userRepository->pushCriteria([ |
410
|
|
|
new FirstTestCriterion(), |
411
|
|
|
new SecondTestCriterion(), |
412
|
|
|
])->pushCriterion(function ($query, $repository) { |
|
|
|
|
413
|
|
|
return $query->where('id', 3); |
414
|
|
|
}); |
415
|
|
|
|
416
|
|
|
$resultQuery = $userRepository->applyCriteria($userRepository->createModel(), $userRepository); |
417
|
|
|
|
418
|
|
|
$this->assertEquals('select * from "users" where "id" = ? and "id" = ? and "id" = ?', $resultQuery->toSql()); |
419
|
|
|
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $resultQuery->getBindings()); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
class FirstTestCriterion implements \Rinvex\Repository\Contracts\CriterionContract |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
public function apply($query, $repository) |
427
|
|
|
{ |
428
|
|
|
return $query->where('id', 1); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
class SecondTestCriterion implements \Rinvex\Repository\Contracts\CriterionContract |
|
|
|
|
433
|
|
|
{ |
434
|
|
|
public function apply($query, $repository) |
435
|
|
|
{ |
436
|
|
|
return $query->where('id', 2); |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.