Skip to content

[12.x] Iterable models assertion #56403

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

Merged
merged 2 commits into from
Jul 24, 2025
Merged
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 @@ -18,13 +18,19 @@ trait InteractsWithDatabase
/**
* Assert that a given where condition exists in the database.
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param array<string, mixed> $data
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseHas($table, array $data = [], $connection = null)
{
if (is_iterable($table)) {
foreach ($table as $item) {
$this->assertDatabaseHas($item, $data, $connection);
}
}

if ($table instanceof Model) {
$data = [
$table->getKeyName() => $table->getKey(),
Expand All @@ -42,13 +48,19 @@ protected function assertDatabaseHas($table, array $data = [], $connection = nul
/**
* Assert that a given where condition does not exist in the database.
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param array<string, mixed> $data
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseMissing($table, array $data = [], $connection = null)
{
if (is_iterable($table)) {
foreach ($table as $item) {
$this->assertDatabaseMissing($item, $data, $connection);
}
}

if ($table instanceof Model) {
$data = [
$table->getKeyName() => $table->getKey(),
Expand Down Expand Up @@ -101,14 +113,20 @@ protected function assertDatabaseEmpty($table, $connection = null)
/**
* Assert the given record has been "soft deleted".
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param array<string, mixed> $data
* @param string|null $connection
* @param string|null $deletedAtColumn
* @return $this
*/
protected function assertSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at')
{
if (is_iterable($table)) {
foreach ($table as $item) {
$this->assertSoftDeleted($item, $data, $connection);
}
}

if ($this->isSoftDeletableModel($table)) {
return $this->assertSoftDeleted(
$table->getTable(),
Expand All @@ -133,14 +151,20 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul
/**
* Assert the given record has not been "soft deleted".
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $table
* @param array<string, mixed> $data
* @param string|null $connection
* @param string|null $deletedAtColumn
* @return $this
*/
protected function assertNotSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at')
{
if (is_iterable($table)) {
foreach ($table as $item) {
$this->assertNotSoftDeleted($item, $data, $connection);
}
}

if ($this->isSoftDeletableModel($table)) {
return $this->assertNotSoftDeleted(
$table->getTable(),
Expand All @@ -165,7 +189,7 @@ protected function assertNotSoftDeleted($table, array $data = [], $connection =
/**
* Assert the given model exists in the database.
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $model
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $model
* @return $this
*/
protected function assertModelExists($model)
Expand All @@ -176,7 +200,7 @@ protected function assertModelExists($model)
/**
* Assert the given model does not exist in the database.
*
* @param \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $model
* @param \Illuminate\Database\Eloquent\Model[]|\Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>|string $model
* @return $this
*/
protected function assertModelMissing($model)
Expand Down