Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

php - Laravel 5.5: PHPUnit (with coverage) doesn't like routes from multiple files and throws "A facade root has not been set". Without coverage it's green

I have Laravel 5.5 where I decided to group routes in files to organise them in a more meaningful way.

Here's a simplified example - the web route files live in:

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

static.php contains:

<?php
declare(strict_types=1);

namespace FooHttpRoutesWeb;

use IlluminateSupportFacadesRoute;

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

test.php contains:

<?php
declare(strict_types=1);

namespace FooHttpRoutesWeb;

use IlluminateSupportFacadesRoute;

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

RouteServiceProvider.php contains:

<?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
        });
    }
}

Up to now I can confirm that everything works by calling php artisan route:list:

enter image description here

Now I was going to write some tests but I require code coverage, so I added:

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

into my phpunit.xml.

When I call phpunit I get:

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

Straight after I remove the coverage lines I added to xml file and run phpunit again - it's green.

$ phpunit PHPUnit 7.0.1 by Sebastian Bergmann and contributors.

.. 2 / 2 (100%)

Time: 381 ms, Memory: 20.00MB

OK (2 tests, 2 assertions)

What's wrong? Why phpunit with code coverage doesn't like routes in multiple files (but without coverage it works perfectly fine)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Someone had the same issue and fixed that by excluding routes directories from code coverage. So I added into phpunit.xml:

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

And phpunit with coverage works good.

Goshh… facades are tricky.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...