Skip to content

Commit

Permalink
refact: add PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 18, 2023
1 parent 569afaa commit fcf4d5d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 89 deletions.
12 changes: 0 additions & 12 deletions src/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,6 @@ public function update($entity, array $options = []): bool
*/
public function delete($entity, array $options = []): bool
{
if (($entity->enabledSoftDeletes ?? false) && !($entity->forceDelete ?? false)) {
return $this->executeSoftDelete($entity, $options);
}

if (false === $this->fireEvent('deleting', $entity, true)) {
return false;
}
Expand Down Expand Up @@ -616,12 +612,4 @@ function ($value) {

return $filtered;
}

private function executeSoftDelete(ModelInterface $entity, array $options): bool
{
$deletedAtCoullum = QueryBuilder::getDeletedAtColumn($entity);
$entity->$deletedAtCoullum = new UTCDateTime(new DateTime('now'));

return $this->update($entity, $options);
}
}
4 changes: 4 additions & 0 deletions src/LegacyRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function update(): bool
*/
public function delete(): bool
{
if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) {
return $this->executeSoftDelete();
}

return $this->execute('delete');
}

Expand Down
4 changes: 4 additions & 0 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public function update(): bool
*/
public function delete(): bool
{
if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) {
return $this->executeSoftDelete();
}

return $this->execute('delete');
}

Expand Down
10 changes: 10 additions & 0 deletions src/Model/SoftDeletesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mongolid\Model;

use DateTime;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Cursor\CursorInterface;
use Mongolid\Util\QueryBuilder;

Expand Down Expand Up @@ -35,6 +37,14 @@ public function forceDelete(): bool
return $this->execute('delete');
}

public function executeSoftDelete(): bool
{
$deletedAtCoullum = QueryBuilder::getDeletedAtColumn($this);
$this->$deletedAtCoullum = new UTCDateTime(new DateTime('now'));

return $this->update();
}

public static function withTrashed(
array|string|int $query = [],
array $projection = [],
Expand Down
12 changes: 0 additions & 12 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ public function update(ModelInterface $model, array $options = []): bool
*/
public function delete(ModelInterface $model, array $options = []): bool
{
if (($model->enabledSoftDeletes ?? false) && !($model->forceDelete ?? false)) {
return $this->executeSoftDelete($model, $options);
}

if (false === $this->fireEvent('deleting', $model, true)) {
return false;
}
Expand Down Expand Up @@ -427,12 +423,4 @@ private function getUpdateData($model, array $data): array

return $changes;
}

private function executeSoftDelete(ModelInterface $entity, array $options): bool
{
$deletedAtCoullum = QueryBuilder::getDeletedAtColumn($entity);
$entity->$deletedAtCoullum = new UTCDateTime(new DateTime('now'));

return $this->update($entity, $options);
}
}
65 changes: 0 additions & 65 deletions tests/Unit/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,71 +645,6 @@ public function testShouldDelete($entity, $writeConcern, $shouldFireEventAfter,
$this->assertEquals($expected, $mapper->delete($entity, $options));
}

public function testShouldExecuteSoftDelete()
{
// Set
$entity = m::mock(ModelInterface::class);
$writeConcern = 1;
$connection = m::mock(Connection::class);
$mapper = m::mock(
DataMapper::class . '[parseToDocument,getCollection]',
[$connection]
);

$collection = m::mock(Collection::class);
$parsedObject = ['_id' => 123];
$operationResult = m::mock();
$options = ['writeConcern' => new WriteConcern($writeConcern)];

$entity->_id = 123;
$entity->enabledSoftDeletes = true;

// Expectations
$mapper->shouldAllowMockingProtectedMethods();

$mapper->shouldReceive('parseToDocument')
->once()
->with($entity)
->andReturn($parsedObject);

$mapper->shouldReceive('getCollection')
->once()
->andReturn($collection);

$collection->shouldReceive('updateOne')
->once()
->with(
['_id' => 123],
['$set' => $parsedObject],
['writeConcern' => new WriteConcern($writeConcern)]
)->andReturn($operationResult);

$operationResult->shouldReceive('isAcknowledged')
->once()
->andReturn((bool) $writeConcern);

$operationResult->shouldReceive('getModifiedCount')
->andReturn(1);

$entity->shouldReceive('syncOriginalDocumentAttributes')
->once()
->with();

$entity->expects()
->getOriginalDocumentAttributes()
->andReturn([]);

$this->expectEventToBeFired('updating', $entity, true);

$this->expectEventToBeFired('updated', $entity, false);

// Actions
$actual = $mapper->delete($entity, $options);

// Assert
$this->assertTrue($actual);
}

/**
* @dataProvider eventsToBailOperations
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Model/SoftDeletesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,56 @@ public function testShouldforceDeleteProduct(): void
$this->assertTrue($actual);
}

public function testShouldExecuteSoftDeleteProduct(): void
{
// Set
$product = new ProductWithSoftDelete();

$dataMapper = $this->instance(
DataMapper::class,
m::mock(DataMapper::class)
);

// Expectations
$dataMapper->expects()
->setSchema(m::type(DynamicSchema::class));

$dataMapper->expects()
->update(m::type(Product::class), m::type('array'))
->andReturnTrue();

// Actions
$actual = $product->delete();

// Assertions
$this->assertTrue($actual);
}

public function testShouldNotExecuteSoftDelete(): void
{
// Set
$product = new Product();

$dataMapper = $this->instance(
DataMapper::class,
m::mock(DataMapper::class)
);

// Expectations
$dataMapper->expects()
->setSchema(m::type(DynamicSchema::class));

$dataMapper->expects()
->delete(m::type(Product::class), m::type('array'))
->andReturnTrue();

// Actions
$actual = $product->delete();

// Assertions
$this->assertTrue($actual);
}

public function testShouldFindWithTrashedProducts(): void
{
// Set
Expand Down

0 comments on commit fcf4d5d

Please sign in to comment.