错误:使用Orchestra Testbench测试独立的Laravel软件包时,在route.php中找不到类"Route"

错误:使用Orchestra Testbench测试独立的Laravel软件包时,在route.php中找不到类

问题描述:

此刻我有一个奇怪的问题.

I've got a bit of a weird issue at the moment.

我目前正在Github上的 https://github上构建一个Laravel软件包. com/matthewbdaly/laravel-error-snapshot .我正在使用 Orchestra Testbench程序包对该程序包及其BrowserKit扩展进行单独测试

I'm currently building a Laravel package which is on Github at https://github.com/matthewbdaly/laravel-error-snapshot. I'm using the Orchestra Testbench package to test this package on its own together with the BrowserKit extension for it.

当我在本地运行测试套件时,它运行良好.但是,在Travis CI中,它将引发以下错误:

When I run the test suite locally, it works fine. However, in Travis CI, it throws the following error:

Error: Class 'Route' not found in /home/travis/build/matthewbdaly/laravel-error-snapshot/src/routes.php on line 3

来源

所以我尝试在routes.php中显式导入Route门面.再次,它只在本地工作,但返回了不同的错误消息:

So I tried explicitly importing the Route facade in routes.php. Again it only worked locally, but it returned a different error message:

PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in /home/travis/build/matthewbdaly/laravel-error-snapshot/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218

来源

我真的不确定为什么它可以在一种环境下工作,而不能在另一种环境下工作.任何人都可以阐明这一点吗?就我所知,这似乎不是Travis CI的临时问题,但我不确定还有什么可能.

I'm really not sure why it's working in one environment but not another. Can anyone shed any light on this? It doesn't look like a temporary issue with Travis CI as far as I can tell, but I'm not sure what else it could be.

这很有趣...

如果我安装Xdebug并在启用coverage的情况下运行测试,则可以在本地重现该错误:

If I install Xdebug and run the tests with coverage enabled, I can reproduce the error locally:

PHP Fatal error:  Uncaught Error: Class 'Route' not found in /home/matthew/Projects/laravel-error-snapshot/src/routes.php:3
Stack trace:
#0 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(1097): include_once()
#1 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(269): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#2 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/phpunit/src/Framework/TestResult.php(659): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(Tests\Feature\SnapshotTest))
#3 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/phpunit/src/Framework/TestCase.php(894): PHPUnit\Framework\TestResult->run(Object(Tests\Feature\SnapshotTest))
#4 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/phpunit/src/Framework/TestSuite.php(744): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult))
#5 /home/matthew/Projects/laravel-error-snapshot/vendor/phpunit/ in /home/matthew/Projects/laravel-error-snapshot/src/routes.php on line 3

我认为这与Route门面没有以某种方式解决有关.

I think it's something to do with the Route facade not being resolved somehow.

事实证明,答案非常简单.我只需要从测试覆盖率生成中排除路由文件:

As it turned out, the answer was fairly straightforward. I just needed to exclude the routes file from the test coverage generation:

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./src</directory>
        <exclude>
            <directory suffix=".php">./src/database</directory>
            <file>./src/routes.php</file>
        </exclude>
    </whitelist>
</filter>