1

My tests are running properly and all green when I run them without any particular flag:

phpunit --configuration /home/vagrant/code/phpunit.xml /home/vagrant/code/tests

When I enable process isolation, the tests are failing:

phpunit --configuration /home/vagrant/code/phpunit.xml /home/vagrant/code/tests --process-isolation

The error looks like this:

PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:234
Stack trace:
#0 /home/vagrant/code/routes/web.php(6): Illuminate\Support\Facades\Facade::__callStatic('get', Array)
#1 Standard input code(1653): require_once('/home/vagrant/c...')
#2 {main}
  thrown in /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 234
Fatal error: Uncaught RuntimeException: A facade root has not been set. in /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:234
Stack trace:
#0 /home/vagrant/code/routes/web.php(6): Illuminate\Support\Facades\Facade::__callStatic('get', Array)
#1 Standard input code(1653): require_once('/home/vagrant/c...')
#2 {main}
  thrown in /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 234

I already tried a few things from the 3 or 4 threads I found on Laracast or StackOverflow regarding that kind of exception but nothing works yet:

  • Excluding routes from PHPUnit.xml
  • Running phpunit from vendor/bin or the one included in the system
  • etc.

Any idea about where to look for or check? I am running out of ideas.

Using latest Laravel 5.8 and PHPUnit 8.0.6

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124

2 Answers2

0

First thing I would try is: run the failing test alone rather than against the entire tests folder.

Next, I would make sure if the failing test is calling setFacadeApplication() via createApplication() or something similar. For instance, if setUp() doesn't call its parent setUp(), the static variable $app in Facade might not be set. That's the situation the RuntimeException message says.

protected function setUp() // in your test case class
{
    // parent::setUp(); // it's necessary
}

Since it is static variable, once it's set its value would stay available among all the test cases without --process-isolation.

Nobu
  • 9,965
  • 4
  • 40
  • 47
0

I faced the same error. I fixed it by creating a new app container and then binding it to the Facade.

use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;

/**
* Setup a new app instance container
* 
* @var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');

/**
* Set $app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);

in lumen: bootstrap/app.php

$app->withFacades();
Moradnejad
  • 3,466
  • 2
  • 30
  • 52