diff --git a/tests/Database/Fixtures/UserLaravel.php b/tests/Database/Fixtures/UserLaravel.php new file mode 100644 index 00000000..ffc7058b --- /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 00000000..10d6a142 --- /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 00000000..71e37451 --- /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)); + } }