Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix AppendTest
  • Loading branch information
francoism90 committed Sep 30, 2020
commit 642c14c7c23a1c07233cb70fdc47f764310a8ae3
31 changes: 27 additions & 4 deletions tests/AppendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Spatie\QueryBuilder\Tests;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\Tests\TestClasses\Models\AppendModel;
Expand Down Expand Up @@ -51,23 +54,23 @@ public function it_can_append_case_insensitive()
/** @test */
public function it_can_append_collections()
{
$model = $this
$models = $this
->createQueryFromAppendRequest('FullName')
->allowedAppends('fullname')
->get();

$this->assertAttributeLoaded($model, 'fullname');
$this->assertCollectionAttributeLoaded($models, 'fullname');
}

/** @test */
public function it_can_append_paginates()
{
$model = $this
$models = $this
->createQueryFromAppendRequest('FullName')
->allowedAppends('fullname')
->paginate();

$this->assertAttributeLoaded($model, 'fullname');
$this->assertPaginateAttributeLoaded($models, 'fullname');
}

/** @test */
Expand Down Expand Up @@ -136,4 +139,24 @@ protected function assertAttributeLoaded(AppendModel $model, string $attribute)
{
$this->assertTrue(array_key_exists($attribute, $model->toArray()));
}

protected function assertCollectionAttributeLoaded(Collection $collection, string $attribute)
{
$hasModelWithoutAttributeLoaded = $collection
->contains(function (Model $model) use ($attribute) {
return ! array_key_exists($attribute, $model->toArray());
});

$this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded.");
}

protected function assertPaginateAttributeLoaded(LengthAwarePaginator $collection, string $attribute)
{
$hasModelWithoutAttributeLoaded = $collection
->contains(function (Model $model) use ($attribute) {
return ! array_key_exists($attribute, $model->toArray());
});

$this->assertFalse($hasModelWithoutAttributeLoaded, "The `{$attribute}` attribute was expected but not loaded.");
}
}