简体   繁体   中英

Error: Class 'Route' not found in routes.php when testing standalone Laravel package using Orchestra Testbench

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

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.

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

Source

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

Source

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.

EDIT: This is interesting...

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

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:

The filter section from file phpunit.xml :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM