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

Event::fake() does not replace dispatcher for guard #27451

Closed
OwlManAtt opened this issue Feb 8, 2019 · 0 comments

Comments

Projects
None yet
2 participants
@OwlManAtt
Copy link

commented Feb 8, 2019

  • Laravel Version: 5.7.25
  • PHP Version: 7.2.9

Description:

Per the docs, no listeners should be executed after running Event::fake().

The auth guard is not informed that Illuminate\Support\Testing\Fakes\EventFake should be used for dispatching events.

When I call Auth::login($user), it is dispatching Illuminate\Auth\Events\Login to the real event dispatcher & my listener is being executed.

Steps To Reproduce:

Succinctly:

>>> Event::fake()
=> null
>>> Auth::guard()->getDispatcher()
=> Illuminate\Events\Dispatcher {#25}

Workaround:

In a test that I don't want to have my listeners executed:

<?php

namespace Tests;

use Tests\TestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;

class FooTest extends TestCase
{
    public function test_login_flow()
    {
        Event::fake();
        Auth::guard()->setDispatcher(app('events'));

        // . . .
    }
}

@driesvints driesvints added the bug label Feb 8, 2019

TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue Apr 16, 2019

[5.8] Fix fake dispatcher issue
Description:
Integration tests proof that it will work with both built-in session guard or any other custom guard whether it has a dispatcher or not.

Note : some build-in guards like Token guard does dispatch any events, hence there is no `setDispatcher ` method or any dispatcher on it.
So there are 2 types of guards.

which I think a contract (interface) is missing here, in order to mark the SessionGuard class or any other custom guard as an event dispatching guard, and enforce `setDispatcher` and `getDispatcher` methods on them.)

So the clunky `method_exists` check won't be needed.

Re-submit: laravel#28131
fixed: laravel#27451

TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue Apr 16, 2019

[5.8] Fix fake dispatcher issue
Description:
Integration tests proof that it will work with both built-in session guard or any other custom guard whether it has a dispatcher or not.

Note : some build-in guards like Token guard does dispatch any events, hence there is no `setDispatcher ` method or any dispatcher on it.
So there are 2 types of guards.

which I think a contract (interface) is missing here, in order to mark the SessionGuard class or any other custom guard as an event dispatching guard, and enforce `setDispatcher` and `getDispatcher` methods on them.)

So the clunky `method_exists` check won't be needed.

Re-submit: laravel#28131
fixed: laravel#27451

taylorotwell pushed a commit to illuminate/auth that referenced this issue Apr 17, 2019

[5.8] Fix fake dispatcher issue
Description:
Integration tests proof that it will work with both built-in session guard or any other custom guard whether it has a dispatcher or not.

Note : some build-in guards like Token guard does dispatch any events, hence there is no `setDispatcher ` method or any dispatcher on it.
So there are 2 types of guards.

which I think a contract (interface) is missing here, in order to mark the SessionGuard class or any other custom guard as an event dispatching guard, and enforce `setDispatcher` and `getDispatcher` methods on them.)

So the clunky `method_exists` check won't be needed.

Re-submit: laravel/framework#28131
fixed: laravel/framework#27451

evsign added a commit to evsign/framework that referenced this issue Apr 27, 2019

[5.8] Fix fake dispatcher issue
Description:
Integration tests proof that it will work with both built-in session guard or any other custom guard whether it has a dispatcher or not.

Note : some build-in guards like Token guard does dispatch any events, hence there is no `setDispatcher ` method or any dispatcher on it.
So there are 2 types of guards.

which I think a contract (interface) is missing here, in order to mark the SessionGuard class or any other custom guard as an event dispatching guard, and enforce `setDispatcher` and `getDispatcher` methods on them.)

So the clunky `method_exists` check won't be needed.

Re-submit: laravel#28131
fixed: laravel#27451
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.