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

Viewed 14162

I have run my laravel app with phpunit. Everything is fine until at some point when I run my test again turns out with this error.

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

enter image description here

Caused by
PDOException: could not find driver

Here's my phpunit.xml file:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">
<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <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>
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1

Does anyone encounter with this error in laravel 6 and phpunit?

8 Answers

If You face this problem

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

You can simply run bellow command on your Ubuntu system

sudo apt-get install php-sqlite3

You might need to enable pdo_sqlite extension in your php.ini too, you can get the path of your loaded ini with the below command:

php --ini

simply add extension=pdo_sqlite to your php.ini and verify it is loaded by running

php -m | grep pdo_sqlite

I think it will help you.. Thanks

PHP 8.1

sudo apt install php8.1-sqlite3

Note that it needs the 8.1 part!

This happens because PDO Sqlite is not enabled. to enable it in Laragon, just open Laragon click on menu > PHP > Extensions > pdo_sqlite That's it.

And if you don't use Laragon the solution might be different. Thanks

Just do the following changes on the php.ini file

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"

In case if you have multiple versions of PHP in Laragon you will still see the error. The solution is to enable pdo_sqlite extension on all versions. The reason is that Laragon has its own PHP but Laravel uses the system PHP and you might not know which one is system PHP version.

For those using windows;

Navigate to the php.ini file(-C:\php\php.ini)

Enable the following:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol

just enabling these two extension from php.ini file worked for me.

extension=pdo_sqlite

extension=sqlite3
Related