Skip to content

Commit

Permalink
Merge pull request #2 from edersoares/soft-delete
Browse files Browse the repository at this point in the history
Soft delete
  • Loading branch information
edersoares authored Jun 5, 2024
2 parents 1694975 + cd7d9d0 commit fcc1977
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 @@ -11,6 +11,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @property int $id
Expand All @@ -28,6 +29,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 fcc1977

Please sign in to comment.