Skip to content

Commit 5202f9e

Browse files
committed
[Bugfix] Add failing test for relationship merging in resource object
This is fixed by updating the laravel-json-api/core package.
1 parent b0a10d9 commit 5202f9e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function fields(): array
7575
DateTime::make('publishedAt')->sortable(),
7676
Str::make('slug'),
7777
Str::make('synopsis'),
78-
BelongsToMany::make('tags'),
78+
BelongsToMany::make('tags')->mustValidate(),
7979
Str::make('title')->sortable(),
8080
DateTime::make('updatedAt')->sortable()->readOnly(),
8181
];

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ protected function setUp(): void
4545

4646
public function test(): void
4747
{
48-
$tag = Tag::factory()->create();
49-
$this->post->tags()->attach($tag);
48+
$this->post->tags()->saveMany(
49+
Tag::factory()->count(2)->create()
50+
);
51+
52+
$tags = Tag::factory()->count(1)->create();
53+
54+
$data = $this
55+
->serialize()
56+
->replace('tags', $this->identifiersFor('tags', $tags));
5057

51-
$data = $this->serialize();
5258
$expected = $data->forget('updatedAt')->jsonSerialize();
5359

5460
$response = $this
@@ -69,6 +75,16 @@ public function test(): void
6975
'synopsis' => $data['synopsis'],
7076
'title' => $data['title'],
7177
]);
78+
79+
$this->assertDatabaseCount('taggables', count($tags));
80+
81+
foreach ($tags as $tag) {
82+
$this->assertDatabaseHas('taggables', [
83+
'tag_id' => $tag->getKey(),
84+
'taggable_type' => Post::class,
85+
'taggable_id' => $this->post->getKey(),
86+
]);
87+
}
7288
}
7389

7490
/**

0 commit comments

Comments
 (0)