[Socialite] Need help testing controller method that uses Socialite's userFromToken #37863
Replies: 2 comments
|
maybe this can help you - |
|
You do not need a real provider token for this test. Mock the provider returned by For example: use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
use Mockery;
public function test_it_logs_in_a_user_with_a_social_provider_token(): void
{
$socialUser = (new SocialiteUser)->map([
'id' => 'google-123',
'nickname' => 'Jane',
'name' => 'Jane Doe',
'email' => 'jane@example.test',
'avatar' => null,
]);
$provider = Mockery::mock();
$provider->shouldReceive('userFromToken')
->once()
->with('fake-provider-token')
->andReturn($socialUser);
Socialite::shouldReceive('driver')
->once()
->with('google')
->andReturn($provider);
// Replace this URL with the route that calls your login method.
$response = $this->postJson('/api/auth/driver', [
'driver' => 'google',
'driver_token' => 'fake-provider-token',
'device_name' => 'phpunit',
]);
$response
->assertOk()
->assertJsonStructure([
'access_token',
'user',
]);
$this->assertDatabaseHas('users', [
'email' => 'jane@example.test',
'name' => 'Jane',
]);
}This leaves your request validation, Using an explicit provider mock is also important for this particular method: newer Socialite versions have You can add a second test where Socialite documentation: https://laravel.com/docs/socialite |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have been looking for about a week and did some trial and errors but still can't find any solution for this. All I want to do is to assert the returned response from the endpoint that points to a method
loginDriverinside myAuthController, but the Socialite implementation was triggered and throws an exception.The implementation works when I test it manually using real device but I need it to be tested via PHPUnit.
I went to the docs about Mocking, but I'm not sure what to put in
shouldReceivefor this to work. To help you get a better idea, please have a look at my implementation.Any kind of help would be very appreciated.
AuthTest.phpAuthController.phpRequestLoginDriver.phpAll reactions