From 06f7b6907a57cb72ef1ba795133daacee7c6801a Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Tue, 27 Feb 2024 13:11:08 +0800 Subject: [PATCH] Add soft delete tests specifically with Laravel relations --- tests/Database/Fixtures/UserLaravel.php | 52 ++++++++++++ .../Fixtures/UserLaravelWithSoftAuthor.php | 13 +++ ...UserLaravelWithSoftAuthorAndSoftDelete.php | 8 ++ .../Fixtures/UserLaravelWithSoftDelete.php | 8 ++ tests/Database/Traits/SoftDeleteTest.php | 79 +++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 tests/Database/Fixtures/UserLaravel.php create mode 100644 tests/Database/Fixtures/UserLaravelWithSoftAuthor.php create mode 100644 tests/Database/Fixtures/UserLaravelWithSoftAuthorAndSoftDelete.php create mode 100644 tests/Database/Fixtures/UserLaravelWithSoftDelete.php diff --git a/tests/Database/Fixtures/UserLaravel.php b/tests/Database/Fixtures/UserLaravel.php new file mode 100644 index 000000000..ffc7058b7 --- /dev/null +++ b/tests/Database/Fixtures/UserLaravel.php @@ -0,0 +1,52 @@ +hasOne(Author::class, 'user_id')->dependent(); + } + + public static function migrateUp(Builder $builder): void + { + if ($builder->hasTable('database_tester_users')) { + return; + } + + $builder->create('database_tester_users', function ($table) { + $table->engine = 'InnoDB'; + $table->increments('id'); + $table->string('name')->nullable(); + $table->string('email')->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + public static function migrateDown(Builder $builder): void + { + if (!$builder->hasTable('database_tester_users')) { + return; + } + + $builder->dropIfExists('database_tester_users'); + } +} diff --git a/tests/Database/Fixtures/UserLaravelWithSoftAuthor.php b/tests/Database/Fixtures/UserLaravelWithSoftAuthor.php new file mode 100644 index 000000000..10d6a1426 --- /dev/null +++ b/tests/Database/Fixtures/UserLaravelWithSoftAuthor.php @@ -0,0 +1,13 @@ +hasOne(SoftDeleteAuthor::class, 'user_id')->softDeletable(); + } +} diff --git a/tests/Database/Fixtures/UserLaravelWithSoftAuthorAndSoftDelete.php b/tests/Database/Fixtures/UserLaravelWithSoftAuthorAndSoftDelete.php new file mode 100644 index 000000000..71e374513 --- /dev/null +++ b/tests/Database/Fixtures/UserLaravelWithSoftAuthorAndSoftDelete.php @@ -0,0 +1,8 @@ +assertNull(Author::find($authorId)); } + public function testDeleteOptionOnHardModelLaravelRelation() + { + Model::unguard(); + $user = UserLaravel::create(['name' => 'Stevie', 'email' => 'stevie@example.com']); + $author = Author::create(['name' => 'Louie', 'email' => 'louie@example.com', 'user_id' => $user->id]); + Model::reguard(); + + $authorId = $author->id; + $user->delete(); // Hard + $this->assertNull(Author::find($authorId)); + } + public function testSoftDeleteOptionOnHardModel() { Model::unguard(); @@ -37,6 +53,18 @@ public function testSoftDeleteOptionOnHardModel() $this->assertNotNull(Author::find($authorId)); // Do nothing } + public function testSoftDeleteOptionOnHardModelLaravelRelation() + { + Model::unguard(); + $user = UserLaravelWithSoftAuthor::create(['name' => 'Stevie', 'email' => 'stevie@example.com']); + $author = Author::create(['name' => 'Louie', 'email' => 'louie@example.com', 'user_id' => $user->id]); + Model::reguard(); + + $authorId = $author->id; + $user->delete(); // Hard + $this->assertNotNull(Author::find($authorId)); // Do nothing + } + public function testSoftDeleteOptionOnSoftModel() { Model::unguard(); @@ -50,6 +78,19 @@ public function testSoftDeleteOptionOnSoftModel() $this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId)); } + public function testSoftDeleteOptionOnSoftModelLaravelRelation() + { + Model::unguard(); + $user = UserLaravelWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@example.com']); + $author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => 'louie@example.com', 'user_id' => $user->id]); + Model::reguard(); + + $authorId = $author->id; + $user->delete(); // Soft + $this->assertNull(SoftDeleteAuthor::find($authorId)); + $this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId)); + } + public function testDeleteOptionOnSoftModel() { Model::unguard(); @@ -69,6 +110,25 @@ public function testDeleteOptionOnSoftModel() $this->assertNull(Author::find($authorId)); } + public function testDeleteOptionOnSoftModelLaravelRelation() + { + Model::unguard(); + $user = UserLaravelWithSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@example.com']); + $author = Author::create(['name' => 'Louie', 'email' => 'louie@example.com', 'user_id' => $user->id]); + Model::reguard(); + + $authorId = $author->id; + $user->delete(); // Soft + $this->assertNotNull(Author::find($authorId)); // Do nothing + + $userId = $user->id; + $user = UserLaravelWithSoftDelete::withTrashed()->find($userId); + $user->restore(); + + $user->forceDelete(); // Hard + $this->assertNull(Author::find($authorId)); + } + public function testRestoreSoftDeleteRelation() { Model::unguard(); @@ -87,4 +147,23 @@ public function testRestoreSoftDeleteRelation() $this->assertNotNull(SoftDeleteAuthor::find($authorId)); } + + public function testRestoreSoftDeleteRelationLaravelRelation() + { + Model::unguard(); + $user = UserLaravelWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@example.com']); + $author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => 'louie@example.com', 'user_id' => $user->id]); + Model::reguard(); + + $authorId = $author->id; + $user->delete(); // Soft + $this->assertNull(SoftDeleteAuthor::find($authorId)); + $this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId)); + + $userId = $user->id; + $user = UserLaravelWithSoftAuthorAndSoftDelete::withTrashed()->find($userId); + $user->restore(); + + $this->assertNotNull(SoftDeleteAuthor::find($authorId)); + } }