Skip to content

Commit

Permalink
Implements soft delete
Browse files Browse the repository at this point in the history
  • Loading branch information
edineivaldameri committed Jun 4, 2024
1 parent f120505 commit 553b7be
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up(): void
$table->string('label')->index();
$table->unique(['type', 'slug']);
$table->timestamps();
$table->softDeletes();
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/Models/Anything.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +23,7 @@ class Anything extends Model
use HasAnythingType;
use HasFactory;
use HasSlug;
use SoftDeletes;

protected $table = 'anything';

Expand Down
30 changes: 30 additions & 0 deletions tests/UseCase/SoftDeleteAnythingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Workbench\Dex\Laravel\Anything\App\Models\Race;

test('soft delete', function () {
$raceBlack = Race::query()->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();
});

0 comments on commit 553b7be

Please sign in to comment.