/** * @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_deletes_detached_hasmany_and_hasone_related_records_if_configured_to() { $this->app['config']->set('nestedmodelupdater.relations.' . Post::class . '.comments.detach', true); $this->app['config']->set('nestedmodelupdater.relations.' . Post::class . '.comments.delete-detached', true); // setup $post = $this->createPost(); $commentA = $this->createComment($post); $commentB = $this->createComment($post); $data = ['comments' => [$commentB->id]]; // test $updater = new ModelUpdater(Post::class); $updater->update($data, $post); $this->notSeeInDatabase('comments', ['id' => $commentA->id]); $this->seeInDatabase('comments', ['id' => $commentB->id, 'post_id' => $post->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); }