Laravel-auditing: Uncaught RuntimeException: A facade root has not been set. in unit tests

Created on 3 Apr 2019  路  6Comments  路  Source: owen-it/laravel-auditing

| Q | A
| ----------------- | ---
| Bug? |yes
| New Feature? | no
| Framework | Laravel
| Framework version | 5.8
| Package version | v9.0.0
| PHP version | 7.3.3

Actual Behaviour

Auditing works fine (thanks this is a great package), but unit tests which create the application don't seem to work for me. I get the Uncaught RuntimeException: A facade root has not been set..

In my tests I have a trait which creates the application, on a previous issue I see that you're supposed to mock app with App::shouldReceive('runningInConsole');, but that doesn't seem to help.

The trait looks like this:

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        /** @var \Illuminate\Foundation\Application $app */
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        Artisan::call('migrate:fresh');
        Artisan::call('db:seed');

        return $app;
    }
}

Steps to Reproduce

  1. vendor/bin/phpunit

'console' => false, is set in the config file so according to the docs, this package shouldn't be running during tests.

If you need anything else let me know, I wasn't sure what else to put here.

Possible Solutions

Something I tried was disabling auditing for the model if we're in a test:

/**
     * Boot function from laravel.
     */
    protected static function boot()
    {
        if (Testing::isTest()){
            self::disableAuditing();
        }

        parent::boot();
    }

But this still gave me the same error.

Most helpful comment

I had the same error in my tests because my test class extended wrong base test class (not that one provided by Laravel).

All 6 comments

The issue was that I had a test listener, the App::shouldReceive('runningInConsole'); should also go in there.

@CraigHarley I'm having this issue. Can you explain where the App::shouldReceive('runningInConsole'); should go please?

@CraigHarley Also having this issue, where did you put App::shouldReceive('runningInConsole');?

You probably have a function that gets called in your test (before they run), which creates the application and possibly runs migrations.

I put it in there. (the test listener)

The problem with that is bootAuditable is being called when the test results are deserialized by phpunit. (callstack included)

I'm not sure where to mock runningInConsole (or if I even can given this call stack)

PHPUnit 8.5.2 by Sebastian Bergmann and contributors.


Fatal error: Uncaught RuntimeException: A facade root has not been set. in /var/www/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 258

RuntimeException: A facade root has not been set. in /var/www/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 258

Call Stack:
    0.0028     402512   1. {main}() /var/www/vendor/phpunit/phpunit/phpunit:0
    0.0694    4984384   2. PHPUnit\TextUI\Command::main() /var/www/vendor/phpunit/phpunit/phpunit:61
    0.0694    4984496   3. PHPUnit\TextUI\Command->run() /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php:159
    0.9441   11082728   4. PHPUnit\TextUI\TestRunner->doRun() /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php:200
    1.1156   11524336   5. PHPUnit\Framework\TestSuite->run() /var/www/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:621
    1.1906   11527208   6. PHPUnit\Framework\TestSuite->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php:597
    1.2111   11661296   7. Tests\DateTest->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php:597
    1.2976   13326064   8. PHPUnit\Util\PHP\DefaultPhpProcess->runTestJob() /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php:754
    3.3823   13396168   9. PHPUnit\Util\PHP\DefaultPhpProcess->processChildResult() /var/www/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:171
    3.3823   13396488  10. unserialize() /var/www/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:272
    3.4284   15466192  11. App\Models\Firm->__wakeup() /var/www/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:272
    3.4284   15466192  12. App\Models\Firm->bootIfNotBooted() /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1652
    3.4284   15466568  13. Illuminate\Database\Eloquent\Model::boot() /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:186
    3.4284   15466568  14. Illuminate\Database\Eloquent\Model::bootTraits() /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:199
    3.4292   15468056  15. forward_static_call() /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:219
    3.4292   15468056  16. App\Models\Firm::bootAuditable() /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:219
    3.4292   15468056  17. App\Models\Firm::isAuditingEnabled() /var/www/vendor/owen-it/laravel-auditing/src/Auditable.php:49
    3.4306   15495120  18. Illuminate\Support\Facades\Facade::runningInConsole() /var/www/vendor/owen-it/laravel-auditing/src/Auditable.php:487
    3.4306   15495120  19. Illuminate\Support\Facades\Facade::__callStatic() /var/www/vendor/owen-it/laravel-auditing/src/Auditable.php:487

I had the same error in my tests because my test class extended wrong base test class (not that one provided by Laravel).

Was this page helpful?
0 / 5 - 0 ratings