laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.32k stars 10.95k forks source link

[9.x] Query exception when assertion fails on assertDatabaseHas/assertDatabaseMissing #43913

Closed bastien-phi closed 2 years ago

bastien-phi commented 2 years ago

Description:

Failing tests with [column, operator, value] syntax fail with QueryException instead of failed assertion.

assertDatabaseHas, assertDatabaseMissing at least are impacted. It looks like it is related to HasInDatabase::getAdditionalInfo

Steps To Reproduce:

it('allows to test database existence with operator', function () {
    User::factory()->createOne(['first_name' => 'Taylor']);

    $this->assertDatabaseMissing(User::class, [
        ['first_name', 'like', 'Tayl%'],
    ]);
});
Illuminate\Database\QueryException : SQLSTATE[42703]: Undefined column: 7 ERROR:  column "0" does not exist
LIGNE 1 : select "0" from "users" where "0" = $1 limit 3
                 ^ (SQL: select "0" from "users" where "0" = first_name limit 3)
 /project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
 /project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:719
 /project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:421
 /project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2630
 /project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2618
 /project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3154
 /project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2619
 /project/vendor/laravel/framework/src/Illuminate/Testing/Constraints/HasInDatabase.php:84
 /project/vendor/laravel/framework/src/Illuminate/Testing/Constraints/HasInDatabase.php:67
 /project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:49
driesvints commented 2 years ago

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

bastien-phi commented 2 years ago

https://github.com/bastien-phi/bug-report

driesvints commented 2 years ago

You're misusing this. You need to pass an array of key/value pairs that correspond to table columns: https://laravel.com/docs/9.x/database-testing#assert-database-has

bastien-phi commented 2 years ago

Not beeing documented doesn't mean it does not work...

As you can see https://github.com/bastien-phi/bug-report/blob/3f66250ab3c48f5a1a1d6416ebdc7dd94067daac/tests/Feature/ExampleTest.php#L25-L32, the syntax is completely valid.

In the other hand, I understand your point of view as this syntax is not yet tested in the framework nor documented...