Bruno Albuquerque
Bruno Albuquerque

Reputation: 687

Laravel 6 PHPUnit Testing - could not find driver (SQL: PRAGMA foreign_keys = ON;)

I'm started to write tests in Laravel. My application works, I can login, run migrations, but when I'm trying to test the login, I'm getting the following error:

could not find driver (SQL: PRAGMA foreign_keys = ON;)

My DB is in Postgres, and my test is as follows:

/** @test */
public function user_login_ok()
{
    $response = $this->json('POST', '/api/login', [
       'email' => '[email protected]',
       'password' => 'test'
    ]);

    $this->assertEquals(200, $response->getStatusCode());
}

I'm not worried (for now) if my test is good enough or even right, but to solve this error.

Upvotes: 5

Views: 5996

Answers (3)

Andre Toledo Gama
Andre Toledo Gama

Reputation: 152

In your phpunit.xml file (probably in the project root) you will see the following settings at the end

<php>
  <server name="APP_ENV" value="testing"/>
  <server name="BCRYPT_ROUNDS" value="4"/>
  <server name="CACHE_DRIVER" value="array"/>
  <server name="DB_CONNECTION" value="mysql"/>
  <server name="DB_DATABASE" value=":memory:"/>
  <server name="MAIL_MAILER" value="array"/>
  <server name="QUEUE_CONNECTION" value="sync"/>
  <server name="SESSION_DRIVER" value="array"/>
  <server name="TELESCOPE_ENABLED" value="false"/>
</php>

Change the line

<server name="DB_CONNECTION" value="sqlite"/>

For the type of database you are using, in my case it is mysql

<server name="DB_CONNECTION" value="mysql"/>

To be sure of the type of database you are using, look at your .env

DB_CONNECTION=mysql

Upvotes: 0

Alaa mohammed
Alaa mohammed

Reputation: 161

You may need to enable extension for pdo_sqlite in your php.ini file that is being used by phpunit.

Upvotes: 10

Bryan
Bryan

Reputation: 3494

Do you have a testing environment specific .env file? Do you have php section environment configuration in your phpunit.xml file?

If either of these has configuration for your database connection, they should be reviewed to make sure they are configured correctly.

Upvotes: 3

Related Questions