laravel / horizon

Dashboard and code-driven configuration for Laravel queues.
https://laravel.com/docs/horizon
MIT License
3.83k stars 643 forks source link

Having Horizon installed is breaking some tests #1406

Closed rpungello closed 4 months ago

rpungello commented 4 months ago

Horizon Version

5.23.2

Laravel Version

11.1.1

PHP Version

8.3.4

Redis Driver

PhpRedis

Redis Version

6.0.2

Database Driver & Version

No response

Description

I have an application I've added Laravel Horizon to. After doing so, one of our tests started mysteriously failing. I was able to reproduce the issue in a dummy repository (linked in steps to reproduce).

Steps To Reproduce

See: https://github.com/rpungello/laravel-test

On the master branch, running the tests via sail test produces the following error: "Received Mockery_0_Illuminate_Cache_CacheManager::driver(), but no expectations were specified"

Switching to HEAD~1 (without Horizon) and re-running composer install, and the error goes away (sail test runs successfully). The only difference between these two commits is the addition of Horizon to composer.json. The horizon:install command wasn't even run.

driesvints commented 4 months ago

You're doing cache faking here: https://github.com/rpungello/laravel-test/blob/master/tests/Feature/ExampleTest.php#L16-L19

This doesn't work when installing Horizon because it needs to resolve the cache driver upon initialisation through its service provider. Since you only added one expectation to the cache faking, this fails.

rpungello commented 4 months ago

@driesvints is this documented somewhere? I followed the official Laravel docs for mocking facades, which explicitly show the method for mocking the Cache facade I used: https://laravel.com/docs/11.x/mocking#mocking-facades

driesvints commented 4 months ago

@rpungello I want to check something real quickly with you. Does it work if you replace:

            $this->app->bind(Console\WorkCommand::class, function ($app) {
                return new Console\WorkCommand($app['queue.worker'], $app['cache.store']);
            });

With

        if ($this->app->runningInConsole()) {
            $this->app->bind(Console\WorkCommand::class, function ($app) {
                return new Console\WorkCommand($app['queue.worker'], $app['cache.store']);
            });
        }

In the HorizonServiceProvider?

rpungello commented 4 months ago

@driesvints it doesn't, though that at least gives me some info on where to look. Commenting out that section and preventing registerCommands() from running during boot() does solve the issue, but obviously that can't be done unilaterally without breaking Horizon.

Your suggestion of using runningInConsole() lead me to the runningUnitTests() function of $this->app, but regrettably that returns false during the initialization of Horizon (despite returning true while actually running the example test).

It does seem possible to work around the issue by adding an expectation that driver() be called on the cache facade, but it's unclear if that could result in other issues if there's a scenario where Horizon doesn't end up calling that function.

rpungello commented 4 months ago

Looking at this a little closer, within HorizonServiceProvider::registerCommands(), changing if ($this->app->runningInConsole()) { to if ($this->app->runningInConsole() && !$this->app->runningUnitTests()) { Does resolve the issue. I was mistaken previously that runningUnitTests() returned false, that was for the standard ExampleTest Laravel ships with, but for my test feature test it does return true.

What I don't know is whether this could realistically be modified without breaking something else.

driesvints commented 4 months ago

@rpungello let's try it: https://github.com/laravel/horizon/pull/1410

driesvints commented 4 months ago

Unfortunately it doesn't seems possible since we need some of these commands themselves in our own test suite. I'm sorry @rpungello but it seems these two just aren't compatible right now.

rpungello commented 4 months ago

@driesvints noted, thanks for the info. I'll settle for just mocking the call to driver() that Horizon ends up calling, as so far that doesn't seem to have any negative repercussions.