Skip to content

Commit

Permalink
Add soft delete tests specifically with Laravel relations
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Feb 27, 2024
1 parent a2b12e5 commit 06f7b69
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/Database/Fixtures/UserLaravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Winter\Storm\Tests\Database\Fixtures;

use Illuminate\Database\Schema\Builder;
use Winter\Storm\Database\Model;
use Winter\Storm\Database\Relations\HasOne;

class UserLaravel extends Model
{
use MigratesForTesting;

/**
* @var string The database table used by the model.
*/
public $table = 'database_tester_users';

/**
* @var array Guarded fields
*/
protected $guarded = [];

public function author(): HasOne
{
return $this->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');
}
}
13 changes: 13 additions & 0 deletions tests/Database/Fixtures/UserLaravelWithSoftAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Winter\Storm\Tests\Database\Fixtures;

use Winter\Storm\Database\Relations\HasOne;

class UserLaravelWithSoftAuthor extends UserLaravel
{
public function author(): HasOne
{
return $this->hasOne(SoftDeleteAuthor::class, 'user_id')->softDeletable();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Winter\Storm\Tests\Database\Fixtures;

class UserLaravelWithSoftAuthorAndSoftDelete extends UserLaravelWithSoftAuthor
{
use \Winter\Storm\Database\Traits\SoftDelete;
}
8 changes: 8 additions & 0 deletions tests/Database/Fixtures/UserLaravelWithSoftDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Winter\Storm\Tests\Database\Fixtures;

class UserLaravelWithSoftDelete extends UserLaravel
{
use \Winter\Storm\Database\Traits\SoftDelete;
}
79 changes: 79 additions & 0 deletions tests/Database/Traits/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Winter\Storm\Tests\Database\Fixtures\UserWithAuthorAndSoftDelete;
use Winter\Storm\Tests\Database\Fixtures\UserWithSoftAuthorAndSoftDelete;
use Winter\Storm\Database\Model;
use Winter\Storm\Tests\Database\Fixtures\UserLaravel;
use Winter\Storm\Tests\Database\Fixtures\UserLaravelWithSoftAuthor;
use Winter\Storm\Tests\Database\Fixtures\UserLaravelWithSoftAuthorAndSoftDelete;
use Winter\Storm\Tests\Database\Fixtures\UserLaravelWithSoftDelete;
use Winter\Storm\Tests\DbTestCase;

class SoftDeleteTest extends DbTestCase
Expand All @@ -25,6 +29,18 @@ public function testDeleteOptionOnHardModel()
$this->assertNull(Author::find($authorId));
}

public function testDeleteOptionOnHardModelLaravelRelation()
{
Model::unguard();
$user = UserLaravel::create(['name' => 'Stevie', 'email' => '[email protected]']);
$author = Author::create(['name' => 'Louie', 'email' => '[email protected]', 'user_id' => $user->id]);
Model::reguard();

$authorId = $author->id;
$user->delete(); // Hard
$this->assertNull(Author::find($authorId));
}

public function testSoftDeleteOptionOnHardModel()
{
Model::unguard();
Expand All @@ -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' => '[email protected]']);
$author = Author::create(['name' => 'Louie', 'email' => '[email protected]', 'user_id' => $user->id]);
Model::reguard();

$authorId = $author->id;
$user->delete(); // Hard
$this->assertNotNull(Author::find($authorId)); // Do nothing
}

public function testSoftDeleteOptionOnSoftModel()
{
Model::unguard();
Expand All @@ -50,6 +78,19 @@ public function testSoftDeleteOptionOnSoftModel()
$this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId));
}

public function testSoftDeleteOptionOnSoftModelLaravelRelation()
{
Model::unguard();
$user = UserLaravelWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => '[email protected]']);
$author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => '[email protected]', '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();
Expand All @@ -69,6 +110,25 @@ public function testDeleteOptionOnSoftModel()
$this->assertNull(Author::find($authorId));
}

public function testDeleteOptionOnSoftModelLaravelRelation()
{
Model::unguard();
$user = UserLaravelWithSoftDelete::create(['name' => 'Stevie', 'email' => '[email protected]']);
$author = Author::create(['name' => 'Louie', 'email' => '[email protected]', '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();
Expand All @@ -87,4 +147,23 @@ public function testRestoreSoftDeleteRelation()

$this->assertNotNull(SoftDeleteAuthor::find($authorId));
}

public function testRestoreSoftDeleteRelationLaravelRelation()
{
Model::unguard();
$user = UserLaravelWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => '[email protected]']);
$author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => '[email protected]', '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));
}
}

0 comments on commit 06f7b69

Please sign in to comment.