Skip to content

Commit

Permalink
feat: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 17, 2023
1 parent 3ddec4a commit 04cba80
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Model/SoftDeletesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +35,7 @@ public function forceDelete(): bool
}

public static function withTrashed(
array $query = [],
array|string|int $query = [],
array $projection = [],
bool $useCache = false
): CursorInterface {
Expand Down
50 changes: 17 additions & 33 deletions tests/Integration/PersisteModelWithSoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -133,7 +117,7 @@ public function testExecuteForceDeleteOnProduct(): void

// Actions
$isDeleted = $product->forceDelete();
$result = ProductWithSoftDelete::all();
$result = ProductWithSoftDelete::withTrashed();

// Assertions
$this->assertTrue($isDeleted);
Expand Down
42 changes: 35 additions & 7 deletions tests/Unit/Model/SoftDeletesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 04cba80

Please sign in to comment.