Skip to content

Commit

Permalink
[10.x] Extract customised deleted_at column name from Model FQN (#47873)
Browse files Browse the repository at this point in the history
* #47856 attempt to extract customized deleted_at column name when Model FCN is passed for table argument

* fix issue identified by styleci

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
edvordo and taylorotwell committed Jul 28, 2023
1 parent 03564b1 commit 86d06a9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Expand Up @@ -106,7 +106,12 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul
}

$this->assertThat(
$this->getTable($table), new SoftDeletedInDatabase($this->getConnection($connection, $table), $data, $deletedAtColumn)
$this->getTable($table),
new SoftDeletedInDatabase(
$this->getConnection($connection, $table),
$data,
$this->getDeletedAtColumn($table, $deletedAtColumn)
)
);

return $this;
Expand All @@ -133,7 +138,12 @@ protected function assertNotSoftDeleted($table, array $data = [], $connection =
}

$this->assertThat(
$this->getTable($table), new NotSoftDeletedInDatabase($this->getConnection($connection, $table), $data, $deletedAtColumn)
$this->getTable($table),
new NotSoftDeletedInDatabase(
$this->getConnection($connection, $table),
$data,
$this->getDeletedAtColumn($table, $deletedAtColumn)
)
);

return $this;
Expand Down Expand Up @@ -270,6 +280,18 @@ protected function getTableConnection($table)
return $this->newModelFor($table)?->getConnectionName();
}

/**
* Get the table column name used for soft deletes.
*
* @param string $table
* @param string $defaultColumnName
* @return string
*/
protected function getDeletedAtColumn($table, $defaultColumnName = 'deleted_at')
{
return $this->newModelFor($table)?->getDeletedAtColumn() ?: $defaultColumnName;
}

/**
* Get the model entity from the given model or string.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Expand Up @@ -238,6 +238,21 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR
$this->assertSoftDeleted($model, ['name' => 'Tailwind']);
}

public function testAssertSoftDeletedInDatabaseDoesNotFindModePassedViaFcnWithCustomColumnResults()
{
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The table is empty.');

$model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
$this->data = ['id' => 1];

$builder = $this->mockCountBuilder(0, 'trashed_at');

$builder->shouldReceive('get')->andReturn(collect());

$this->assertSoftDeleted(CustomProductStub::class, ['id' => $model->id]);
}

public function testAssertNotSoftDeletedInDatabaseFindsResults()
{
$this->mockCountBuilder(1);
Expand Down Expand Up @@ -305,6 +320,21 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelWithCustomColu
$this->assertNotSoftDeleted($model, ['name' => 'Tailwind']);
}

public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelPassedViaFcnWithCustomColumnResults()
{
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The table is empty.');

$model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
$this->data = ['id' => 1];

$builder = $this->mockCountBuilder(0, 'trashed_at');

$builder->shouldReceive('get')->andReturn(collect());

$this->assertNotSoftDeleted(CustomProductStub::class, ['id' => $model->id]);
}

public function testAssertExistsPassesWhenFindsResults()
{
$this->data = ['id' => 1];
Expand All @@ -323,6 +353,12 @@ public function testGetTableNameFromModel()
$this->assertEquals($this->table, $this->getTable($this->table));
}

public function testGetTableCustomizedDeletedAtColumnName()
{
$this->assertEquals('trashed_at', $this->getDeletedAtColumn(CustomProductStub::class));
$this->assertEquals('trashed_at', $this->getDeletedAtColumn(new CustomProductStub()));
}

public function testExpectsDatabaseQueryCount()
{
$case = new class('foo') extends TestingTestCase
Expand Down

0 comments on commit 86d06a9

Please sign in to comment.