Skip to content

Commit

Permalink
feat: add softDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 15, 2023
1 parent 90c61cb commit 603bd7a
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,16 @@ public function where(

$model = new $this->schema->entityClass;

$query = $this->prepareValueQuery($query);

$query = $this->insertFilterForSoftDelete($query);

return new $cursorClass(
$this->schema,
$this->getCollection(),
'find',
[
$this->prepareValueQuery($query),
$query,
[
'projection' => $this->prepareProjection($projection),
'eagerLoads' => $model->with ?? [],
Expand Down Expand Up @@ -665,4 +669,21 @@ function ($value) {

return $filtered;
}

private function insertFilterForSoftDelete(array $query): array
{
return array_merge(
$query,
[
'$or' => [
[
'deleted_at' => null,
],
[
'deleted_at' => ['$exists' => false],
],
],
]
);
}
}
55 changes: 55 additions & 0 deletions src/Model/SoftDeletesTrait.php
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],
],
],
];
}
}
73 changes: 73 additions & 0 deletions tests/Unit/Model/SoftDeletesTraitTest.php
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;
};
}
}

0 comments on commit 603bd7a

Please sign in to comment.