Skip to content

Commit

Permalink
add unit test for soft delete/restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Sep 14, 2023
1 parent 3770e94 commit b82e651
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ protected function performDeleteOnRelations()
continue;
}
// we want to remove the pivot record, not the actual relation record
$relation()->detach();
$this->{$name}()->detach();
} else {
if (!Arr::get($options, 'delete', false)) {
continue;
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Traits/SoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ protected function performSoftDeleteOnRelations()
// relations using pivot table
$value = $this->fromDateTime($this->freshTimestamp());
$this->updatePivotDeletedAtColumn($name, $options, $value);

} else {
if ($relation instanceof EloquentModel) {
$relation->delete();
Expand Down Expand Up @@ -187,12 +188,10 @@ public function restore()
*/
protected function updatePivotDeletedAtColumn(string $relationName, array $options, string|null $value)
{
$relation = $this->{$relationName};

// get deletedAtColumn from the relation options, otherwise use default
$deletedAtColumn = array_get($options, 'deletedAtColumn', 'deleted_at');

$query = $relation()->newPivotQuery()->update([
$this->{$relationName}()->newPivotQuery()->update([
$deletedAtColumn => $value,
]);
}
Expand Down
17 changes: 8 additions & 9 deletions tests/Database/Traits/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function setUp(): void
];

$this->createTables();
$this->seedTables();
}

protected function createTables()
Expand All @@ -39,8 +40,6 @@ protected function createTables()
$table->unsignedInteger('category_id');
$table->timestamp('deleted_at')->nullable();
});

$this->seedTables();
}

protected function seedTables()
Expand Down Expand Up @@ -68,7 +67,7 @@ protected function seedTables()

public function testDeleteAndRestore()
{
$post = Post::where('title', 'First Post')->first();
$post = Post::first();
$this->assertTrue($post->deleted_at === null);
$this->assertTrue($post->categories()->where('deleted_at', null)->count() === 2);

Expand Down Expand Up @@ -102,9 +101,9 @@ class Post extends \Winter\Storm\Database\Model
public $belongsToMany = [
'categories' => [
Category::class,
'table' => 'categories_posts',
'key' => 'post_id',
'otherKey' => 'category_id',
'table' => 'categories_posts',
'key' => 'post_id',
'otherKey' => 'category_id',
'softDelete' => true,
],
];
Expand All @@ -124,9 +123,9 @@ class Category extends \Winter\Storm\Database\Model
public $belongsToMany = [
'posts' => [
Post::class,
'table' => 'categories_posts',
'key' => 'category_id',
'otherKey' => 'post_id',
'table' => 'categories_posts',
'key' => 'category_id',
'otherKey' => 'post_id',
],
];
}

0 comments on commit b82e651

Please sign in to comment.