-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for relationships in CaseSort
- Loading branch information
1 parent
971e5ca
commit 2b69841
Showing
2 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,24 @@ | |
use Spatie\QueryBuilder\QueryBuilder; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use TeamQ\QueryBuilder\Sorts\CaseSort; | ||
use Tests\Mocks\Enums\AuthorTypeEnum; | ||
use Tests\Mocks\Enums\BookClassificationEnum; | ||
use Tests\Mocks\Models\Author; | ||
use Tests\Mocks\Models\Book; | ||
|
||
beforeEach(function () { | ||
$this->request = new Illuminate\Http\Request(); | ||
$this->request->setMethod(Request::METHOD_GET); | ||
|
||
$this->firstBook = Book::factory() | ||
->for( | ||
Author::factory() | ||
->create([ | ||
'name' => 'Spatie', | ||
'email' => '[email protected]', | ||
'type' => AuthorTypeEnum::VIP, | ||
]) | ||
) | ||
->create([ | ||
'title' => 'Laravel Beyond Crud', | ||
'isbn' => '758952123', | ||
|
@@ -20,6 +30,14 @@ | |
]); | ||
|
||
$this->secondBook = Book::factory() | ||
->for( | ||
Author::factory() | ||
->create([ | ||
'name' => 'Spatie', | ||
'email' => '[email protected]', | ||
'type' => AuthorTypeEnum::Public, | ||
]) | ||
) | ||
->create([ | ||
'title' => 'Domain Driven Design for Laravel', | ||
'isbn' => '5895421369', | ||
|
@@ -79,5 +97,26 @@ | |
}); | ||
|
||
it('sort records by relationship fields', function () { | ||
$this->request->query->add([ | ||
'sort' => 'author.type', | ||
]); | ||
|
||
$cases = collect(AuthorTypeEnum::cases()) | ||
->pluck('name', 'value') | ||
->toArray(); | ||
|
||
$queryBuilder = QueryBuilder::for(Book::class, $this->request) | ||
->allowedSorts([ | ||
AllowedSort::custom('author.type', new CaseSort($cases)), | ||
]); | ||
|
||
})->skip('WIP'); | ||
expect($queryBuilder->toSql()) | ||
->toBe( | ||
"select `books`.* from `books` inner join `authors` on `books`.`author_id` = `authors`.`id` order by case when authors.type = 1 then 'VIP' when authors.type = 2 then 'Public' when authors.type = 3 then 'Private' end asc" | ||
) | ||
->and($queryBuilder->get()) | ||
->sequence( | ||
fn ($book) => $book->author->type->toBe(AuthorTypeEnum::Public), | ||
fn ($book) => $book->author->type->toBe(AuthorTypeEnum::VIP), | ||
); | ||
}); |