5

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.

3
  • 1
    Do you have an environment file specifically for testing, or are there some configuration options for the database in phpunit.xml php section? If yes, these settings could be trying to use a database connection you do not have set up. Commented Feb 5, 2020 at 15:41
  • That solved it many thanks! Commented Feb 6, 2020 at 17:06
  • 1
    No problem. I added an answer with my comment contents for future question visitors. Feel free to accept. Commented Feb 6, 2020 at 17:45

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.