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

Mat*_*cki 3 php phpunit laravel php-7 laravel-5.5

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

这是一个简化的示例 - Web 路由文件位于:

app/Http/Routes/Web/static.php
app/Http/Routes/Web/test.php
Run Code Online (Sandbox Code Playgroud)

static.php包含:

<?php
declare(strict_types=1);

namespace Foo\Http\Routes\Web;

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});
Run Code Online (Sandbox Code Playgroud)

test.php包含:

<?php
declare(strict_types=1);

namespace Foo\Http\Routes\Web;

use Illuminate\Support\Facades\Route;

Route::get('/test', function () {
    return 'test'; // just to simplify
});
Run Code Online (Sandbox Code Playgroud)

RouteServiceProvider.php包含:

<?php
declare(strict_types=1);

namespace Foo\App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'Foo\Http\Controllers';

    /**
     * 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
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

现在我要编写一些测试,但我需要代码覆盖率,所以我添加了:

<logging>
    <log type="coverage-html" target="./report" charset="UTF-8"
         yui="true" highlight="true"
         lowUpperBound="50" highLowerBound="80"/>
</logging>
Run Code Online (Sandbox Code Playgroud)

进入我的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): Illuminate\Support\Facades\Facade::__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): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#3 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/phpunit/Framework/TestResult.php(671): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(Tests\Feature\ExampleTest))
#4 phar:///usr/local/Cellar/phpunit/7.0.1/libexec/phpunit-7.0.1.phar/phpunit/Framework/TestCase.php(687): PHPUnit\Framework\TestResult->run(Object(Tests\Feature\ExampleTest))
#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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

$ phpunit PHPUnit 7.0.1 由 Sebastian Bergmann 和贡献者编写。

.. 2 / 2 (100%)

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

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

怎么了?为什么具有代码覆盖率的 phpunit 不喜欢多个文件中的路由(但没有覆盖率它工作得很好)?

Mat*_*cki 5

有人遇到了同样的问题,并通过从代码覆盖范围中排除路由目录来解决该问题。所以我添加到phpunit.xml

\n\n
<filter>\n    <whitelist processUncoveredFilesFromWhitelist="true">\n        <directory suffix=".php">./app</directory>\n        <exclude>\n            <directory suffix=".php">./app/Http/Routes</directory>\n        </exclude>\n    </whitelist>\n</filter>\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且phpunit覆盖效果很好。

\n\n

Goshh\xe2\x80\xa6 外观很棘手。

\n