|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Spatie\QueryBuilder\Tests; |
4 | | - |
5 | 3 | use Illuminate\Database\Eloquent\Model; |
6 | 4 | use Illuminate\Http\Request; |
7 | 5 | use Illuminate\Support\Collection; |
8 | 6 | use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery; |
9 | 7 | use Spatie\QueryBuilder\QueryBuilder; |
10 | 8 | use Spatie\QueryBuilder\Tests\TestClasses\Models\AppendModel; |
11 | 9 |
|
12 | | -class AppendTest extends TestCase |
| 10 | +uses(TestCase::class); |
| 11 | + |
| 12 | +beforeEach(function () { |
| 13 | + AppendModel::factory()->count(5)->create(); |
| 14 | +}); |
| 15 | + |
| 16 | +it('does not require appends', function () { |
| 17 | + $models = QueryBuilder::for(AppendModel::class, new Request()) |
| 18 | + ->allowedAppends('fullname') |
| 19 | + ->get(); |
| 20 | + |
| 21 | + $this->assertCount(AppendModel::count(), $models); |
| 22 | +}); |
| 23 | + |
| 24 | +it('can append attributes', function () { |
| 25 | + $model = createQueryFromAppendRequest('fullname') |
| 26 | + ->allowedAppends('fullname') |
| 27 | + ->first(); |
| 28 | + |
| 29 | + assertAttributeLoaded($model, 'fullname'); |
| 30 | +}); |
| 31 | + |
| 32 | +it('cannot append case insensitive', function () { |
| 33 | + $this->expectException(InvalidAppendQuery::class); |
| 34 | + |
| 35 | + createQueryFromAppendRequest('FullName') |
| 36 | + ->allowedAppends('fullname') |
| 37 | + ->first(); |
| 38 | +}); |
| 39 | + |
| 40 | +it('can append collections', function () { |
| 41 | + $models = createQueryFromAppendRequest('FullName') |
| 42 | + ->allowedAppends('FullName') |
| 43 | + ->get(); |
| 44 | + |
| 45 | + assertCollectionAttributeLoaded($models, 'FullName'); |
| 46 | +}); |
| 47 | + |
| 48 | +it('can append paginates', function () { |
| 49 | + $models = createQueryFromAppendRequest('FullName') |
| 50 | + ->allowedAppends('FullName') |
| 51 | + ->paginate(); |
| 52 | + |
| 53 | + assertPaginateAttributeLoaded($models, 'FullName'); |
| 54 | +}); |
| 55 | + |
| 56 | +it('can append simple paginates', function () { |
| 57 | + $models = createQueryFromAppendRequest('FullName') |
| 58 | + ->allowedAppends('FullName') |
| 59 | + ->simplePaginate(); |
| 60 | + |
| 61 | + assertPaginateAttributeLoaded($models, 'FullName'); |
| 62 | +}); |
| 63 | + |
| 64 | +it('can append cursor paginates', function () { |
| 65 | + $models = createQueryFromAppendRequest('FullName') |
| 66 | + ->allowedAppends('FullName') |
| 67 | + ->cursorPaginate(); |
| 68 | + |
| 69 | + assertPaginateAttributeLoaded($models, 'FullName'); |
| 70 | +}); |
| 71 | + |
| 72 | +it('guards against invalid appends', function () { |
| 73 | + $this->expectException(InvalidAppendQuery::class); |
| 74 | + |
| 75 | + createQueryFromAppendRequest('random-attribute-to-append') |
| 76 | + ->allowedAppends('attribute-to-append'); |
| 77 | +}); |
| 78 | + |
| 79 | +it('can allow multiple appends', function () { |
| 80 | + $model = createQueryFromAppendRequest('fullname') |
| 81 | + ->allowedAppends('fullname', 'randomAttribute') |
| 82 | + ->first(); |
| 83 | + |
| 84 | + assertAttributeLoaded($model, 'fullname'); |
| 85 | +}); |
| 86 | + |
| 87 | +it('can allow multiple appends as an array', function () { |
| 88 | + $model = createQueryFromAppendRequest('fullname') |
| 89 | + ->allowedAppends(['fullname', 'randomAttribute']) |
| 90 | + ->first(); |
| 91 | + |
| 92 | + assertAttributeLoaded($model, 'fullname'); |
| 93 | +}); |
| 94 | + |
| 95 | +it('can append multiple attributes', function () { |
| 96 | + $model = createQueryFromAppendRequest('fullname,reversename') |
| 97 | + ->allowedAppends(['fullname', 'reversename']) |
| 98 | + ->first(); |
| 99 | + |
| 100 | + assertAttributeLoaded($model, 'fullname'); |
| 101 | + assertAttributeLoaded($model, 'reversename'); |
| 102 | +}); |
| 103 | + |
| 104 | +test('an invalid append query exception contains the not allowed and allowed appends', function () { |
| 105 | + $exception = new InvalidAppendQuery(collect(['not allowed append']), collect(['allowed append'])); |
| 106 | + |
| 107 | + $this->assertEquals(['not allowed append'], $exception->appendsNotAllowed->all()); |
| 108 | + $this->assertEquals(['allowed append'], $exception->allowedAppends->all()); |
| 109 | +}); |
| 110 | + |
| 111 | +// Helpers |
| 112 | +function createQueryFromAppendRequest(string $appends): QueryBuilder |
13 | 113 | { |
14 | | - public function setUp(): void |
15 | | - { |
16 | | - parent::setUp(); |
17 | | - |
18 | | - AppendModel::factory()->count(5)->create(); |
19 | | - } |
20 | | - |
21 | | - /** @test */ |
22 | | - public function it_does_not_require_appends() |
23 | | - { |
24 | | - $models = QueryBuilder::for(AppendModel::class, new Request()) |
25 | | - ->allowedAppends('fullname') |
26 | | - ->get(); |
27 | | - |
28 | | - $this->assertCount(AppendModel::count(), $models); |
29 | | - } |
30 | | - |
31 | | - /** @test */ |
32 | | - public function it_can_append_attributes() |
33 | | - { |
34 | | - $model = $this |
35 | | - ->createQueryFromAppendRequest('fullname') |
36 | | - ->allowedAppends('fullname') |
37 | | - ->first(); |
38 | | - |
39 | | - $this->assertAttributeLoaded($model, 'fullname'); |
40 | | - } |
41 | | - |
42 | | - /** @test */ |
43 | | - public function it_cannot_append_case_insensitive() |
44 | | - { |
45 | | - $this->expectException(InvalidAppendQuery::class); |
46 | | - |
47 | | - $this |
48 | | - ->createQueryFromAppendRequest('FullName') |
49 | | - ->allowedAppends('fullname') |
50 | | - ->first(); |
51 | | - } |
52 | | - |
53 | | - /** @test */ |
54 | | - public function it_can_append_collections() |
55 | | - { |
56 | | - $models = $this |
57 | | - ->createQueryFromAppendRequest('FullName') |
58 | | - ->allowedAppends('FullName') |
59 | | - ->get(); |
60 | | - |
61 | | - $this->assertCollectionAttributeLoaded($models, 'FullName'); |
62 | | - } |
63 | | - |
64 | | - /** @test */ |
65 | | - public function it_can_append_paginates() |
66 | | - { |
67 | | - $models = $this |
68 | | - ->createQueryFromAppendRequest('FullName') |
69 | | - ->allowedAppends('FullName') |
70 | | - ->paginate(); |
71 | | - |
72 | | - $this->assertPaginateAttributeLoaded($models, 'FullName'); |
73 | | - } |
74 | | - |
75 | | - /** @test */ |
76 | | - public function it_can_append_simple_paginates() |
77 | | - { |
78 | | - $models = $this |
79 | | - ->createQueryFromAppendRequest('FullName') |
80 | | - ->allowedAppends('FullName') |
81 | | - ->simplePaginate(); |
82 | | - |
83 | | - $this->assertPaginateAttributeLoaded($models, 'FullName'); |
84 | | - } |
85 | | - |
86 | | - /** @test */ |
87 | | - public function it_can_append_cursor_paginates() |
88 | | - { |
89 | | - $models = $this |
90 | | - ->createQueryFromAppendRequest('FullName') |
91 | | - ->allowedAppends('FullName') |
92 | | - ->cursorPaginate(); |
93 | | - |
94 | | - $this->assertPaginateAttributeLoaded($models, 'FullName'); |
95 | | - } |
96 | | - |
97 | | - /** @test */ |
98 | | - public function it_guards_against_invalid_appends() |
99 | | - { |
100 | | - $this->expectException(InvalidAppendQuery::class); |
101 | | - |
102 | | - $this |
103 | | - ->createQueryFromAppendRequest('random-attribute-to-append') |
104 | | - ->allowedAppends('attribute-to-append'); |
105 | | - } |
106 | | - |
107 | | - /** @test */ |
108 | | - public function it_can_allow_multiple_appends() |
109 | | - { |
110 | | - $model = $this |
111 | | - ->createQueryFromAppendRequest('fullname') |
112 | | - ->allowedAppends('fullname', 'randomAttribute') |
113 | | - ->first(); |
114 | | - |
115 | | - $this->assertAttributeLoaded($model, 'fullname'); |
116 | | - } |
117 | | - |
118 | | - /** @test */ |
119 | | - public function it_can_allow_multiple_appends_as_an_array() |
120 | | - { |
121 | | - $model = $this |
122 | | - ->createQueryFromAppendRequest('fullname') |
123 | | - ->allowedAppends(['fullname', 'randomAttribute']) |
124 | | - ->first(); |
125 | | - |
126 | | - $this->assertAttributeLoaded($model, 'fullname'); |
127 | | - } |
128 | | - |
129 | | - /** @test */ |
130 | | - public function it_can_append_multiple_attributes() |
131 | | - { |
132 | | - $model = $this |
133 | | - ->createQueryFromAppendRequest('fullname,reversename') |
134 | | - ->allowedAppends(['fullname', 'reversename']) |
135 | | - ->first(); |
136 | | - |
137 | | - $this->assertAttributeLoaded($model, 'fullname'); |
138 | | - $this->assertAttributeLoaded($model, 'reversename'); |
139 | | - } |
140 | | - |
141 | | - /** @test */ |
142 | | - public function an_invalid_append_query_exception_contains_the_not_allowed_and_allowed_appends() |
143 | | - { |
144 | | - $exception = new InvalidAppendQuery(collect(['not allowed append']), collect(['allowed append'])); |
145 | | - |
146 | | - $this->assertEquals(['not allowed append'], $exception->appendsNotAllowed->all()); |
147 | | - $this->assertEquals(['allowed append'], $exception->allowedAppends->all()); |
148 | | - } |
149 | | - |
150 | | - protected function createQueryFromAppendRequest(string $appends): QueryBuilder |
151 | | - { |
152 | | - $request = new Request([ |
153 | | - 'append' => $appends, |
154 | | - ]); |
155 | | - |
156 | | - return QueryBuilder::for(AppendModel::class, $request); |
157 | | - } |
158 | | - |
159 | | - protected function assertAttributeLoaded(AppendModel $model, string $attribute) |
160 | | - { |
161 | | - $this->assertTrue(array_key_exists($attribute, $model->toArray())); |
162 | | - } |
163 | | - |
164 | | - protected function assertCollectionAttributeLoaded(Collection $collection, string $attribute) |
165 | | - { |
166 | | - $hasModelWithoutAttributeLoaded = $collection |
167 | | - ->contains(function (Model $model) use ($attribute) { |
168 | | - return ! array_key_exists($attribute, $model->toArray()); |
169 | | - }); |
170 | | - |
171 | | - $this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
172 | | - } |
173 | | - |
174 | | - /** |
| 114 | + $request = new Request([ |
| 115 | + 'append' => $appends, |
| 116 | + ]); |
| 117 | + |
| 118 | + return QueryBuilder::for(AppendModel::class, $request); |
| 119 | +} |
| 120 | + |
| 121 | +function assertAttributeLoaded(AppendModel $model, string $attribute) |
| 122 | +{ |
| 123 | + test()->assertTrue(array_key_exists($attribute, $model->toArray())); |
| 124 | +} |
| 125 | + |
| 126 | +function assertCollectionAttributeLoaded(Collection $collection, string $attribute) |
| 127 | +{ |
| 128 | + $hasModelWithoutAttributeLoaded = $collection |
| 129 | + ->contains(function (Model $model) use ($attribute) { |
| 130 | + return ! array_key_exists($attribute, $model->toArray()); |
| 131 | + }); |
| 132 | + |
| 133 | + test()->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
175 | 137 | * @param \Illuminate\Pagination\LengthAwarePaginator|\Illuminate\Contracts\Pagination\Paginator $collection |
176 | 138 | * @param string $attribute |
177 | 139 | */ |
178 | | - protected function assertPaginateAttributeLoaded($collection, string $attribute) |
179 | | - { |
180 | | - $hasModelWithoutAttributeLoaded = $collection |
181 | | - ->contains(function (Model $model) use ($attribute) { |
182 | | - return ! array_key_exists($attribute, $model->toArray()); |
183 | | - }); |
184 | | - |
185 | | - $this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
186 | | - } |
| 140 | +function assertPaginateAttributeLoaded($collection, string $attribute) |
| 141 | +{ |
| 142 | + $hasModelWithoutAttributeLoaded = $collection |
| 143 | + ->contains(function (Model $model) use ($attribute) { |
| 144 | + return ! array_key_exists($attribute, $model->toArray()); |
| 145 | + }); |
| 146 | + |
| 147 | + test()->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
187 | 148 | } |
0 commit comments