laravel PDOException: 使用 phpunit 时找不到驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23245800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:24:29  来源:igfitidea点击:

PDOException: could not find driver when using phpunit

phpmysqllaravelpdophpunit

提问by melanholly

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.

我开始使用 laravel 4 并且我正在尝试开始使用单元测试,这样我就可以让我的生活更轻松。在尝试 phpunit 测试后,你们所有人都会猜到我的开发并没有变得更容易。简单的测试很好,很容易,但是当事情开始变得有点复杂时,它们不会像我想的那样进行。

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-mysqland when I call php -mit says that I have both PDO and pdo_mysql. The actual command I use is php -m |grep -i "pdo"and the output is:

问题是我进行了简单的测试,但出现了一些奇怪的错误PDOException: could not find driver。我已经阅读了一些关于这个主题的文章和帖子,但没有解决我的问题。我已经安装php5-mysql,当我调用php -m它时说我有 PDO 和 pdo_mysql。我使用的实际命令是php -m |grep -i "pdo",输出是:

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 consolethe result is not the same.

好吧,我已经尝试在浏览器中实际测试 PDOException 类。为此,我已将 mysql 用户密码更改为不正确的密码,并测试了artisan 服务器中会发生的情况(使用命令php artisan serve--->调用http://localhost:8000/)。在浏览器中,一切都像魅力一样,但是当我尝试在控制台中调用“phpunit”时,结果不一样。

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:

我试图查看 webserver 和 cli 是否有不同的配置文件,但结果是这些文件是相同的。我比较过的配置文件是:

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.

为了比较它们,我像这样使用 diff 命令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 :)

感谢您的时间 :)

回答by common sense

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

Laravel 似乎使用 SQLite 作为测试数据库。查看第 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.

但这似乎未安装在您的系统上。所以我认为你需要安装 SQLite 驱动程序。

回答by Web Developer in Pune

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

如果您使用 sqlite 进行测试,则需要 php sqlite 驱动程序

For Ubuntu 14.04

对于 Ubuntu 14.04

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

In ubuntu 16.04 there is no php5-sqlite

在 ubuntu 16.04 中没有 php5-sqlite

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

回答by Fabian Picone

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

在 Windows 上,我必须extension=php_pdo_sqlite.dll在 php.ini 中激活。

回答by Mau Web

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

如果你正在占用 Sqlite,你必须输入 php.init 并取消注释这一行。

;extension = pdo_sqlite

回答by camelCase

For those using Laravel Homestead, make sure you're running phpunitfrom withinHomestead and not on your local machine! You can SSH into itwith vagrant ssh.

对于那些使用Laravel Homestead 的人,请确保您Homestead运行phpunit,而不是在本地机器上运行!你可以通过SSH连接到它与。vagrant ssh

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

正如其他人提到的,它需要 SQLite,因此在您的虚拟机中运行它可确保您的测试可用。

回答by Adam Winnipass

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

php7.0-sqlite 不再有效使用 php7.1-sqlite

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