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

Vic*_*dam 26 phpunit laravel laragon

我已经使用 phpunit 运行了我的 laravel 应用程序。一切都很好,直到在某个时候我再次运行测试时出现此错误。

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Caused by
PDOException: could not find driver
Run Code Online (Sandbox Code Playgroud)

这是我的 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>
Run Code Online (Sandbox Code Playgroud)
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1
Run Code Online (Sandbox Code Playgroud)

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

Eah*_*iya 67

如果您遇到这个问题

(找不到驱动程序(SQL:PRAGMAforeign_keys = ON;))

您只需在 Ubuntu 系统上运行以下命令即可

sudo apt-get install php-sqlite3
Run Code Online (Sandbox Code Playgroud)

另外,如果你想安装特定版本,例如 php 8.1,只需运行以下命令

sudo apt install php8.1-sqlite3
Run Code Online (Sandbox Code Playgroud)

您可能还需要在 php.ini 中启用 pdo_sqlite 扩展,您可以使用以下命令获取加载的 ini 的路径:

php --ini
Run Code Online (Sandbox Code Playgroud)

只需将 extension=pdo_sqlite 添加到您的 php.ini 并通过运行验证它是否已加载

php -m | grep pdo_sqlite
Run Code Online (Sandbox Code Playgroud)

我想这会对你有帮助..谢谢


Vic*_*dam 25

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


Sli*_*liq 10

PHP 8.1

sudo apt install php8.1-sqlite3

请注意,它需要 8.1 部分!


Has*_*man 5

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

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