From 553b7be4f9a11113de88d8b4f0132952af2b8554 Mon Sep 17 00:00:00 2001 From: Edinei Valdameri Date: Tue, 4 Jun 2024 16:39:09 -0300 Subject: [PATCH] Implements soft delete --- ...000_00_00_000000_create_anything_table.php | 1 + src/Models/Anything.php | 2 ++ tests/UseCase/SoftDeleteAnythingTest.php | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/UseCase/SoftDeleteAnythingTest.php diff --git a/database/migrations/0000_00_00_000000_create_anything_table.php b/database/migrations/0000_00_00_000000_create_anything_table.php index ce9460c..41e3401 100644 --- a/database/migrations/0000_00_00_000000_create_anything_table.php +++ b/database/migrations/0000_00_00_000000_create_anything_table.php @@ -17,6 +17,7 @@ public function up(): void $table->string('label')->index(); $table->unique(['type', 'slug']); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/src/Models/Anything.php b/src/Models/Anything.php index d937581..b2133e4 100644 --- a/src/Models/Anything.php +++ b/src/Models/Anything.php @@ -9,6 +9,7 @@ use Dex\Laravel\Anything\Models\Concerns\HasSlug; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; /** * @property int $id @@ -22,6 +23,7 @@ class Anything extends Model use HasAnythingType; use HasFactory; use HasSlug; + use SoftDeletes; protected $table = 'anything'; diff --git a/tests/UseCase/SoftDeleteAnythingTest.php b/tests/UseCase/SoftDeleteAnythingTest.php new file mode 100644 index 0000000..51aa968 --- /dev/null +++ b/tests/UseCase/SoftDeleteAnythingTest.php @@ -0,0 +1,30 @@ +updateOrCreate([ + 'label' => 'Black', + ]); + + $raceWhite = Race::query()->updateOrCreate([ + 'label' => 'White', + ]); + + expect(Race::query()->count())->toBe(2); + + $raceWhite->delete(); + + expect(Race::withTrashed()->count())->toBe(2) + ->and(Race::onlyTrashed()->count())->toBe(1) + ->and(Race::find($raceWhite->getKey()))->toBeNull() + ->and(Race::withTrashed()->find($raceWhite->getKey()))->not->toBeNull(); + + $raceWhite->forceDelete(); + + expect(Race::withTrashed()->count())->toBe(1) + ->and(Race::onlyTrashed()->count())->toBe(0) + ->and(Race::find($raceWhite->getKey()))->toBeNull(); +});