You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

php artisan test无法切换数据库连接至mysql_test的问题求助

php artisan test无法切换数据库连接至mysql_test的问题求助

大家好,我碰到一个挺头疼的问题:在PhpStorm里运行测试的时候,一切都正常,数据库连接会自动切换到mysql_test;但我手动在终端执行php artisan test命令时,数据库连接根本不会切换,一直用的是默认的mysql连接。

下面是我的phpunit.xml配置文件内容:

<?xml version="1.0" encoding="UTF-8"?>

<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">./Modules/**/Tests/Unit</directory>

</testsuite>

<testsuite name="Feature">

<directory suffix="Test.php">./Modules/**/Tests/Feature</directory>

</testsuite>

</testsuites>

<php>

<env name="APP_ENV" value="testing"/>

<env name="BCRYPT_ROUNDS" value="4"/>

<env name="CACHE_DRIVER" value="array"/>

<env name="DB_CONNECTION" value="mysql_test"/>

<env name="DB_CONNECTION" value="mysql_test"/>

<env name="MAIL_MAILER" value="array"/>

<env name="QUEUE_DRIVER" value="sync"/>

<env name="QUEUE_CONNECTION" value="sync"/>

<env name="SESSION_DRIVER" value="array"/>

<env name="TELESCOPE_ENABLED" value="false"/>

</php>

<source>

<include>

<directory suffix=".php">./app</directory>

<directory suffix=".php">./Modules</directory>

</include>

</source>

</phpunit>

这是其中一个测试文件的代码:

class BannerTest extends TestCase
{

use WithFaker;

private static $authUser;

public function setUp(): void
{
// first include all the normal setUp operations
parent::setUp();

self::$authUser = Operator::factory()->create();

$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();
}

public function giveBannerPermission()
{
$operator = self::$authUser;
$this->be(self::$authUser);
$operator->assignRole('admin');
$role = Role::findByName('admin');
$role->givePermissionTo('bannerAdd');
}

public function banner_requires_status()
{
$this->giveBannerPermission();
$banner = Banner::factory()->raw(['title' => NULL]);
$this->post(route('banner.store'), $banner)->assertSessionHasErrors('title');
}

}

我已经试过在运行测试前执行php artisan optimize:clear和php artisan cache:clear,但问题还是没解决,有没有朋友遇到过类似的情况,能给点思路吗?

备注:内容来源于stack exchange,提问作者arya_la

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 11:28:04