Skip to content

Commit 642c14c

Browse files
author
francoism90
committed
Fix AppendTest
1 parent e4f4a49 commit 642c14c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

tests/AppendTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Spatie\QueryBuilder\Tests;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Http\Request;
7+
use Illuminate\Pagination\LengthAwarePaginator;
8+
use Illuminate\Support\Collection;
69
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
710
use Spatie\QueryBuilder\QueryBuilder;
811
use Spatie\QueryBuilder\Tests\TestClasses\Models\AppendModel;
@@ -51,23 +54,23 @@ public function it_can_append_case_insensitive()
5154
/** @test */
5255
public function it_can_append_collections()
5356
{
54-
$model = $this
57+
$models = $this
5558
->createQueryFromAppendRequest('FullName')
5659
->allowedAppends('fullname')
5760
->get();
5861

59-
$this->assertAttributeLoaded($model, 'fullname');
62+
$this->assertCollectionAttributeLoaded($models, 'fullname');
6063
}
6164

6265
/** @test */
6366
public function it_can_append_paginates()
6467
{
65-
$model = $this
68+
$models = $this
6669
->createQueryFromAppendRequest('FullName')
6770
->allowedAppends('fullname')
6871
->paginate();
6972

70-
$this->assertAttributeLoaded($model, 'fullname');
73+
$this->assertPaginateAttributeLoaded($models, 'fullname');
7174
}
7275

7376
/** @test */
@@ -136,4 +139,24 @@ protected function assertAttributeLoaded(AppendModel $model, string $attribute)
136139
{
137140
$this->assertTrue(array_key_exists($attribute, $model->toArray()));
138141
}
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+
}
139162
}

0 commit comments

Comments
 (0)