Skip to content

Commit 788e152

Browse files
committed
[Tests] Update some existing tests
1 parent 68c6809 commit 788e152

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

Diff for: tests/dummy/app/JsonApi/V1/Posts/PostResource.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function attributes(): iterable
3434
return [
3535
'content' => $this->content,
3636
'createdAt' => $this->created_at,
37+
'publishedAt' => $this->published_at,
3738
'slug' => $this->slug,
3839
'synopsis' => $this->synopsis,
3940
'title' => $this->title,

Diff for: tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function fields(): array
5353
HasMany::make('comments')->readOnly(),
5454
Str::make('content'),
5555
DateTime::make('createdAt')->sortable()->readOnly(),
56+
DateTime::make('publishedAt')->sortable(),
5657
Str::make('slug'),
5758
Str::make('synopsis'),
5859
BelongsToMany::make('tags'),

Diff for: tests/dummy/app/Models/Post.php

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class Post extends Model
4040
'title',
4141
];
4242

43+
/**
44+
* @var string[]
45+
*/
46+
protected $casts = [
47+
'published_at' => 'datetime',
48+
];
49+
4350
/**
4451
* @return BelongsTo
4552
*/

Diff for: tests/dummy/tests/Api/V1/Posts/ReadTagsTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ protected function setUp(): void
4242

4343
public function test(): void
4444
{
45-
$expected = Tag::factory()
46-
->count(3)
47-
->create();
45+
$tags = Tag::factory()->count(3)->create();
46+
$this->post->tags()->attach($tags);
4847

49-
$this->post->tags()->attach($expected);
48+
$expected = $tags
49+
->map(fn(Tag $tag) => $this->serializer->tag($tag)->jsonSerialize())
50+
->all();
5051

5152
$response = $this
5253
->jsonApi('tags')

Diff for: tests/dummy/tests/Api/V1/Serializer.php

+41-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace App\Tests\Api\V1;
2121

2222
use App\Models\Post;
23+
use App\Models\Tag;
2324
use App\Models\User;
2425
use LaravelJsonApi\Core\Document\ResourceObject;
2526
use function url;
@@ -42,11 +43,12 @@ public function post(Post $post): ResourceObject
4243
'id' => (string) $post->getRouteKey(),
4344
'attributes' => [
4445
'content' => $post->content,
45-
'createdAt' => $post->created_at->toJSON(),
46+
'createdAt' => $post->created_at->jsonSerialize(),
47+
'publishedAt' => optional($post->published_at)->jsonSerialize(),
4648
'slug' => $post->slug,
4749
'synopsis' => $post->synopsis,
4850
'title' => $post->title,
49-
'updatedAt' => $post->updated_at->toJSON(),
51+
'updatedAt' => $post->updated_at->jsonSerialize(),
5052
],
5153
'relationships' => [
5254
'author' => [
@@ -74,6 +76,41 @@ public function post(Post $post): ResourceObject
7476
]);
7577
}
7678

79+
/**
80+
* Get the expected tag resource.
81+
*
82+
* @param Tag $tag
83+
* @return ResourceObject
84+
*/
85+
public function tag(Tag $tag): ResourceObject
86+
{
87+
$self = url('/api/v1/tags', $tag);
88+
89+
return ResourceObject::fromArray([
90+
'type' => 'tags',
91+
'id' => (string) $tag->getRouteKey(),
92+
'attributes' => [
93+
'createdAt' => $tag->created_at->jsonSerialize(),
94+
'name' => $tag->name,
95+
'updatedAt' => $tag->updated_at->jsonSerialize(),
96+
],
97+
'relationships' => [
98+
'posts' => [
99+
'links' => [
100+
'self' => "{$self}/relationships/posts",
101+
'related' => "{$self}/posts",
102+
],
103+
],
104+
'videos' => [
105+
'links' => [
106+
'self' => "{$self}/relationships/videos",
107+
'related' => "{$self}/videos",
108+
],
109+
],
110+
],
111+
]);
112+
}
113+
77114
/**
78115
* Get the expected user resource.
79116
*
@@ -88,9 +125,9 @@ public function user(User $user): ResourceObject
88125
'type' => 'users',
89126
'id' => (string) $user->getRouteKey(),
90127
'attributes' => [
91-
'createdAt' => $user->created_at->toJSON(),
128+
'createdAt' => $user->created_at->jsonSerialize(),
92129
'name' => $user->name,
93-
'updatedAt' => $user->updated_at->toJSON(),
130+
'updatedAt' => $user->updated_at->jsonSerialize(),
94131
],
95132
'links' => [
96133
'self' => $self,

0 commit comments

Comments
 (0)