laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.22k stars 10.91k forks source link

Event faking regression introduced in #49730 #50467

Closed stancl closed 6 months ago

stancl commented 6 months ago

Laravel Version

From 11.x-dev#abfd659 on

PHP Version

8.3.3

Database Driver & Version

No response

Description

I'll start by saying that I'm not 100% sure if this is a bug introduced on Laravel's end, or if we should simply somehow update our code to support Laravel 11, but I discovered this while updating dependencies in a PR adding Laravel 11 support to our tenancy package.

1) Stancl\Tenancy\Tests\QueueTest::tenant_id_is_passed_to_tenant_queues
TypeError: Illuminate\Log\Context\Repository::__construct(): Argument #1 ($events) must be of type Illuminate\Events\Dispatcher, Illuminate\Support\Testing\Fakes\EventFake given

/var/www/html/vendor/laravel/framework/src/Illuminate/Log/Context/Repository.php:50
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:944
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:293
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:900
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:787
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:1026
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:723
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:1011
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:1446
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:208
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:351
/var/www/html/vendor/laravel/framework/src/Illuminate/Log/Context/ContextServiceProvider.php:30
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:308
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:142
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:129
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:107
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:60
/var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:45
/var/www/html/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:254
/var/www/html/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:230
/var/www/html/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:77
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bus/PendingDispatch.php:193
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:388
/var/www/html/tests/QueueTest.php:124

Notice the ContextServiceProvider in the stack trace. The test is just:

/** @test */
public function tenant_id_is_passed_to_tenant_queues()
{
    config(['queue.default' => 'sync']);

    $tenant = Tenant::create();

    tenancy()->initialize($tenant);

    Event::fake([JobProcessing::class, JobProcessed::class]);

    dispatch(new TestJob($this->valuestore)); // line 124

    Event::assertDispatched(JobProcessing::class, function ($event) {
        return $event->job->payload()['tenant_id'] === tenant('id');
    });
}

This test passes on 5e87357 (the commit immediately preceding the PR) but fails from abfd659 on.

Steps To Reproduce

I believe the combination of Event::fake([JobProcessing::class, JobProcessed::class]); and dispatching a job is the cause of this.

Event::fake() (without any events specified) and dispatching a job also seems to cause this. This test fails with the same stack trace:

/** @test */
public function tenant_id_is_not_passed_to_central_queues()
{
    $tenant = Tenant::create();

    tenancy()->initialize($tenant);

    Event::fake();

    config(['queue.connections.central' => [
        'driver' => 'sync',
        'central' => true,
    ]]);

    dispatch(new TestJob($this->valuestore))->onConnection('central');

    Event::assertDispatched(JobProcessing::class, function ($event) {
        return ! isset($event->job->payload()['tenant_id']);
    });
}
taylorotwell commented 6 months ago

Fixed.

stancl commented 6 months ago

Thanks! Can confirm https://github.com/laravel/framework/commit/dde4bfecca6a224dc582eff0f7401ba5ca2de0ad fixed this for us.