diff --git a/src/Model/SoftDeletesTrait.php b/src/Model/SoftDeletesTrait.php index 056bcfb8..0defac6b 100644 --- a/src/Model/SoftDeletesTrait.php +++ b/src/Model/SoftDeletesTrait.php @@ -11,7 +11,7 @@ trait SoftDeletesTrait public function isTrashed(): bool { - return !is_null($this->{ self::getDeletedAtColumn()}); + return !is_null($this->{self::getDeletedAtColumn()}); } public function restore(): bool @@ -35,7 +35,7 @@ public function forceDelete(): bool } public static function withTrashed( - array $query = [], + array|string|int $query = [], array $projection = [], bool $useCache = false ): CursorInterface { diff --git a/tests/Integration/PersisteModelWithSoftDeleteTest.php b/tests/Integration/PersisteModelWithSoftDeleteTest.php index 4347d1fd..0df20625 100644 --- a/tests/Integration/PersisteModelWithSoftDeleteTest.php +++ b/tests/Integration/PersisteModelWithSoftDeleteTest.php @@ -13,16 +13,18 @@ final class PersisteModelWithSoftDeleteTest extends IntegrationTestCase { private ObjectId $_id; - public function testFindNotDeletedProduct(): void + public function testShouldFindNotDeletedProduct(): void { // Set $product = $this->persiteProduct(); // Actions - $result = ProductWithSoftDelete::where()->first(); + $actualWhereResult = ProductWithSoftDelete::where()->first(); + $actualFirstResult = ProductWithSoftDelete::first($this->_id); // Assertions - $this->assertEquals($product, $result); + $this->assertEquals($product, $actualWhereResult); + $this->assertEquals($product, $actualFirstResult); } public function testCannotFindDeletedProduct(): void @@ -31,13 +33,15 @@ public function testCannotFindDeletedProduct(): void $this->persiteProduct(true); // Actions - $result = ProductWithSoftDelete::where()->first(); + $actualWhereResult = ProductWithSoftDelete::where()->first(); + $actualFirstResult = ProductWithSoftDelete::first($this->_id); // Assertions - $this->assertNull($result); + $this->assertNull($actualWhereResult); + $this->assertNull($actualFirstResult); } - public function testFindDeletedProductTrashed(): void + public function testShouldFindATrashedProduct(): void { // Set $this->persiteProduct(true); @@ -57,34 +61,11 @@ public function testFindDeletedProductTrashed(): void $this->assertNull($resultArray[1]['deleted_at'] ?? null); } - public function testFindDeletedProductWithFirst(): void - { - // Set - $product = $this->persiteProduct(); - - // Actions - $result = ProductWithSoftDelete::first($this->_id); - - // Assertions - $this->assertEquals($product, $result); - } - - public function testCannotFindDeletedProductWithFirst(): void - { - // Set - $this->persiteProduct(true); - - // Actions - $result = ProductWithSoftDelete::first($this->_id); - - // Assertions - $this->assertNull($result); - } - public function testRestoreDeletedProduct(): void { // Set - $product = $this->persiteProduct(true); + $product = $this->persiteProduct(); + $product->delete(); // Actions $isRestored = $product->restore(); @@ -121,7 +102,10 @@ public function testExecuteSoftDeleteOnProduct(): void // Assertions $this->assertTrue($isDeleted); $this->assertNull($result); - $this->assertInstanceOf(UTCDateTime::class, $product->deleted_at); + $this->assertEquals( + $product, + ProductWithSoftDelete::withTrashed()->first() + ); } public function testExecuteForceDeleteOnProduct(): void @@ -133,7 +117,7 @@ public function testExecuteForceDeleteOnProduct(): void // Actions $isDeleted = $product->forceDelete(); - $result = ProductWithSoftDelete::all(); + $result = ProductWithSoftDelete::withTrashed(); // Assertions $this->assertTrue($isDeleted); diff --git a/tests/Unit/Model/SoftDeletesTraitTest.php b/tests/Unit/Model/SoftDeletesTraitTest.php index 060ff6dc..04456c76 100644 --- a/tests/Unit/Model/SoftDeletesTraitTest.php +++ b/tests/Unit/Model/SoftDeletesTraitTest.php @@ -14,18 +14,45 @@ class SoftDeletesTraitTest extends TestCase { - public function testShouldReturnStatusOfSoftDelete(): void - { + /** + * @dataProvider getSoftDeleteStatus + */ + public function testShouldReturnStatusOfSoftDelete( + ?UTCDateTime $date, + bool $expected, + bool $isFillable = true + ): void { // Set - $date = new UTCDateTime(new DateTime('today')); $product = new ProductWithSoftDelete(); - $product->deleted_at = $date; + + if ($isFillable) { + $product->deleted_at = $date; + } // Actions $actual = $product->isTrashed(); // Assertions - $this->assertTrue($actual); + $this->assertSame($expected, $actual); + } + + public function getSoftDeleteStatus(): array + { + return [ + 'When deleted_at field is filled' => [ + 'deletedAt' => new UTCDateTime(new DateTime('today')), + 'expected' => true, + ], + 'When deleted_at field is null' => [ + 'deletedAt' => null, + 'expected' => false, + ], + 'When there is not an deleted_at field' => [ + 'deletedAt' => null, + 'expected' => false, + 'isFillable ' => false, + ], + ]; } public function testShouldRestoreProduct(): void @@ -34,6 +61,7 @@ public function testShouldRestoreProduct(): void $product = new ProductWithSoftDelete(); $date = new UTCDateTime(new DateTime('today')); $product->deleted_at = $date; + $dataMapper = $this->instance( DataMapper::class, m::mock(DataMapper::class) @@ -107,11 +135,11 @@ public function testShouldFindWithTrashedProducts(): void ->setSchema(m::type(DynamicSchema::class)); $dataMapper->expects() - ->where(['withTrashed' => true], [], false) + ->where(['_id' => '123', 'withTrashed' => true], [], false) ->andReturn($cursor); // Actions - $actual = $product->withTrashed(); + $actual = $product->withTrashed('123'); // Assertions $this->assertInstanceOf(CursorInterface::class, $actual);