-
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 4744c81
Showing
6 changed files
with
261 additions
and
2 deletions.
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,42 @@ | ||
<?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 (!$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'; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Mongolid\Util; | ||
|
||
use Mongolid\Model\ModelInterface; | ||
|
||
class SoftDeleteQueries | ||
{ | ||
public static function insertFilterForSoftDelete(array $query, ModelInterface $model): array | ||
{ | ||
$field = self::getDeletedAtColumn($model); | ||
|
||
return array_merge( | ||
$query, | ||
[ | ||
'$or' => [ | ||
[ | ||
$field => null, | ||
], | ||
[ | ||
$field => ['$exists' => false], | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
private static function getDeletedAtColumn(ModelInterface $model): string | ||
{ | ||
return defined( | ||
$model::class . '::DELETED_AT' | ||
) | ||
? $model::DELETED_AT | ||
: 'deleted_at'; | ||
} | ||
} |
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,99 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Integration; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Tests\Stubs\ProductWithSoftDelete; | ||
|
||
final class PersisteModelWithSoftDeleteTest extends IntegrationTestCase | ||
{ | ||
private ObjectId $_id; | ||
|
||
public function testShouldFindNotDeletedProduct(): void | ||
{ | ||
// Set | ||
$product = $this->persiteProduct(); | ||
|
||
// Actions | ||
$result = ProductWithSoftDelete::where()->first(); | ||
|
||
// Assertion | ||
$this->assertEquals($product, $result); | ||
} | ||
|
||
public function testShouldNotFindDeletedProduct(): void | ||
{ | ||
// Set | ||
$this->persiteProduct(true); | ||
|
||
// Actions | ||
$result = ProductWithSoftDelete::where()->first(); | ||
|
||
// Assertion | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testShouldFindDeletedProductWithFirst(): void | ||
{ | ||
// Set | ||
$product = $this->persiteProduct(); | ||
|
||
// Actions | ||
$result = ProductWithSoftDelete::first($this->_id); | ||
|
||
// Assertion | ||
$this->assertEquals($product, $result); | ||
} | ||
|
||
public function testShouldNotFindDeletedProductWithFirst(): void | ||
{ | ||
// Set | ||
$this->persiteProduct(true); | ||
|
||
// Actions | ||
$result = ProductWithSoftDelete::first($this->_id); | ||
|
||
// Assertion | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testShouldRestoreDeletedProduct(): void | ||
{ | ||
// Set | ||
$product = $this->persiteProduct(true); | ||
|
||
// Actions | ||
$product->restore(); | ||
$result = ProductWithSoftDelete::first($this->_id); | ||
|
||
// Assertion | ||
$this->assertEquals($product, $result); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_id = new ObjectId('5bcb310783a7fcdf1bf1a672'); | ||
} | ||
|
||
private function persiteProduct(bool $softDeleted = false): ProductWithSoftDelete | ||
{ | ||
$product = new ProductWithSoftDelete(); | ||
$product->_id = $this->_id; | ||
$product->short_name = 'Furadeira de Impacto Bosch com Chave de Mandril '; | ||
$product->name = 'Furadeira de Impacto Bosch com Chave de Mandril e Acessórios 550W 1/2 GSB 550 RE 127V (110V)'; | ||
|
||
if ($softDeleted) { | ||
$date = new UTCDateTime(new DateTime('today')); | ||
|
||
$product->deleted_at = $date; | ||
} | ||
|
||
$product->save(); | ||
|
||
return $product; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs; | ||
|
||
use Mongolid\Model\SoftDeletesTrait; | ||
|
||
class ProductWithSoftDelete extends Product | ||
{ | ||
use SoftDeletesTrait; | ||
} |
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,67 @@ | ||
<?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; | ||
use Mongolid\Tests\Stubs\ProductWithSoftDelete; | ||
|
||
class SoftDeletesTraitTest extends TestCase | ||
{ | ||
public function testShouldReturnStatusOfSoftDelete(): void | ||
{ | ||
// Set | ||
$date = new UTCDateTime(new DateTime('today')); | ||
$product = new ProductWithSoftDelete(); | ||
$product->deleted_at = $date; | ||
|
||
// Actions | ||
$actual = $product->isTrashed(); | ||
|
||
// Assertions | ||
$this->assertTrue($actual); | ||
} | ||
|
||
public function testShouldRestoreProduct(): void | ||
{ | ||
// Set | ||
$product = new ProductWithSoftDelete(); | ||
$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 = new ProductWithSoftDelete(); | ||
|
||
// Actions | ||
$actual = $product->restore(); | ||
|
||
// Assertions | ||
$this->assertFalse($actual); | ||
} | ||
} |