PDOException: could not find driver when using phpunit

16,418

Solution 1

It seems Laravel using SQLite as database for testing. See the backtrace at line 2:

/var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Connectors/SQLiteConnector.php:22

But this seems not installed on your system. So I think you need to install the SQLite driver.

Solution 2

If you are using sqlite for testing you will need php sqlite drivers

For Ubuntu 14.04

sudo apt-get install php5-sqlite
sudo service apache2 restart

In ubuntu 16.04 there is no php5-sqlite

sudo apt-get install php7.0-sqlite
sudo service apache2 restart

Solution 3

If you are occupying SqLite you have to enter php.init and uncomment this line.

;extension = pdo_sqlite

Solution 4

On Windows i had to activate extension=php_pdo_sqlite.dll in php.ini.

Solution 5

For those using Laravel Homestead, make sure you're running phpunit from within Homestead and not on your local machine! You can SSH into it with vagrant ssh.

As others have mentioned, it requires SQLite and thus running it within your virtual machine ensures that's available to your test.

Share:
16,418

Related videos on Youtube

melanholly
Author by

melanholly

Updated on September 15, 2022

Comments

  • melanholly
    melanholly over 1 year

    I am starting to use laravel 4 and I am trying to start using unit tests so I can make my live easier. Well as all of you will guess my development hasn't become easier after trying phpunit tests. The simple tests are well, easy but when the things start to get a bit more complicated they does not go as I though they will.

    The problem is I have conducted simple tests but I get some strange error PDOException: could not find driver. I have read a few articles and post on this topic but nothing solved my problem. I have installed php5-mysql and when I call php -m it says that I have both PDO and pdo_mysql. The actual command I use is php -m |grep -i "pdo" and the output is:

    PDO
    pdo_mysql
    

    Well I've tried to actualy test PDOException class in the browser. For that purpose I have change the mysql user password to incorrect one and tested what will happen in artisan server (called with command php artisan serve ---> http://localhost:8000/). In the browser everything works as a charm but when I try to call ``phpunit` in the console the result is not the same.

    I have tried to see if webserver and cli have different configuration files but it turnout that the files are identical. The configuration files that I have compared are:

    for the web server

    /etc/php5/apache2/conf.d/20-pdo_mysql.ini
    /etc/php5/apache2/conf.d/05-opcache.ini
    /etc/php5/apache2/conf.d/20-json.ini
    /etc/php5/apache2/conf.d/20-mysql.ini
    /etc/php5/apache2/conf.d/20-mysqli.ini
    /etc/php5/apache2/conf.d/10-pdo.ini
    /etc/php5/apache2/conf.d/20-mcrypt.ini
    /etc/php5/apache2/conf.d/20-curl.ini
    /etc/php5/apache2/php.ini
    

    for command line

    /etc/php5/cli/conf.d/20-pdo_mysql.ini
    /etc/php5/cli/conf.d/05-opcache.ini
    /etc/php5/cli/conf.d/20-json.ini
    /etc/php5/cli/conf.d/20-mysql.ini
    /etc/php5/cli/conf.d/20-mysqli.ini
    /etc/php5/cli/conf.d/10-pdo.ini
    /etc/php5/cli/conf.d/20-mcrypt.ini
    /etc/php5/cli/conf.d/20-curl.ini
    /etc/php5/cli/php.ini
    

    to compare them I user the diff command like so diff -s /path/to/file1 /path/to/file2.

    The errors look like this:

    1) ExampleTest::testBasicExample
    PDOException: could not find driver
    
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Connectors/SQLiteConnector.php:22
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:59
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:47
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:127
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:63
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:167
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:135
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:366
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:93
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:56
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Console/Command.php:108
    /var/www/smlsspd/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:241
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Console/Command.php:96
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Console/Application.php:96
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:57
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:208
    /var/www/smlsspd/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:208
    /var/www/smlsspd/app/tests/TestCase.php:70
    /var/www/smlsspd/app/tests/TestCase.php:70
    /var/www/smlsspd/app/tests/TestCase.php:46
    phar:///var/www/smlsspd/phpunit.phar/phpunit/TextUI/Command.php:179
    phar:///var/www/smlsspd/phpunit.phar/phpunit/TextUI/Command.php:132
    

    Can you give me a hint or solution to this problem?

    Thank you for your time :)

  • melanholly
    melanholly almost 10 years
    Ok that seem to be a problem because I really don't have SQLite PDO driver installed. I'll test things out later.
  • melanholly
    melanholly almost 10 years
    I finaly got home and tested things out. It seems like I don't read all the error messages. Thank you for your help.
  • mikhail-t
    mikhail-t about 6 years
    I've run sudo apt-get update then sudo apt-get install php7.1-sqlite but got those errors: E: Unable to locate package php7.1-sqlite E: Couldn't find any package by glob 'php7.1-sqlite' E: Couldn't find any package by regex 'php7.1-sqlite'
  • Winnipass
    Winnipass about 6 years
    try sudo apt-get install php7.1-sqlite3
  • mikhail-t
    mikhail-t about 6 years
    that sneaky 3 I missed, in any case I did apt-cache search php7.1-sqlite3 and got no search results, and same for php7.1-sqlite only php7.0-sqlite returns php7.0-sqlite3 - SQLite3 module for PHP, I may be missing required ppa (I use Ubuntu subsystem on Windows)
  • Tajuddin Khandaker
    Tajuddin Khandaker over 4 years
    I did but still showing "$ php artisan migrate --env=testing PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20160303/php_pdo_sqlite.dll' - /usr/lib/php/20160303/php_pdo_sqlite.dll: cannot open shared object file: No such file or directory in Unknown on line 0". My OS is Ubuntu 14.04 LTS
  • Tajuddin Khandaker
    Tajuddin Khandaker over 4 years
    I have php 7.1 what should be expected?
  • Mau Web
    Mau Web over 4 years
    Apart uncomment this line in php.unit extension = pdo_sqlite In your phpunit.xml file you have to have these two lines <server name = "DB_CONNECTION" value = "sqlite" /> <server name = "DB_DATABASE" value = ": memory:" />