/** * @test */ function it_creates_and_updates_a_nested_relation_using_multiple_distinct_temporary_ids() { $post = $this->createPost(); $comment = $this->createComment($post); $data = ['comments' => [['id' => $comment->id, 'title' => 'updated title', 'author' => ['_tmp_id' => 'auth_1', 'name' => 'new shared author', 'posts' => [['title' => 'new nested title', 'body' => 'new nested body', 'genre' => ['_tmp_id' => 'genre_2']]]]], ['title' => 'new title', 'body' => 'for new comment', 'author' => ['_tmp_id' => 'auth_1']]], 'genre' => ['_tmp_id' => 'genre_2', 'name' => 'new shared genre']]; $updater = new ModelUpdater(Post::class); $updater->update($data, $post); $this->assertEquals(1, Author::count(), "Exactly one author should have been created"); /** @var Author $author */ $author = Author::first(); $this->assertEquals(1, Genre::count(), "Exactly one tag should have been created"); /** @var Genre $genre */ $genre = Genre::first(); $this->assertEquals(2, Post::count(), "Exactly two posts should exist (1 created by nesting)"); /** @var Post $newPost */ $newPost = Post::orderBy('id', 'desc')->first(); $this->seeInDatabase('comments', ['id' => $comment->id, 'title' => 'updated title']); $this->seeInDatabase('comments', ['title' => 'new title', 'body' => 'for new comment']); $this->seeInDatabase('authors', ['id' => $author->id, 'name' => 'new shared author']); $this->seeInDatabase('posts', ['id' => $post->id, 'genre_id' => $genre->id]); $this->seeInDatabase('posts', ['id' => $newPost->id, 'title' => 'new nested title', 'genre_id' => $genre->id]); $this->seeInDatabase('genres', ['id' => $genre->id, 'name' => 'new shared genre']); }