From 4bde2add570fe7b80835c289c2bbddc8a0b80651 Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Thu, 24 Aug 2023 18:41:51 -0300 Subject: [PATCH] refact: add PR suggestions --- src/DataMapper/DataMapper.php | 10 ++- src/LegacyRecord.php | 4 +- src/Model/AbstractModel.php | 4 +- src/Model/SoftDeleteTrait.php | 84 +++++++++++++++++++ src/Model/SoftDeletesTrait.php | 69 --------------- src/Query/Builder.php | 11 ++- src/Util/QueryBuilder.php | 24 +++--- ... PersistLegacyModelWithSoftDeleteTest.php} | 84 +++++++++---------- ...php => PersistModelWithSoftDeleteTest.php} | 74 ++++++++-------- tests/Stubs/Legacy/ProductWithSoftDelete.php | 4 +- tests/Stubs/ProductWithSoftDelete.php | 4 +- tests/Unit/DataMapper/DataMapperTest.php | 3 - tests/Unit/Model/SoftDeletesTraitTest.php | 6 +- tests/Unit/Query/BuilderTest.php | 7 -- tests/Unit/Util/QueryBuilderTest.php | 22 +++-- 15 files changed, 219 insertions(+), 191 deletions(-) create mode 100644 src/Model/SoftDeleteTrait.php delete mode 100644 src/Model/SoftDeletesTrait.php rename tests/Integration/{PersisteLegacyModelWithSoftDeleteTest.php => PersistLegacyModelWithSoftDeleteTest.php} (55%) rename tests/Integration/{PersisteModelWithSoftDeleteTest.php => PersistModelWithSoftDeleteTest.php} (57%) diff --git a/src/DataMapper/DataMapper.php b/src/DataMapper/DataMapper.php index cc8550b8..9bb5a994 100644 --- a/src/DataMapper/DataMapper.php +++ b/src/DataMapper/DataMapper.php @@ -2,9 +2,7 @@ namespace Mongolid\DataMapper; -use DateTime; use InvalidArgumentException; -use MongoDB\BSON\UTCDateTime; use MongoDB\Collection; use Mongolid\Container\Container; use Mongolid\Cursor\CursorInterface; @@ -27,6 +25,8 @@ */ class DataMapper implements HasSchemaInterface { + public bool $withTrashed = false; + /** * Name of the schema class to be used. * @@ -261,12 +261,16 @@ public function where( $model = new $this->schema->entityClass; + $query = $this->withTrashed ? + QueryBuilder::prepareValueQuery($query) : + QueryBuilder::resolveQuery($query, $model); + return new $cursorClass( $this->schema, $this->getCollection(), 'find', [ - QueryBuilder::resolveQuery($query, $model), + $query, [ 'projection' => $this->prepareProjection($projection), 'eagerLoads' => $model->with ?? [], diff --git a/src/LegacyRecord.php b/src/LegacyRecord.php index 2b0d383c..5904e448 100644 --- a/src/LegacyRecord.php +++ b/src/LegacyRecord.php @@ -116,7 +116,7 @@ public function update(): bool */ public function delete(): bool { - if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) { + if ($this->isSoftDeleteEnabled ?? false) { return $this->executeSoftDelete(); } @@ -362,7 +362,7 @@ protected function execute(string $action) * * @return mixed */ - private static function getDataMapperInstance() + protected static function getDataMapperInstance() { $instance = Container::make(get_called_class()); diff --git a/src/Model/AbstractModel.php b/src/Model/AbstractModel.php index 439e8ab1..90aa79d1 100644 --- a/src/Model/AbstractModel.php +++ b/src/Model/AbstractModel.php @@ -140,7 +140,7 @@ public static function firstOrNew($id) /** * Returns a valid instance from Ioc. */ - private static function getBuilderInstance(): Builder + protected static function getBuilderInstance(): Builder { $instance = new static(); @@ -176,7 +176,7 @@ public function update(): bool */ public function delete(): bool { - if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) { + if ($this->isSoftDeleteEnabled ?? false) { return $this->executeSoftDelete(); } diff --git a/src/Model/SoftDeleteTrait.php b/src/Model/SoftDeleteTrait.php new file mode 100644 index 00000000..9779aa02 --- /dev/null +++ b/src/Model/SoftDeleteTrait.php @@ -0,0 +1,84 @@ +{QueryBuilder::getDeletedAtColumn($this)}); + } + + public function restore(): bool + { + $collumn = QueryBuilder::getDeletedAtColumn($this); + + if (!$this->isTrashed()) { + return false; + } + + unset($this->{$collumn}); + + return $this->update(); + } + + public function forceDelete(): bool + { + return $this->execute('delete'); + } + + public function executeSoftDelete(): bool + { + $deletedAtColumn = QueryBuilder::getDeletedAtColumn($this); + $this->$deletedAtColumn = new UTCDateTime(new DateTime('now')); + + return $this->update(); + } + + public static function withTrashed( + mixed $query = [], + array $projection = [], + bool $useCache = false + ): CursorInterface { + return self::performSearch($query, $projection, $useCache); + } + + private static function whereWithMapper(mixed $query, array $projection, bool $useCache) + { + $mapper = self::getDataMapperInstance(); + + $mapper->withTrashed = true; + + return $mapper->where($query, $projection, $useCache); + } + + private static function whereWithBuilder(mixed $query, array $projection, bool $useCache) + { + $mapper = self::getBuilderInstance(); + + $mapper->withTrashed = true; + + return $mapper->where( + new static(), + $query, + $projection, + $useCache + ); + } + + private static function performSearch(mixed $query, array $projection, bool $useCache) + { + $isLegacy = method_exists(self::class, 'getDataMapperInstance'); + + return $isLegacy ? + self::whereWithMapper($query, $projection, $useCache) : + self::whereWithBuilder($query, $projection, $useCache); + } +} diff --git a/src/Model/SoftDeletesTrait.php b/src/Model/SoftDeletesTrait.php deleted file mode 100644 index b0a6d8ae..00000000 --- a/src/Model/SoftDeletesTrait.php +++ /dev/null @@ -1,69 +0,0 @@ -{self::getDeletedAtColumn()}); - } - - public function restore(): bool - { - $collumn = self::getDeletedAtColumn(); - - if (!$this->isTrashed()) { - return false; - } - - unset($this->{$collumn}); - - return $this->execute('save'); - } - - public function forceDelete(): bool - { - $this->forceDelete = true; - - 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 = [], - bool $useCache = false - ): CursorInterface { - $query = QueryBuilder::prepareValueQuery($query); - $query = array_merge($query, [ - 'withTrashed' => true, - ]); - - return parent::where($query, $projection, $useCache); - } - - private static function getDeletedAtColumn(): string - { - return defined( - static::class . '::DELETED_AT' - ) - ? static::DELETED_AT - : 'deleted_at'; - } -} diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 24162b5d..d1f1707b 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -1,10 +1,7 @@ withTrashed ? + QueryBuilder::prepareValueQuery($query) : + QueryBuilder::resolveQuery($query, $model); + return new $cursor( $model->getCollection(), 'find', [ - QueryBuilder::resolveQuery($query, $model), + $query, [ 'projection' => $this->prepareProjection($projection), 'eagerLoads' => $model->with ?? [], diff --git a/src/Util/QueryBuilder.php b/src/Util/QueryBuilder.php index 68729381..efa68471 100644 --- a/src/Util/QueryBuilder.php +++ b/src/Util/QueryBuilder.php @@ -7,7 +7,7 @@ class QueryBuilder { - public static function resolveQuery(int|array|string $query, ModelInterface $model): array + public static function resolveQuery(mixed $query, ModelInterface $model): array { $query = self::prepareValueQuery($query); @@ -28,7 +28,7 @@ public static function getDeletedAtColumn(ModelInterface $model): string * This method will take care of converting a single value into a query for * an _id, including when a objectId is passed as a string. */ - public static function prepareValueQuery(int|array|string $value): array + public static function prepareValueQuery(mixed $value): array { if (!is_array($value)) { $value = ['_id' => $value]; @@ -54,20 +54,18 @@ public static function prepareValueQuery(int|array|string $value): array private static function addSoftDeleteFilterIfRequired(array $query, ModelInterface $model): array { - $field = self::getDeletedAtColumn($model); + if ($model->isSoftDeleteEnabled) { + $field = self::getDeletedAtColumn($model); - if (isset($query['withTrashed'])) { - unset($query['withTrashed']); - - return $query; + return array_merge( + $query, + [ + $field => ['$exists' => false], + ] + ); } - return array_merge( - $query, - [ - $field => ['$exists' => false], - ] - ); + return $query; } /** diff --git a/tests/Integration/PersisteLegacyModelWithSoftDeleteTest.php b/tests/Integration/PersistLegacyModelWithSoftDeleteTest.php similarity index 55% rename from tests/Integration/PersisteLegacyModelWithSoftDeleteTest.php rename to tests/Integration/PersistLegacyModelWithSoftDeleteTest.php index 564705c0..afebadb6 100644 --- a/tests/Integration/PersisteLegacyModelWithSoftDeleteTest.php +++ b/tests/Integration/PersistLegacyModelWithSoftDeleteTest.php @@ -2,39 +2,39 @@ namespace Mongolid\Tests\Integration; -use DateTime; use MongoDB\BSON\ObjectId; use MongoDB\BSON\UTCDateTime; use Mongolid\Model\ModelInterface; use Mongolid\Tests\Stubs\Legacy\ProductWithSoftDelete; use Mongolid\Tests\Stubs\Legacy\Product; -final class PersisteLegacyModelWithSoftDeleteTest extends IntegrationTestCase +final class PersistLegacyModelWithSoftDeleteTest extends IntegrationTestCase { private ObjectId $_id; - public function testShouldFindNotDeletedProduct(): void + public function testShouldFindUndeletedProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $actualWhereResult = ProductWithSoftDelete::where()->first(); - $actualFirstResult = ProductWithSoftDelete::first($this->_id); + $actualFirstResult = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertEquals($product, $actualWhereResult); $this->assertEquals($product, $actualFirstResult); } - public function testCannotFindDeletedProduct(): void + public function testShouldNotFindDeletedProduct(): void { // Set - $this->persiteProduct(true); + $product = $this->persitProductWithSoftDeleteTrait(); + $product->delete(); // Actions $actualWhereResult = ProductWithSoftDelete::where()->first(); - $actualFirstResult = ProductWithSoftDelete::first($this->_id); + $actualFirstResult = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertNull($actualWhereResult); @@ -44,9 +44,9 @@ public function testCannotFindDeletedProduct(): void public function testShouldFindATrashedProduct(): void { // Set - $this->persiteProduct(true); - $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a123'); - $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); + $product->delete(); + $this->persitProductWithSoftDeleteTrait('5bcb310783a7fcdf1bf1a123'); // Actions $result = ProductWithSoftDelete::withTrashed(); @@ -61,43 +61,43 @@ public function testShouldFindATrashedProduct(): void $this->assertNull($resultArray[1]['deleted_at'] ?? null); } - public function testRestoreDeletedProduct(): void + public function testShouldRestoreDeletedProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); $product->delete(); // Actions $isRestored = $product->restore(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertTrue($isRestored); $this->assertEquals($product, $result); } - public function testCannotRestoreAlreadyRestoredProduct(): void + public function testShouldNotRestoreAlreadyRestoredProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $isRestored = $product->restore(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertFalse($isRestored); $this->assertEquals($product, $result); } - public function testExecuteSoftDeleteOnProduct(): void + public function testShouldExecuteSoftDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $isDeleted = $product->delete(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); $deletedProduct = ProductWithSoftDelete::withTrashed()->first(); // Assertions @@ -110,12 +110,13 @@ public function testExecuteSoftDeleteOnProduct(): void ); } - public function testExecuteForceDeleteOnProduct(): void + public function testShouldExecuteForceDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(); - $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a123'); - $product2 = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); + $product2 = $this->persitProductWithSoftDeleteTrait( + '5bcb310783a7fcdf1bf1a123' + ); // Actions $isDeleted = $product->forceDelete(); @@ -127,14 +128,14 @@ public function testExecuteForceDeleteOnProduct(): void $this->assertEquals($product2, $result->first()); } - public function testCannotExecuteSoftDeleteOnProduct(): void + public function testShouldNotExecuteSoftDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(model:Product::class); + $product = $this->persitProduct(); // Actions $isDeleted = $product->delete(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertTrue($isDeleted); @@ -142,28 +143,27 @@ public function testCannotExecuteSoftDeleteOnProduct(): void $this->assertNull($result->deleted_at ?? null); } - protected function setUp(): void - { - parent::setUp(); + private function persitProductWithSoftDeleteTrait( + string $id = '5bcb310783a7fcdf1bf1a672' + ): ModelInterface { + $product = new ProductWithSoftDelete(); + $product->_id = new ObjectId($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)'; + + $product->save(); - $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a672'); + return $product; } - private function persiteProduct( - bool $softDeleted = false, - string $model = ProductWithSoftDelete::class + private function persitProduct( + string $id = '5bcb310783a7fcdf1bf1a672' ): ModelInterface { - $product = new $model(); - $product->_id = $this->_id; + $product = new Product(); + $product->_id = new ObjectId($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; diff --git a/tests/Integration/PersisteModelWithSoftDeleteTest.php b/tests/Integration/PersistModelWithSoftDeleteTest.php similarity index 57% rename from tests/Integration/PersisteModelWithSoftDeleteTest.php rename to tests/Integration/PersistModelWithSoftDeleteTest.php index 113da17f..456670e7 100644 --- a/tests/Integration/PersisteModelWithSoftDeleteTest.php +++ b/tests/Integration/PersistModelWithSoftDeleteTest.php @@ -9,32 +9,33 @@ use Mongolid\Tests\Stubs\ProductWithSoftDelete; use Mongolid\Tests\Stubs\Product; -final class PersisteModelWithSoftDeleteTest extends IntegrationTestCase +final class PersistModelWithSoftDeleteTest extends IntegrationTestCase { private ObjectId $_id; - public function testShouldFindNotDeletedProduct(): void + public function testShouldFindUndeletedProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $actualWhereResult = ProductWithSoftDelete::where()->first(); - $actualFirstResult = ProductWithSoftDelete::first($this->_id); + $actualFirstResult = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertEquals($product, $actualWhereResult); $this->assertEquals($product, $actualFirstResult); } - public function testCannotFindDeletedProduct(): void + public function testShouldNotFindDeletedProduct(): void { // Set - $this->persiteProduct(true); + $product = $this->persitProductWithSoftDeleteTrait(); + $product->delete(); // Actions $actualWhereResult = ProductWithSoftDelete::where()->first(); - $actualFirstResult = ProductWithSoftDelete::first($this->_id); + $actualFirstResult = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertNull($actualWhereResult); @@ -44,9 +45,9 @@ public function testCannotFindDeletedProduct(): void public function testShouldFindATrashedProduct(): void { // Set - $this->persiteProduct(true); - $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a123'); - $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); + $product->delete(); + $this->persitProductWithSoftDeleteTrait('5bcb310783a7fcdf1bf1a123'); // Actions $result = ProductWithSoftDelete::withTrashed(); @@ -61,43 +62,43 @@ public function testShouldFindATrashedProduct(): void $this->assertNull($result->current()->deleted_at ?? null); } - public function testRestoreDeletedProduct(): void + public function testShouldRestoreDeletedProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); $product->delete(); // Actions $isRestored = $product->restore(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertTrue($isRestored); $this->assertEquals($product, $result); } - public function testCannotRestoreAlreadyRestoredProduct(): void + public function testShouldNotRestoreAlreadyRestoredProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $isRestored = $product->restore(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertFalse($isRestored); $this->assertNull($result->deleted_at); } - public function testExecuteSoftDeleteOnProduct(): void + public function testShouldExecuteSoftDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); // Actions $isDeleted = $product->delete(); - $result = ProductWithSoftDelete::first($this->_id); + $result = ProductWithSoftDelete::first('5bcb310783a7fcdf1bf1a672'); // Assertions $this->assertTrue($isDeleted); @@ -108,12 +109,11 @@ public function testExecuteSoftDeleteOnProduct(): void ); } - public function testExecuteForceDeleteOnProduct(): void + public function testShouldExecuteForceDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(); - $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a123'); - $product2 = $this->persiteProduct(); + $product = $this->persitProductWithSoftDeleteTrait(); + $product2 = $this->persitProductWithSoftDeleteTrait('5bcb310783a7fcdf1bf1a123'); // Actions $isDeleted = $product->forceDelete(); @@ -125,10 +125,10 @@ public function testExecuteForceDeleteOnProduct(): void $this->assertEquals($product2, $result->first()); } - public function testCannotExecuteSoftDeleteOnProduct(): void + public function testShouldNotExecuteSoftDeleteOnProduct(): void { // Set - $product = $this->persiteProduct(model:Product::class); + $product = $this->persitProduct(); // Actions $isDeleted = $product->delete(); @@ -147,19 +147,25 @@ protected function setUp(): void $this->_id = new ObjectId('5bcb310783a7fcdf1bf1a672'); } - private function persiteProduct( - bool $softDeleted = false, - string $model = ProductWithSoftDelete::class + private function persitProductWithSoftDeleteTrait( + string $id = '5bcb310783a7fcdf1bf1a672' ): ModelInterface { - $product = new $model(); - $product->_id = $this->_id; + $product = new ProductWithSoftDelete(); + $product->_id = new ObjectId($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; + } + private function persitProduct( + string $id = '5bcb310783a7fcdf1bf1a672' + ): ModelInterface { + $product = new Product(); + $product->_id = new ObjectId($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)'; $product->save(); diff --git a/tests/Stubs/Legacy/ProductWithSoftDelete.php b/tests/Stubs/Legacy/ProductWithSoftDelete.php index 4449134a..19b25c2c 100644 --- a/tests/Stubs/Legacy/ProductWithSoftDelete.php +++ b/tests/Stubs/Legacy/ProductWithSoftDelete.php @@ -2,9 +2,9 @@ namespace Mongolid\Tests\Stubs\Legacy; -use Mongolid\Model\SoftDeletesTrait; +use Mongolid\Model\SoftDeleteTrait; class ProductWithSoftDelete extends Product { - use SoftDeletesTrait; + use SoftDeleteTrait; } diff --git a/tests/Stubs/ProductWithSoftDelete.php b/tests/Stubs/ProductWithSoftDelete.php index ceae0037..8704bc03 100644 --- a/tests/Stubs/ProductWithSoftDelete.php +++ b/tests/Stubs/ProductWithSoftDelete.php @@ -2,9 +2,9 @@ namespace Mongolid\Tests\Stubs; -use Mongolid\Model\SoftDeletesTrait; +use Mongolid\Model\SoftDeleteTrait; class ProductWithSoftDelete extends Product { - use SoftDeletesTrait; + use SoftDeleteTrait; } diff --git a/tests/Unit/DataMapper/DataMapperTest.php b/tests/Unit/DataMapper/DataMapperTest.php index 02715ada..1e947467 100644 --- a/tests/Unit/DataMapper/DataMapperTest.php +++ b/tests/Unit/DataMapper/DataMapperTest.php @@ -693,7 +693,6 @@ public function testShouldGetWithWhereQuery() $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $projection = ['project' => true, '_id' => false]; @@ -751,7 +750,6 @@ public function testShouldGetNullIfFirstCantFindAnything() $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $schema->entityClass = Product::class; @@ -792,7 +790,6 @@ public function testShouldGetFirstProjectingFields() $projection = ['project' => true, 'fields' => false]; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $schema->entityClass = Product::class; diff --git a/tests/Unit/Model/SoftDeletesTraitTest.php b/tests/Unit/Model/SoftDeletesTraitTest.php index 3d1fa9af..424996c9 100644 --- a/tests/Unit/Model/SoftDeletesTraitTest.php +++ b/tests/Unit/Model/SoftDeletesTraitTest.php @@ -72,7 +72,7 @@ public function testShouldRestoreProduct(): void ->setSchema(m::type(DynamicSchema::class)); $dataMapper->expects() - ->save(m::type(Product::class), m::type('array')) + ->update(m::type(Product::class), m::type('array')) ->andReturnTrue(); // Actions @@ -94,7 +94,7 @@ public function testShouldNotRestoreProduct(): void $this->assertFalse($actual); } - public function testShouldforceDeleteProduct(): void + public function testShouldForceDeleteOnProduct(): void { // Set $product = new ProductWithSoftDelete(); @@ -185,7 +185,7 @@ public function testShouldFindWithTrashedProducts(): void ->setSchema(m::type(DynamicSchema::class)); $dataMapper->expects() - ->where(['_id' => '123', 'withTrashed' => true], [], false) + ->where('123', [], false) ->andReturn($cursor); // Actions diff --git a/tests/Unit/Query/BuilderTest.php b/tests/Unit/Query/BuilderTest.php index e3e0adc7..413e995a 100644 --- a/tests/Unit/Query/BuilderTest.php +++ b/tests/Unit/Query/BuilderTest.php @@ -509,7 +509,6 @@ public function testShouldGetWithWhereQuery(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $projection = ['project' => true, '_id' => false, '__pclass' => true]; @@ -544,7 +543,6 @@ public function testShouldGetWithWhereQueryEagerLoadingModels(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $projection = ['project' => true, '_id' => false, '__pclass' => true]; $expectedParams = [ @@ -588,7 +586,6 @@ public function testShouldGetCacheableCursor(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $projection = ['project' => true, '_id' => false, '__pclass' => true]; @@ -635,7 +632,6 @@ public function testShouldGetFirstWithQuery(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $model = new ReplaceCollectionModel(); $model->setCollection($collection); @@ -704,7 +700,6 @@ public function testFirstOrFailShouldGetFirst(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $model = new ReplaceCollectionModel(); $model->setCollection($collection); @@ -777,7 +772,6 @@ public function testShouldGetNullIfFirstCantFindAnything(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $model = new ReplaceCollectionModel(); $model->setCollection($collection); @@ -805,7 +799,6 @@ public function testShouldGetFirstProjectingFields(): void $query = 123; $preparedQuery = [ '_id' => 123, - 'deleted_at' => ['$exists' => false], ]; $projection = ['project' => true, 'fields' => false, '__pclass' => true]; $model = new ReplaceCollectionModel(); diff --git a/tests/Unit/Util/QueryBuilderTest.php b/tests/Unit/Util/QueryBuilderTest.php index 037bc7ef..e09e32c9 100644 --- a/tests/Unit/Util/QueryBuilderTest.php +++ b/tests/Unit/Util/QueryBuilderTest.php @@ -80,11 +80,13 @@ public function queryValueScenarios(): array * @dataProvider getQuery */ public function testShouldResolveQuery( - int|array|string $query, + mixed $query, + bool $isSoftDeleteEnabled, array $expected ): void { // Set $model = m::mock(ModelInterface::class); + $model->isSoftDeleteEnabled = $isSoftDeleteEnabled; // Actions $actual = QueryBuilder::resolveQuery($query, $model); @@ -96,22 +98,32 @@ public function testShouldResolveQuery( public function getQuery(): array { return [ - 'When query is a string' => [ + 'When query is a string and softDelete is enabled' => [ 'query' => '123', + 'isSoftDeleteEnabled' => true, 'expected' => [ '_id' => '123', 'deleted_at' => ['$exists' => false], ], ], - 'When query is a int' => [ + 'When query is a string and softDelete is disabled' => [ + 'query' => '123', + 'isSoftDeleteEnabled' => false, + 'expected' => [ + '_id' => '123', + ], + ], + 'When query is a int and softDelete is enabled' => [ 'query' => 123, + 'isSoftDeleteEnabled' => true, 'expected' => [ '_id' => 123, 'deleted_at' => ['$exists' => false], ], ], - 'When query have withTrashed field' => [ - 'query' => ['_id' => 123,'withTrashed' => true], + 'When query is a int and softDelete is disabled' => [ + 'query' => 123, + 'isSoftDeleteEnabled' => false, 'expected' => [ '_id' => 123, ],