From 558dda1db052b03dd9abc2ab1e77719d92f20771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20SANCHEZ?= Date: Sat, 11 Dec 2021 18:39:05 +0100 Subject: [PATCH 1/2] SoftDelete::performDeleteOnModel() sets "exists" property to false only when succeeded --- .../Database/Eloquent/SoftDeletes.php | 6 ++-- ...baseEloquentSoftDeletesIntegrationTest.php | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index cac971ccd59b..aa6c81784972 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -64,9 +64,9 @@ public function forceDelete() protected function performDeleteOnModel() { if ($this->forceDeleting) { - $this->exists = false; - - return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(); + return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () { + $this->exists = false; + }); } return $this->runSoftDelete(); diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 9bc91cbbb882..89b3ffbdd213 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -11,6 +11,7 @@ use Illuminate\Pagination\CursorPaginator; use Illuminate\Pagination\Paginator; use Illuminate\Support\Carbon; +use Mockery; use PHPUnit\Framework\TestCase; class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase @@ -189,6 +190,41 @@ public function testForceDeleteActuallyDeletesRecords() $this->assertEquals(1, $users->first()->id); } + public function testForceDeleteUpdateExistsProperty() + { + $this->createUsers(); + $user = SoftDeletesTestUser::find(2); + + $this->assertTrue($user->exists); + + $user->forceDelete(); + + $this->assertFalse($user->exists); + } + + public function testForceDeleteDoesntUpdateExistsPropertyIfFailed() + { + $user = new class() extends SoftDeletesTestUser { + public $exists = true; + + public function newModelQuery() + { + return Mockery::spy(parent::newModelQuery(), function (Mockery\MockInterface $mock) { + $mock->shouldReceive('forceDelete')->andThrow(new \Exception()); + }); + } + }; + + $this->assertTrue($user->exists); + + try { + $user->forceDelete(); + } catch (\Exception $exception) { + } + + $this->assertTrue($user->exists); + } + public function testRestoreRestoresRecords() { $this->createUsers(); From 3b545507793d4f64fa16f6e02b0e0e52ec793129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20SANCHEZ?= Date: Sat, 11 Dec 2021 19:08:51 +0100 Subject: [PATCH 2/2] Fix lint --- tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 89b3ffbdd213..786c9c35242b 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -204,7 +204,8 @@ public function testForceDeleteUpdateExistsProperty() public function testForceDeleteDoesntUpdateExistsPropertyIfFailed() { - $user = new class() extends SoftDeletesTestUser { + $user = new class() extends SoftDeletesTestUser + { public $exists = true; public function newModelQuery()