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

Matthew Daly

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.

Matthew Daly

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

using orchestral testbench in laravel workbench - class not found

Fatal error: Class 'MongoDate' not found when using mongodb php driver 1.1.2 and PHP 7.0.2 - Laravel 5.1

Laravel api route class to found error

Fatal error: Uncaught Error: Class 'Route' not found in C:\xampp\htdocs\code\project\routes\web.php:18

Laravel PHP Fatal Error Class not found when seeding

Class not found error when using CloudConvert to combine PDFs using PHP

Laravel CustomServiceProvider Class not found in package when required

Route is not defined when using laravel package

Laravel 4 PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found

Laravel Newbie: model Class Not Found in routes.php

Class "Routes" not found laravel 9

Moving routes/web.php file gives class not found error

Laravel package Controller not found in route

Laravel 8 - Class not found error using SDK

Laravel 5 - Fatal error: Class not found Vendor Feedly Package

Laravel 5 - Custom Package "Class Not Found" When Pushed to Heroku

Why doesn't PHP throw class not found error when using ::class?

Fatal Error: Class not Found when using namespaces

Error on authentication when using passport package on laravel

composer - testing created package - class not found

Laravel 5 Package, Class not found

Routed not found 404 error but routes exist in laravel

No tests found in <package> when testing with Espresso

How to cache routes when using Symfony Routing as a standalone?

FatalErrorException in routes.php line 51: Class 'app\Game' not found laravel 5.1

`Not Found (HTTP 404)` error when using `genius` package to get lyrics

GitLab '404 Not Found' error when publishing a package using NPM

Using Testing Library in standalone

Disable route filters when testing in Laravel 4.2