|
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; |
@@ -48,6 +51,28 @@ public function it_can_append_case_insensitive() |
48 | 51 | $this->assertAttributeLoaded($model, 'fullname'); |
49 | 52 | } |
50 | 53 |
|
| 54 | + /** @test */ |
| 55 | + public function it_can_append_collections() |
| 56 | + { |
| 57 | + $models = $this |
| 58 | + ->createQueryFromAppendRequest('FullName') |
| 59 | + ->allowedAppends('fullname') |
| 60 | + ->get(); |
| 61 | + |
| 62 | + $this->assertCollectionAttributeLoaded($models, 'fullname'); |
| 63 | + } |
| 64 | + |
| 65 | + /** @test */ |
| 66 | + public function it_can_append_paginates() |
| 67 | + { |
| 68 | + $models = $this |
| 69 | + ->createQueryFromAppendRequest('FullName') |
| 70 | + ->allowedAppends('fullname') |
| 71 | + ->paginate(); |
| 72 | + |
| 73 | + $this->assertPaginateAttributeLoaded($models, 'fullname'); |
| 74 | + } |
| 75 | + |
51 | 76 | /** @test */ |
52 | 77 | public function it_guards_against_invalid_appends() |
53 | 78 | { |
@@ -114,4 +139,24 @@ protected function assertAttributeLoaded(AppendModel $model, string $attribute) |
114 | 139 | { |
115 | 140 | $this->assertTrue(array_key_exists($attribute, $model->toArray())); |
116 | 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 | + } |
117 | 162 | } |
0 commit comments