/** * @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]); }
/** * @test * @expectedException \Czim\NestedModelUpdater\Exceptions\DisallowedNestedActionException * @expectedExceptionMessageRegExp #authors\.0#i */ function it_throws_an_exception_if_not_allowed_to_create_an_update_only_nested_model_record() { $this->app['config']->set('nestedmodelupdater.relations.' . Post::class . '.authors', ['link-only' => false, 'update-only' => true]); $data = ['title' => 'Problem Post', 'body' => 'Body', 'authors' => [['name' => 'New Name']]]; $updater = new ModelUpdater(Post::class); $updater->create($data); }