Laravel 正在测试 - 该名称已被使用

Laravel Testing - the name is already in use

我在使用 phpunit 时遇到问题,我不知道为什么我在文件没问题的情况下遇到这种错误, 我的理论是,当测试是 运行 时,它将重新包含名称文件,而不是检查文件是否已经导入,因此不应重新发布。

我尝试在 CreateApplications 上执行 require_once 但它会导致错误,

由于错误

,我无法在一个 class 上测试所有测试用例

Cannot declare class <class omitted>, because the name is already in use in <file omitted>

<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require_once(__DIR__.'/../bootstrap/app.php');

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

我的问题是,如何修复此错误或如何不重新导入 class?如何预防?

截至目前,我在 PHPUnit 上使用 --filter 选项以便在测试中调用特定方法 Class

更新

有一个自定义包,它使用 include,这会导致错误, 在我的例子中,它位于自定义包服务提供商的启动方法

<?php

namespace CustomPackage;

use Illuminate\Support\ServiceProvider;

class CustomPackageProvider extends ServiceProvider{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(){
        ...
        include(__DIR__.'/Nem/NemPhp.php');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register(){

    }
}

所以将 include 更改为 require_once 解决了问题 报告中的错误不是很清楚,这就是为什么我很难质疑理论化的原因,因为一个错误 :)