Skip to content

Commit 188a50c

Browse files
committed
[Tests] Use new test features to tidy up tests
1 parent 8b48598 commit 188a50c

File tree

11 files changed

+42
-66
lines changed

11 files changed

+42
-66
lines changed

tests/dummy/tests/Api/V1/Posts/Actions/PublishTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function test(): void
4646

4747
$expected = $this->serializer
4848
->post($this->post)
49-
->replace('publishedAt', $date->jsonSerialize())
49+
->replace('publishedAt', $date)
5050
->replace('author', ['type' => 'users', 'id' => $this->post->author]);
5151

5252
$response = $this
@@ -57,7 +57,7 @@ public function test(): void
5757
->includePaths('author')
5858
->post(url('/api/v1/posts', [$this->post, '-actions/publish']));
5959

60-
$response->assertFetchedOneExact($expected->jsonSerialize());
60+
$response->assertFetchedOneExact($expected);
6161
$response->assertIncluded([$expected['author']]);
6262

6363
$this->assertDatabaseHas('posts', array_replace(

tests/dummy/tests/Api/V1/Posts/CreateTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public function test(): void
5151

5252
$expected = $data
5353
->forget('createdAt', 'updatedAt')
54-
->replace('author', ['type' => 'users', 'id' => $post->author])
55-
->jsonSerialize();
54+
->replace('author', ['type' => 'users', 'id' => $post->author]);
5655

5756
$response = $this
5857
->withoutExceptionHandling()
@@ -107,8 +106,7 @@ public function testInvalid(): void
107106

108107
$data = $this
109108
->serialize($post)
110-
->replace('slug', $exists->slug)
111-
->jsonSerialize();
109+
->replace('slug', $exists->slug);
112110

113111
$expected = [
114112
'detail' => 'The slug has already been taken.',
@@ -132,8 +130,7 @@ public function testClientId(): void
132130

133131
$data = $this
134132
->serialize($post)
135-
->withId('81166677-f3c4-440c-9a4a-12b89802d731')
136-
->jsonSerialize();
133+
->withId('81166677-f3c4-440c-9a4a-12b89802d731');
137134

138135
$expected = [
139136
'detail' => "Resource type posts does not support client-generated IDs.",

tests/dummy/tests/Api/V1/Posts/IndexTest.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,13 @@ public function testIncludeAuthor(): void
125125
{
126126
$posts = Post::factory()->count(2)->create();
127127

128-
$expected1 = $this->serializer->post($posts[0])->jsonSerialize();
129-
$expected1['relationships']['author']['data'] = $user1 = [
130-
'type' => 'users',
131-
'id' => (string) $posts[0]->author->getRouteKey(),
132-
];
128+
$expected1 = $this->serializer
129+
->post($posts[0])
130+
->putRelation('author', $user1 = ['type' => 'users', 'id' => $posts[0]->author]);
133131

134-
$expected2 = $this->serializer->post($posts[1])->jsonSerialize();
135-
$expected2['relationships']['author']['data'] = $user2 = [
136-
'type' => 'users',
137-
'id' => (string) $posts[1]->author->getRouteKey(),
138-
];
132+
$expected2 = $this->serializer
133+
->post($posts[1])
134+
->putRelation('author', $user2 = ['type' => 'users', 'id' => $posts[1]->author]);
139135

140136
$response = $this
141137
->withoutExceptionHandling()
@@ -174,7 +170,7 @@ public function testIdFilter(): void
174170
public function testSlugFilter(): void
175171
{
176172
$posts = Post::factory()->count(2)->create();
177-
$expected = $this->serializer->post($posts[1])->jsonSerialize();
173+
$expected = $this->serializer->post($posts[1]);
178174

179175
$response = $this
180176
->withoutExceptionHandling()
@@ -237,7 +233,6 @@ public function testWithCount(): void
237233
$expected = collect($posts)->map(fn($post) => $this->serializer
238234
->post($post)
239235
->withRelationshipMeta('tags', ['count' => 1])
240-
->jsonSerialize()
241236
)->all();
242237

243238
/** Draft post should not appear. */

tests/dummy/tests/Api/V1/Posts/ReadAuthorTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ protected function setUp(): void
4545
public function test(): void
4646
{
4747
$expected = $this->serializer
48-
->user($this->post->author)
49-
->jsonSerialize();
48+
->user($this->post->author);
5049

5150
$response = $this
5251
->withoutExceptionHandling()
@@ -62,8 +61,7 @@ public function test(): void
6261
public function testFilterMatches(): void
6362
{
6463
$expected = $this->serializer
65-
->user($this->post->author)
66-
->jsonSerialize();
64+
->user($this->post->author);
6765

6866
$response = $this
6967
->jsonApi('users')
@@ -124,8 +122,7 @@ public function testDraftAsAuthor(): void
124122
$this->post->update(['published_at' => null]);
125123

126124
$expected = $this->serializer
127-
->user($this->post->author)
128-
->jsonSerialize();
125+
->user($this->post->author);
129126

130127
$response = $this
131128
->actingAs($this->post->author)

tests/dummy/tests/Api/V1/Posts/ReadMediaTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,11 @@ public function testWithCount(): void
150150

151151
$expectedImages = $images->toBase()->map(fn(Image $image) => $this->serializer
152152
->image($image)
153-
->jsonSerialize()
154153
)->all();
155154

156155
$expectedVideos = $videos->toBase()->map(fn(Video $video) => $this->serializer
157156
->video($video)
158157
->withRelationshipMeta('tags', ['count' => 1])
159-
->jsonSerialize()
160158
)->all();
161159

162160
$expected = collect($expectedImages)->merge($expectedVideos)->all();

tests/dummy/tests/Api/V1/Posts/ReadTagsTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function test(): void
4646
$this->post->tags()->attach($tags);
4747

4848
$expected = $tags
49-
->map(fn(Tag $tag) => $this->serializer->tag($tag)->jsonSerialize())
49+
->map(fn(Tag $tag) => $this->serializer->tag($tag))
5050
->all();
5151

5252
$response = $this
@@ -84,7 +84,6 @@ public function testWithCount(): void
8484
$expected = $tags->map(fn(Tag $tag) => $this->serializer
8585
->tag($tag)
8686
->withRelationshipMeta('posts', ['count' => 1])
87-
->jsonSerialize()
8887
)->all();
8988

9089
$response = $this

tests/dummy/tests/Api/V1/Posts/ReadTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ReadTest extends TestCase
3131
public function test(): void
3232
{
3333
$post = Post::factory()->create();
34-
$expected = $this->serializer->post($post)->jsonSerialize();
34+
$expected = $this->serializer->post($post);
3535

3636
$response = $this
3737
->withoutExceptionHandling()
@@ -56,8 +56,7 @@ public function testIncludeAuthorAndTags(): void
5656
$expected = $this->serializer
5757
->post($post)
5858
->replace('author', $author = ['type' => 'users', 'id' => $post->author])
59-
->replace('tags', $identifiers)
60-
->jsonSerialize();
59+
->replace('tags', $identifiers);
6160

6261
$response = $this
6362
->withoutExceptionHandling()
@@ -96,8 +95,7 @@ public function testIncludeMedia(): void
9695

9796
$expected = $this->serializer
9897
->post($post)
99-
->replace('media', $ids)
100-
->jsonSerialize();
98+
->replace('media', $ids);
10199

102100
$response = $this
103101
->withoutExceptionHandling()
@@ -120,8 +118,7 @@ public function testIncludeEmptyTags(): void
120118

121119
$expected = $this->serializer
122120
->post($post)
123-
->replace('tags', [])
124-
->jsonSerialize();
121+
->replace('tags', []);
125122

126123
$response = $this
127124
->withoutExceptionHandling()
@@ -135,7 +132,7 @@ public function testIncludeEmptyTags(): void
135132
public function testSlugFilter(): void
136133
{
137134
$post = Post::factory()->create();
138-
$expected = $this->serializer->post($post)->jsonSerialize();
135+
$expected = $this->serializer->post($post);
139136

140137
$response = $this
141138
->jsonApi()
@@ -166,13 +163,11 @@ public function testSparseFieldSets(): void
166163
$expected = $this->serializer
167164
->post($post)
168165
->only('author', 'slug', 'synopsis', 'title')
169-
->replace('author', ['type' => 'users', 'id' => $post->author])
170-
->jsonSerialize();
166+
->replace('author', ['type' => 'users', 'id' => $post->author]);
171167

172168
$author = $this->serializer
173169
->user($post->author)
174-
->only('name')
175-
->jsonSerialize();
170+
->only('name');
176171

177172
$response = $this
178173
->withoutExceptionHandling()
@@ -195,8 +190,7 @@ public function testWithCount(): void
195190
$expected = $this->serializer
196191
->post($post)
197192
->withRelationshipMeta('tags', ['count' => 1])
198-
->withRelationshipMeta('comments', ['count' => 3])
199-
->jsonSerialize();
193+
->withRelationshipMeta('comments', ['count' => 3]);
200194

201195
$response = $this
202196
->withoutExceptionHandling()
@@ -246,7 +240,7 @@ public function testDraftUserIsNotAuthor(): void
246240
public function testDraftAsAuthor(): void
247241
{
248242
$post = Post::factory()->create(['published_at' => null]);
249-
$expected = $this->serializer->post($post)->jsonSerialize();
243+
$expected = $this->serializer->post($post);
250244

251245
$response = $this
252246
->actingAs($post->author)

tests/dummy/tests/Api/V1/Posts/UpdateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function test(): void
5555
->serialize()
5656
->replace('tags', $this->identifiersFor('tags', $tags));
5757

58-
$expected = $data->forget('updatedAt')->jsonSerialize();
58+
$expected = $data->forget('updatedAt');
5959

6060
$response = $this
6161
->actingAs($this->post->author)

tests/dummy/tests/Api/V1/Serializer.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function image(Image $image): ResourceObject
4444
'type' => 'images',
4545
'id' => (string) $image->getRouteKey(),
4646
'attributes' => [
47-
'createdAt' => $image->created_at->jsonSerialize(),
48-
'updatedAt' => $image->updated_at->jsonSerialize(),
47+
'createdAt' => $image->created_at,
48+
'updatedAt' => $image->updated_at,
4949
'url' => $image->url,
5050
],
5151
'links' => [
@@ -72,13 +72,13 @@ public function post(Post $post): ResourceObject
7272
'id' => (string) $id,
7373
'attributes' => [
7474
'content' => $post->content,
75-
'createdAt' => optional($post->created_at)->jsonSerialize(),
76-
'deletedAt' => optional($post->deleted_at)->jsonSerialize(),
77-
'publishedAt' => optional($post->published_at)->jsonSerialize(),
75+
'createdAt' => $post->created_at,
76+
'deletedAt' => $post->deleted_at,
77+
'publishedAt' => $post->published_at,
7878
'slug' => $post->slug,
7979
'synopsis' => $post->synopsis,
8080
'title' => $post->title,
81-
'updatedAt' => optional($post->updated_at)->jsonSerialize(),
81+
'updatedAt' => $post->updated_at,
8282
],
8383
'relationships' => [
8484
'author' => [
@@ -129,9 +129,9 @@ public function tag(Tag $tag): ResourceObject
129129
'type' => 'tags',
130130
'id' => (string) $id,
131131
'attributes' => [
132-
'createdAt' => $tag->created_at->jsonSerialize(),
132+
'createdAt' => $tag->created_at,
133133
'name' => $tag->name,
134-
'updatedAt' => $tag->updated_at->jsonSerialize(),
134+
'updatedAt' => $tag->updated_at,
135135
],
136136
'relationships' => [
137137
'posts' => [
@@ -170,9 +170,9 @@ public function user(User $user): ResourceObject
170170
'type' => 'users',
171171
'id' => (string) $id,
172172
'attributes' => [
173-
'createdAt' => $user->created_at->jsonSerialize(),
173+
'createdAt' => $user->created_at,
174174
'name' => $user->name,
175-
'updatedAt' => $user->updated_at->jsonSerialize(),
175+
'updatedAt' => $user->updated_at,
176176
],
177177
'links' => [
178178
'self' => $self,
@@ -195,9 +195,9 @@ public function video(Video $video): ResourceObject
195195
'type' => 'videos',
196196
'id' => (string) $video->getRouteKey(),
197197
'attributes' => [
198-
'createdAt' => $video->created_at->jsonSerialize(),
198+
'createdAt' => $video->created_at,
199199
'title' => $video->title,
200-
'updatedAt' => $video->updated_at->jsonSerialize(),
200+
'updatedAt' => $video->updated_at,
201201
'url' => $video->url,
202202
],
203203
'relationships' => [

tests/dummy/tests/Api/V1/Users/ReadTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public function test(): void
3030
$user = User::factory()->createOne();
3131

3232
$expected = $this->serializer
33-
->user($user)
34-
->jsonSerialize();
33+
->user($user);
3534

3635
$response = $this
3736
->actingAs(User::factory()->createOne())
@@ -46,8 +45,7 @@ public function testMe(): void
4645
$user = User::factory()->createOne();
4746

4847
$expected = $this->serializer
49-
->user($user)
50-
->jsonSerialize();
48+
->user($user);
5149

5250
$response = $this
5351
->actingAs($user)

0 commit comments

Comments
 (0)