/** * @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_detaches_omitted_nested_belongstomany_relations() { $this->app['config']->set('nestedmodelupdater.relations.' . Post::class . '.authors.link-only', false); // setup $post = $this->createPost(); $authorA = $this->createAuthor(); $authorB = $this->createAuthor(); $post->authors()->sync([$authorA->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]); $data = ['authors' => [$authorA->id, ['name' => 'New Author', 'gender' => 'f']]]; // test $updater = new ModelUpdater(Post::class); $updater->update($data, $post); $authorC = Author::latest()->first(); $this->assertInstanceOf(Author::class, $authorC); $this->seeInDatabase('authors', ['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' => $authorC->id]); $this->notSeeInDatabase('author_post', ['post_id' => $post->id, 'author_id' => $authorB->id]); }