PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

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

 January 19, 2022     laravel, laravel-5, php     No comments   

Issue

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.


Solution

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>


Answered By - Matthew Daly
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing