Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having Horizon installed is breaking some tests #1406

Closed
rpungello opened this issue Apr 1, 2024 · 8 comments
Closed

Having Horizon installed is breaking some tests #1406

rpungello opened this issue Apr 1, 2024 · 8 comments

Comments

@rpungello
Copy link

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

@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
Copy link
Author

@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
Copy link
Author

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
Copy link
Member

@rpungello let's try it: #1410

@driesvints
Copy link
Member

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
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants