1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EloquentRepositoryCriteriaIntegrationTests extends AbstractEloquentTests |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function testFindAllByClassNameCriterion() |
6
|
|
|
{ |
7
|
|
|
$userRepository = $this->userRepository(); |
8
|
|
|
$userRepository->pushCriterion(FirstTestCriterion::class); |
9
|
|
|
$result = $userRepository->findAll(); |
10
|
|
|
|
11
|
|
|
$this->assertCount(1, $result); |
12
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
13
|
|
|
$this->assertEquals($result->first()->id, 1); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function testFindAllByObjectCriterion() |
17
|
|
|
{ |
18
|
|
|
$userRepository = $this->userRepository(); |
19
|
|
|
$userRepository->pushCriterion(new FirstTestCriterion()); |
20
|
|
|
$result = $userRepository->findAll(); |
21
|
|
|
|
22
|
|
|
$this->assertCount(1, $result); |
23
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
24
|
|
|
$this->assertEquals($result->first()->id, 1); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testFindAllByClosureCriterion() |
28
|
|
|
{ |
29
|
|
|
$userRepository = $this->userRepository(); |
30
|
|
|
$userRepository->pushCriterion(function ($query, $repository) { |
|
|
|
|
31
|
|
|
return $query->where('id', 2); |
32
|
|
|
}); |
33
|
|
|
$result = $userRepository->findAll(); |
34
|
|
|
|
35
|
|
|
$this->assertCount(1, $result); |
36
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
37
|
|
|
$this->assertEquals($result->first()->id, 2); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testFindAllWithDefaultCriteria() |
41
|
|
|
{ |
42
|
|
|
$userRepository = $this->userRepository(); |
43
|
|
|
$userRepository->pushCriterion(function ($query, $repository) { |
|
|
|
|
44
|
|
|
return $query->orWhere('id', 1); |
45
|
|
|
})->setDefaultCriteria([new SecondTestCriterion()]); |
46
|
|
|
|
47
|
|
|
$result = $userRepository->findAll(); |
48
|
|
|
|
49
|
|
|
$this->assertCount(2, $result); |
50
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFindAllWithSkipCriteria() |
54
|
|
|
{ |
55
|
|
|
$userRepository = $this->userRepository(); |
56
|
|
|
|
57
|
|
|
$userRepository->pushCriteria([ |
58
|
|
|
new FirstTestCriterion(), |
59
|
|
|
new SecondTestCriterion(), |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$result = $userRepository->skipCriteria()->findAll(); |
63
|
|
|
|
64
|
|
|
$this->assertCount(2, $result); |
65
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testFindAllAfterRemoveSkipCriteria() |
69
|
|
|
{ |
70
|
|
|
$userRepository = $this->userRepository(); |
71
|
|
|
|
72
|
|
|
$userRepository->pushCriteria([ |
73
|
|
|
new FirstTestCriterion(), |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$result = $userRepository->skipCriteria()->findAll(); |
77
|
|
|
|
78
|
|
|
$this->assertCount(2, $result); |
79
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
80
|
|
|
|
81
|
|
|
$result = $userRepository->skipCriteria(false)->findAll(); |
82
|
|
|
$this->assertCount(1, $result); |
83
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testFindAllWithSkipDefaultCriteria() |
87
|
|
|
{ |
88
|
|
|
$userRepository = $this->userRepository(); |
89
|
|
|
|
90
|
|
|
$userRepository->pushCriteria([ |
91
|
|
|
new FirstTestCriterion(), |
92
|
|
|
])->setDefaultCriteria([new SecondTestCriterion()]); |
93
|
|
|
|
94
|
|
|
$result = $userRepository->skipDefaultCriteria()->findAll(); |
95
|
|
|
|
96
|
|
|
$this->assertCount(1, $result); |
97
|
|
|
$this->assertContainsOnlyInstancesOf(\Rinvex\Tests\Stubs\EloquentUser::class, $result); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.