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