melanholly
melanholly

Reputation: 772

PDOException: could not find driver when using phpunit

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 :)

Upvotes: 25

Views: 20332

Answers (9)

Lincoln Biancardi
Lincoln Biancardi

Reputation: 1

If using mysql, add this line in phpunit.xml:

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

Then install the mysql package in php

$ apt-get install php-mysql

Try again :)

Upvotes: 0

Mahsa
Mahsa

Reputation: 742

In Laravel, in phpunit.xml file, just set "DB_CONNECTION" and "DB_DATABASE" values. Since I was using mysql (not sqlite), I changed them like this:

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

Upvotes: 0

Mahdi mehrabi
Mahdi mehrabi

Reputation: 1744

first, install SQLite for PHP with sudo apt-get install php-sqlite3

then check your php.ini and make sure the below line is uncommented

;extension = pdo_sqlite

then if you are using apache

sudo service apache2 restart

or if you are using Nginx

sudo service nginx restart


note: if you don't know where is your php.ini path use php -i |grep php.ini command

Upvotes: 2

Mau Web
Mau Web

Reputation: 97

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

;extension = pdo_sqlite

Upvotes: 8

Winnipass
Winnipass

Reputation: 950

php7.0-sqlite no longer works use php7.1-sqlite

sudo apt-get install php7.1-sqlite3
sudo service apache2 restart

Upvotes: 2

camelCase
camelCase

Reputation: 5598

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.

Upvotes: 3

fabpico
fabpico

Reputation: 2927

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

Upvotes: 4

Ganesh Bhosale
Ganesh Bhosale

Reputation: 2100

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

Upvotes: 23

common sense
common sense

Reputation: 3912

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.

Upvotes: 23

Related Questions