/** * @test */ function it_can_be_configured_not_to_use_database_transactions() { $post = $this->createPost(); $data = ['title' => 'this should be', 'body' => 'rolled back', 'comments' => [['id' => 999]]]; $updater = new ModelUpdater(Post::class); $updater->disableDatabaseTransaction(); try { $updater->update($data, $post); // should never get here $this->fail('Exception should have been thrown while attempting update'); } catch (NestedModelNotFoundException $e) { // expected } // unchanged data $this->seeInDatabase('posts', ['title' => 'this should be', 'body' => 'rolled back']); }
/** * @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\InvalidNestedDataException * @expectedExceptionMessageRegExp #allowed.*['"]auth_1['"]# */ function it_throws_an_exception_if_not_allowed_to_create_for_any_nested_use_of_a_temporary_id() { Config::set('nestedmodelupdater.relations.' . Comment::class . '.author', ['update-only' => true]); $post = $this->createPost(); $comment = $this->createComment($post); $data = ['comments' => [['id' => $comment->id, 'title' => 'updated title', 'author' => ['_tmp_id' => 'auth_1', 'name' => 'Some Author Name']], ['title' => 'new title', 'body' => 'for new comment', 'author' => ['_tmp_id' => 'auth_1']]]]; $updater = new ModelUpdater(Post::class); $updater->update($data, $post); }