/** * @test */ function it_creates_a_new_nested_model_related_as_belongs_to() { $post = $this->createPost(); $data = ['title' => 'updated aswell', 'genre' => ['name' => 'New Genre']]; $updater = new ModelUpdater(Post::class); $updater->update($data, $post); $this->seeInDatabase('posts', ['id' => $post->id, 'title' => 'updated aswell']); $post = Post::find($post->id); $this->assertEquals(1, $post->genre_id, "New Genre should be associated with Post"); $this->seeInDatabase('genres', ['name' => 'New Genre']); }
/** * @test */ function it_creates_a_new_nested_model_related_as_belongs_to() { $data = ['title' => 'created', 'body' => 'fresh', 'comments' => [['title' => 'created comment', 'body' => 'comment body', 'author' => ['name' => 'new author']]]]; $post = Post::create($data); $this->assertInstanceOf(Post::class, $post); $this->assertTrue($post->exists); $author = Author::latest()->first(); $this->assertInstanceOf(Author::class, $author, "Author model should have been created"); $this->seeInDatabase('posts', ['id' => $post->id, 'title' => 'created', 'body' => 'fresh']); $this->seeInDatabase('comments', ['post_id' => $post->id, 'title' => 'created comment', 'body' => 'comment body', 'author_id' => $author->id]); $this->seeInDatabase('authors', ['id' => $author->id, 'name' => 'new author']); }
/** * @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']); }
/** * @test */ function it_creates_a_deeply_nested_structure_with_linked_models() { $genre = $this->createGenre(); $authorA = $this->createAuthor(); $authorB = $this->createAuthor(); $data = ['title' => 'new title', 'body' => 'new body', 'genre' => $genre->id, 'comments' => [['title' => 'title 1', 'body' => 'body 1', 'author' => ['id' => $authorA->id]], ['title' => 'title 2', 'body' => 'body 2', 'author' => $authorB->id]], 'authors' => [$authorA->id, ['id' => $authorB->id]]]; $updater = new ModelUpdater(Post::class); $updater->create($data); // check the whole structure $post = Post::latest()->first(); $this->assertInstanceOf(Post::class, $post); $this->seeInDatabase('posts', ['id' => $post->id, 'title' => 'new title', 'body' => 'new body', 'genre_id' => $genre->id]); $this->seeInDatabase('comments', ['title' => 'title 1', 'body' => 'body 1', 'author_id' => $authorA->id]); $this->seeInDatabase('comments', ['title' => 'title 2', 'body' => 'body 2', 'author_id' => $authorB->id]); $this->seeInDatabase('author_post', ['post_id' => $post->id, 'author_id' => $authorA->id]); $this->seeInDatabase('author_post', ['post_id' => $post->id, 'author_id' => $authorB->id]); }
/** * @param Post $post * @param string $title * @param string $body * @param null $author * @return Comment */ protected function createComment(Post $post, $title = 'testing title', $body = 'testing body', $author = null) { $comment = new Comment(['title' => $title, 'body' => $body]); if ($author) { $comment->author()->associate($author); } return $post->comments()->save($comment); }