laravel 和 phpunit:找不到驱动程序(SQL:PRAGMA foreign_keys = ON;)

laravel and phpunit: could not find driver (SQL: PRAGMA foreign_keys = ON;)

我有 运行 我的 laravel 应用程序和 phpunit。 一切都很好,直到我 运行 我的测试再次出现此错误。

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

Caused by
PDOException: could not find driver

这是我的 phpunit.xml 文件:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">
<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1

有人在 laravel 6 和 phpunit 中遇到过这个错误吗?

这是我在 laragon 中升级我的 php 版本后实际发生的。解决方案是在 laragon 中启用 pdo_sqlite on php 扩展。而已。这个答案在这里 https://laracasts.com/discuss/channels/general-discussion/phpunit-sqlite-error-after-updating-laragon-pdoexception-could-not-find-driver

如果您在 Laragon 中有多个 PHP 版本,您仍然会看到错误。 解决方案是在所有版本上启用 pdo_sqlite 扩展。 原因是 Laragon 有自己的 PHP 但 Laravel 使用系统 PHP 你可能不知道哪个是系统 PHP 版本。

只需对 php.ini 文件进行以下更改

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"

发生这种情况是因为未启用 PDO Sqlite。 要在 Laragon 中启用它,只需打开 Laragon 点击​​菜单 > PHP > 扩展 > pdo_sqlite 就是这样。

如果您不使用 Laragon,解决方案可能会有所不同。谢谢

如果你遇到这个问题

(could not find driver (SQL: PRAGMA foreign_keys = ON;))

您可以简单地 运行 在您的 Ubuntu 系统上使用以下命令

sudo apt-get install php-sqlite3

我想它会对你有所帮助..谢谢

对于那些使用 windows;

导航到 php.ini 文件(-C:\php\php.ini)

启用以下功能:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol

PHP 8.1

sudo apt install php8.1-sqlite3

注意需要8.1部分!

仅从 php.ini 文件中启用这两个扩展对我有用。

extension=pdo_sqlite

extension=sqlite3