Database Expectations

toBeInDatabase()

Assert that the given where condition exists in the database.

expect(['name' => 'Fabio'])->toBeInDatabase(table: 'users');

toMatchQuery()

Assert that sql query/bindings match with the one generated by the ones generated by the given query builder

$query = User::query()->where('name' => 'Luke')->where('email' => 'luke@pest.dev');

expect($query)->toMatchQuery(
    'select * from "users" where "name" = ? and "email" = ?', //sql query
    ['Luke', 'luke@pest.dev'],                                //bindings
);

if needed, a third param allows to do a partial match

$query = User::query()
    ->where('name' => 'Luke')
    ->where('email' => 'luke@pest.dev')
    ->where('role' => 'developer');

expect($query)->toMatchQuery(
    'and "email" = ?',
    ['luke@pest.dev'],
    false,
);
Edit this page on GitHub Updated at Wed, Feb 8, 2023