Laravel 5.5:PHPUnit(带覆盖)不喜欢来自多个文件的路由并抛出“尚未设置外观根”。没有报道它是绿色的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel 5.5:PHPUnit(带覆盖)不喜欢来自多个文件的路由并抛出“尚未设置外观根”。没有报道它是绿色的相关的知识,希望对你有一定的参考价值。

我有Laravel 5.5,我决定将文件中的路径分组,以更有意义的方式组织它们。

这是一个简化的示例 - Web路径文件存在于:

app/Http/Routes/Web/static.php
app/Http/Routes/Web/test.php

static.php包含:

<?php
declare(strict_types=1);

namespace FooHttpRoutesWeb;

use IlluminateSupportFacadesRoute;

Route::get('/', function () {
    return view('welcome');
});

test.php包含:

<?php
declare(strict_types=1);

namespace FooHttpRoutesWeb;

use IlluminateSupportFacadesRoute;

Route::get('/test', function () {
    return 'test'; // just to simplify
});

RouteServiceProvider.php包含:

<?php
declare(strict_types=1);

namespace FooAppProviders;

use IlluminateFoundationSupportProvidersRouteServiceProvider as ServiceProvider;
use IlluminateSupportFacadesRoute;

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'FooHttpControllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapWebRoutes();
    }

    protected function mapWebRoutes()
    {
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function($router) {
            require app_path('Http/Routes/Web/static.php');
            require app_path('Http/Routes/Web/test.php');
            // more files will land here in the future
        });
    }
}

到目前为止,我可以通过调用php artisan route:list确认一切正常:

enter image description here

现在我打算写一些测试,但我需要代码覆盖,所以我补充说:

<logging>
    <log type="coverage-html" target="./report" charset="UTF-8"
         yui="true" highlight="true"
         lowUpperBound="50" highLowerBound="80"/>
</logging>

进入我的phpunit.xml

当我打电话给phpunit时,我得到:

PHPUnit 7.0.1 by Sebastian Bergmann and contributors.

PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in /Users/slick/Code/foo/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218
Stack trace:
#0 /Users/slick/Code/foo/app/Http/Routes/Web/static.php(10): IlluminateSupportFacadesFacade::__callStatic('get', Array)
#1 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/php-code-coverage/CodeCoverage.php(929): include_once('/Users/slick/Co...')
#2 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/php-code-coverage/CodeCoverage.php(243): SebastianBergmannCodeCoverageCodeCoverage->initializeData()
#3 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/phpunit/Framework/TestResult.php(671): SebastianBergmannCodeCoverageCodeCoverage->start(Object(TestsFeatureExampleTest))
#4 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/phpunit/Framework/TestCase.php(687): PHPUnitFrameworkTestResult->run(Object(TestsFeatureExampleTest))
#5 phar:///usr/local/Cell in /Users/slick/Code/foo/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 218

Fatal error: Uncaught RuntimeException: A facade root has not been set. in /Users/slick/Code/foo/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 218

enter image description here

直接删除覆盖线后,我添加到xml文件并再次运行phpunit - 它是绿色的。

由Sebastian Bergmann和贡献者提供的$ phpunit PHPUnit 7.0.1。

.. 2 / 2 (100%)

时间:381毫秒,内存:20.00MB

好的(2个测试,2个断言)

怎么了?为什么phpunit代码覆盖不喜欢多个文件中的路由(但没有覆盖它完全正常)?

答案

Someone had the same issue并通过从代码覆盖范围中排除路径目录来修复它。所以我加入了phpunit.xml

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
        <exclude>
            <directory suffix=".php">./app/Http/Routes</directory>
        </exclude>
    </whitelist>
</filter>

而覆盖范围的phpunit效果很好。

Goshh ...外墙很棘手。

以上是关于Laravel 5.5:PHPUnit(带覆盖)不喜欢来自多个文件的路由并抛出“尚未设置外观根”。没有报道它是绿色的的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.5 PHPunit 测试 - “尚未设置外观根。”

phpunit 测试资源 laravel 5.5 返回集合而不是 json

如何在 laravel 5.5 中使用 phpunit 测试中间件?

Laravel 5:PHPUnit 和没有可用的代码覆盖驱动程序

在 laravel 4.2 项目上运行具有代码覆盖率的 phpunit 时的空目录

在 Laravel 5.5 中测试授权策略时遇到问题