Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Extract customised deleted_at column name from Model FQN #47873

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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