-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90c61cb
commit 603bd7a
Showing
3 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model; | ||
|
||
trait SoftDeletesTrait | ||
{ | ||
public $enabledSoftDeletes = true; | ||
|
||
public function isTrashed(): bool | ||
{ | ||
return !is_null($this->{$this->getDeletedAtColumn()}); | ||
} | ||
|
||
public function restore(): bool | ||
{ | ||
$collumn = $this->getDeletedAtColumn(); | ||
if (!$collumn = $this->{$collumn}) { | ||
return false; | ||
} | ||
|
||
$this->{$collumn} = null; | ||
|
||
return $this->execute('save'); | ||
} | ||
|
||
public function forceDelete(): bool | ||
{ | ||
$this->forceDelete = true; | ||
|
||
return $this->execute('delete'); | ||
} | ||
|
||
private function getDeletedAtColumn(): string | ||
{ | ||
return defined( | ||
static::class . '::DELETED_AT' | ||
) | ||
? static::DELETED_AT | ||
: 'deleted_at'; | ||
} | ||
|
||
private function getFiltredQuery(): array | ||
{ | ||
return [ | ||
'$or' => [ | ||
[ | ||
'deleted_at' => null, | ||
], | ||
[ | ||
'deleted_at' => ['$exists' => false], | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\DataMapper\DataMapper; | ||
use Mongolid\Schema\DynamicSchema; | ||
use Mongolid\TestCase; | ||
use Mongolid\Tests\Stubs\Product; | ||
use Mockery as m; | ||
|
||
class SoftDeletesTraitTest extends TestCase | ||
{ | ||
public function testShouldReturnStatusOfSoftDelete(): void | ||
{ | ||
// Set | ||
$date = new UTCDateTime(new DateTime('today')); | ||
$product = $this->makeProduct(); | ||
$product->deleted_at = $date; | ||
|
||
// Actions | ||
$actual = $product->isTrashed(); | ||
|
||
// Assertions | ||
$this->assertTrue($actual); | ||
} | ||
|
||
public function testShouldRestoreProduct(): void | ||
{ | ||
// Set | ||
$product = $this->makeProduct(); | ||
$date = new UTCDateTime(new DateTime('today')); | ||
$product->deleted_at = $date; | ||
$dataMapper = $this->instance( | ||
DataMapper::class, | ||
m::mock(DataMapper::class) | ||
); | ||
|
||
// Expectations | ||
$dataMapper->expects() | ||
->setSchema(m::type(DynamicSchema::class)); | ||
|
||
$dataMapper->expects() | ||
->save(m::type(Product::class), m::type('array')) | ||
->andReturnTrue(); | ||
|
||
// Actions | ||
$actual = $product->restore(); | ||
|
||
// Assertions | ||
$this->assertTrue($actual); | ||
} | ||
|
||
public function testShouldNotRestoreProduct(): void | ||
{ | ||
// Set | ||
$product = $this->makeProduct(); | ||
|
||
// Actions | ||
$actual = $product->restore(); | ||
|
||
// Assertions | ||
$this->assertFalse($actual); | ||
} | ||
|
||
private function makeProduct(): Product | ||
{ | ||
return new class extends Product { | ||
use SoftDeletesTrait; | ||
}; | ||
} | ||
} |