Skip to content

Commit

Permalink
clean up tests methods;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed May 4, 2023
1 parent aa60e7c commit 80f262b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 157 deletions.
58 changes: 0 additions & 58 deletions tests/Feature/Http/Resources/JsonResourceCollectionQueryTest.php

This file was deleted.

36 changes: 0 additions & 36 deletions tests/Feature/Http/Resources/JsonResourceIncludesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,4 @@ public function includesThroughApi(): void {
);
}

/**
* @test
*
* @return void
*/
public function includesOnCollectionClassThroughApi(): void {
PostFactory::new()->count( 2 )->has( CommentFactory::new()->count( 5 ) )->create();
$content = $this->get( "/includes/posts?includes=comments" )
->json();
self::assertEquals(
[
'data' => Post::all()->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
);
}


}
63 changes: 0 additions & 63 deletions tests/Feature/Http/Resources/JsonResourceQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,6 @@ public function queries(): void {
);
}

/**
* @test
*
* @return void
*/
public function queriesInCollectionClass(): void {
$posts = PostFactory::new()->count( 3 )->has( CommentFactory::new()->count( 5 ) )->create();
$resource = PostCollection::make( $posts )->withFirstCommentQuery();

self::assertEquals(
[
'data' => $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 )
);
}

/**
* @test
*
Expand Down Expand Up @@ -105,36 +74,4 @@ public function queriesThroughApi(): void {
);
}

/**
* @test
*
* @return void
*/
public function queriesInCollectionClassThroughApi(): void {
PostFactory::new()->count( 2 )->has( CommentFactory::new()->count( 5 ) )->create();
$content = $this->get( "/queries/posts?with_first_comment" )
->json();

self::assertEquals(
[
'data' => Post::all()->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
);
}

}
153 changes: 153 additions & 0 deletions tests/Feature/Http/Resources/ResourceCollectionQueryTest.php
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 )
);
}

}

0 comments on commit 80f262b

Please sign in to comment.