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

[5.8] Fix fake dispatcher issue #28238

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Illuminate/Auth/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register()
$this->registerAccessGate();

$this->registerRequestRebindHandler();

$this->registerEventRebindHandler();
}

/**
Expand Down Expand Up @@ -87,4 +89,19 @@ protected function registerRequestRebindHandler()
});
});
}

/**
* Register a resolver for the 'events' rebinding.
*
* @return void
*/
protected function registerEventRebindHandler()
{
$this->app->rebinding('events', function ($app, $dispatcher) {
$guard = $app['auth']->guard();
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($dispatcher);
}
});
}
}
77 changes: 77 additions & 0 deletions tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

use Illuminate\Support\Str;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\SessionGuard;
use Illuminate\Events\Dispatcher;
use Orchestra\Testbench\TestCase;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Logout;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Support\Facades\Schema;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Support\Testing\Fakes\EventFake;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;

/**
Expand Down Expand Up @@ -245,4 +249,77 @@ public function test_auth_via_attempt_remembering()

$this->assertNull($provider->retrieveByToken($user->id, $token));
}

public function test_dispatcher_changes_if_there_is_one_on_the_auth_guard()
{
$this->assertInstanceOf(SessionGuard::class, $this->app['auth']->guard());
$this->assertInstanceOf(Dispatcher::class, $this->app['auth']->guard()->getDispatcher());

Event::fake();

$this->assertInstanceOf(SessionGuard::class, $this->app['auth']->guard());
$this->assertInstanceOf(EventFake::class, $this->app['auth']->guard()->getDispatcher());
}

public function test_dispatcher_changes_if_there_is_one_on_the_custom_auth_guard()
{
$this->app['config']['auth.guards.myGuard'] = [
'driver' => 'myCustomDriver',
'provider' => 'user',
];

Auth::extend('myCustomDriver', function () {
return new MyCustomGuardStub();
});

$this->assertInstanceOf(MyCustomGuardStub::class, $this->app['auth']->guard('myGuard'));
$this->assertInstanceOf(Dispatcher::class, $this->app['auth']->guard()->getDispatcher());

Event::fake();

$this->assertInstanceOf(MyCustomGuardStub::class, $this->app['auth']->guard('myGuard'));
$this->assertInstanceOf(EventFake::class, $this->app['auth']->guard()->getDispatcher());
}

public function test_has_no_problem_if_there_is_no_dispatching_the_auth_custom_guard()
{
$this->app['config']['auth.guards.myGuard'] = [
'driver' => 'myCustomDriver',
'provider' => 'user',
];

Auth::extend('myCustomDriver', function () {
return new MyDispatcherLessCustomGuardStub();
});

$this->assertInstanceOf(MyDispatcherLessCustomGuardStub::class, $this->app['auth']->guard('myGuard'));

Event::fake();

$this->assertInstanceOf(MyDispatcherLessCustomGuardStub::class, $this->app['auth']->guard('myGuard'));
}
}

class MyCustomGuardStub
{
protected $events;

public function __construct()
{
$this->setDispatcher(new Dispatcher());
}

public function setDispatcher(Dispatcher $events)
{
$this->events = $events;
}

public function getDispatcher()
{
return $this->events;
}
}

class MyDispatcherLessCustomGuardStub
{
}