Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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

;extension = pdo_sqlite

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

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.

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

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

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

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"/>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top