-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa60e7c
commit 80f262b
Showing
4 changed files
with
153 additions
and
157 deletions.
There are no files selected for viewing
58 changes: 0 additions & 58 deletions
58
tests/Feature/Http/Resources/JsonResourceCollectionQueryTest.php
This file was deleted.
Oops, something went wrong.
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
153 changes: 153 additions & 0 deletions
153
tests/Feature/Http/Resources/ResourceCollectionQueryTest.php
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace Hans\Tests\Valravn\Feature\Http\Resources; | ||
|
||
use Hans\Tests\Valravn\Core\Factories\CommentFactory; | ||
use Hans\Tests\Valravn\Core\Factories\PostFactory; | ||
use Hans\Tests\Valravn\Core\Models\Comment; | ||
use Hans\Tests\Valravn\Core\Models\Post; | ||
use Hans\Tests\Valravn\Core\Resources\Post\PostCollection; | ||
use Hans\Tests\Valravn\TestCase; | ||
use Illuminate\Support\Collection; | ||
|
||
class ResourceCollectionQueryTest extends TestCase { | ||
private Collection $posts; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->posts = PostFactory::new()->count( 3 )->has( CommentFactory::new()->count( 5 ) )->create(); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @return void | ||
*/ | ||
public function collection(): void { | ||
$resource = PostCollection::make( $this->posts )->withAllCommentsQuery(); | ||
$comments = $this->posts->map( fn( Post $post ) => $post->comments )->flatten(); | ||
|
||
self::assertEquals( | ||
[ | ||
'data' => $this->posts->map( | ||
fn( Post $post ) => [ | ||
'type' => 'posts', | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'content' => $post->content, | ||
] | ||
)->toArray(), | ||
'type' => 'posts', | ||
'all-comments' => | ||
$comments->map( | ||
fn( Comment $comment ) => [ | ||
'type' => 'comments', | ||
'id' => $comment->id, | ||
'content' => $comment->content, | ||
] | ||
) | ||
->toArray() | ||
], | ||
$this->resourceToJson( $resource ) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @return void | ||
*/ | ||
public function includesOnCollectionClassThroughApi(): void { | ||
$content = $this->get( "/includes/posts?includes=comments" ) | ||
->json(); | ||
self::assertEquals( | ||
[ | ||
'data' => $this->posts->map( | ||
fn( Post $post ) => [ | ||
'type' => 'posts', | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'content' => $post->content, | ||
'comments' => $post->comments | ||
->map( | ||
fn( Comment $value ) => [ | ||
'type' => 'comments', | ||
'id' => $value->id, | ||
'content' => $value->content, | ||
] | ||
) | ||
->toArray() | ||
] | ||
) | ||
->toArray(), | ||
'type' => 'posts' | ||
], | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @return void | ||
*/ | ||
public function queriesThroughApi(): void { | ||
$content = $this->get( "/queries/posts?with_first_comment" ) | ||
->json(); | ||
|
||
self::assertEquals( | ||
[ | ||
'data' => $this->posts->map( | ||
fn( Post $post ) => [ | ||
'type' => 'posts', | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'content' => $post->content, | ||
'first_comment' => [ | ||
'type' => 'comments', | ||
'id' => ( $comment = $post->comments()->limit( 1 )->first() )->id, | ||
'content' => $comment->content, | ||
], | ||
] | ||
) | ||
->toArray(), | ||
'type' => 'posts', | ||
], | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @return void | ||
*/ | ||
public function queriesInCollectionClass(): void { | ||
$resource = PostCollection::make( $this->posts )->withFirstCommentQuery(); | ||
|
||
self::assertEquals( | ||
[ | ||
'data' => $this->posts->map( | ||
fn( Post $post ) => [ | ||
'type' => 'posts', | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'content' => $post->content, | ||
'first_comment' => [ | ||
'type' => 'comments', | ||
'id' => ( $comment = $post->comments()->limit( 1 )->first() )->id, | ||
'content' => $comment->content, | ||
], | ||
] | ||
) | ||
->toArray(), | ||
'type' => 'posts', | ||
], | ||
$this->resourceToJson( $resource ) | ||
); | ||
} | ||
|
||
} |