|
2 | 2 |
|
3 | 3 | namespace Spatie\QueryBuilder\Tests; |
4 | 4 |
|
| 5 | +use Illuminate\Database\Eloquent\Model; |
5 | 6 | use Illuminate\Http\Request; |
| 7 | +use Illuminate\Pagination\LengthAwarePaginator; |
| 8 | +use Illuminate\Support\Collection; |
6 | 9 | use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery; |
7 | 10 | use Spatie\QueryBuilder\QueryBuilder; |
8 | 11 | use Spatie\QueryBuilder\Tests\TestClasses\Models\AppendModel; |
@@ -51,23 +54,23 @@ public function it_can_append_case_insensitive() |
51 | 54 | /** @test */ |
52 | 55 | public function it_can_append_collections() |
53 | 56 | { |
54 | | - $model = $this |
| 57 | + $models = $this |
55 | 58 | ->createQueryFromAppendRequest('FullName') |
56 | 59 | ->allowedAppends('fullname') |
57 | 60 | ->get(); |
58 | 61 |
|
59 | | - $this->assertAttributeLoaded($model, 'fullname'); |
| 62 | + $this->assertCollectionAttributeLoaded($models, 'fullname'); |
60 | 63 | } |
61 | 64 |
|
62 | 65 | /** @test */ |
63 | 66 | public function it_can_append_paginates() |
64 | 67 | { |
65 | | - $model = $this |
| 68 | + $models = $this |
66 | 69 | ->createQueryFromAppendRequest('FullName') |
67 | 70 | ->allowedAppends('fullname') |
68 | 71 | ->paginate(); |
69 | 72 |
|
70 | | - $this->assertAttributeLoaded($model, 'fullname'); |
| 73 | + $this->assertPaginateAttributeLoaded($models, 'fullname'); |
71 | 74 | } |
72 | 75 |
|
73 | 76 | /** @test */ |
@@ -136,4 +139,24 @@ protected function assertAttributeLoaded(AppendModel $model, string $attribute) |
136 | 139 | { |
137 | 140 | $this->assertTrue(array_key_exists($attribute, $model->toArray())); |
138 | 141 | } |
| 142 | + |
| 143 | + protected function assertCollectionAttributeLoaded(Collection $collection, string $attribute) |
| 144 | + { |
| 145 | + $hasModelWithoutAttributeLoaded = $collection |
| 146 | + ->contains(function (Model $model) use ($attribute) { |
| 147 | + return ! array_key_exists($attribute, $model->toArray()); |
| 148 | + }); |
| 149 | + |
| 150 | + $this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
| 151 | + } |
| 152 | + |
| 153 | + protected function assertPaginateAttributeLoaded(LengthAwarePaginator $collection, string $attribute) |
| 154 | + { |
| 155 | + $hasModelWithoutAttributeLoaded = $collection |
| 156 | + ->contains(function (Model $model) use ($attribute) { |
| 157 | + return ! array_key_exists($attribute, $model->toArray()); |
| 158 | + }); |
| 159 | + |
| 160 | + $this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded."); |
| 161 | + } |
139 | 162 | } |
0 commit comments