Laravel 5.5:PHPUnit(具有覆盖率)不喜欢来自多个文件的路由,并抛出“尚未设置外观根".没有覆盖它是绿色的 [英] 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
问题描述
我有Laravel 5.5,我决定在文件中对路由进行分组,以更有意义的方式组织它们.
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 包含:
<?php
declare(strict_types=1);
namespace Foo\Http\Routes\Web;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
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
});
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
});
}
}
到目前为止,我可以通过调用php artisan route:list
来确认一切正常:
Up to now I can confirm that everything works by calling php artisan route:list
:
现在我要编写一些测试,但是我需要覆盖代码,所以我添加了:
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>
进入我的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
在删除添加到xml文件中的覆盖线并再次运行phpunit
之后,立即变为绿色.
Straight after I remove the coverage lines I added to xml file and run phpunit
again - it's green.
Sebastian Bergmann及其贡献者提供的$ phpunit PHPUnit 7.0.1.
$ phpunit PHPUnit 7.0.1 by Sebastian Bergmann and contributors.
.. 2 /2(100%)
.. 2 / 2 (100%)
时间:381毫秒,内存:20.00MB
Time: 381 ms, Memory: 20.00MB
好的(2个测试,2个断言)
OK (2 tests, 2 assertions)
怎么了?为什么具有代码覆盖率的phpunit不喜欢多个文件中的路由(但如果没有覆盖率,它就可以正常工作)?
What's wrong? Why phpunit with code coverage doesn't like routes in multiple files (but without coverage it works perfectly fine)?
推荐答案
有人遇到了相同的问题,并通过从代码覆盖范围中排除路由目录.所以我加入了phpunit.xml
:
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>
而且phpunit
具有覆盖范围的效果很好.
And phpunit
with coverage works good.
天哪……外墙很棘手.
这篇关于Laravel 5.5:PHPUnit(具有覆盖率)不喜欢来自多个文件的路由,并抛出“尚未设置外观根".没有覆盖它是绿色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!