-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add soft delete tests specifically with Laravel relations
- Loading branch information
1 parent
a2b12e5
commit 06f7b69
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Database/Fixtures/UserLaravelWithSoftAuthorAndSoftDelete.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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)); | ||
} | ||
} |