25

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

9 Answers 9

23

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.

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

2 Comments

Ok that seem to be a problem because I really don't have SQLite PDO driver installed. I'll test things out later.
I finaly got home and tested things out. It seems like I don't read all the error messages. Thank you for your help.
23

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

1 Comment

I have php 7.1 what should be expected?
8

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

;extension = pdo_sqlite

2 Comments

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

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

Comments

3

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.

Comments

2

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

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

3 Comments

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'
try sudo apt-get install php7.1-sqlite3
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)
2

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

Comments

0

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

Comments

0

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

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.