Skip to content

Commit 6d46a8e

Browse files
committed
document testing socialite
1 parent b8f025f commit 6d46a8e

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

socialite.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Slack Bot Scopes](#slack-bot-scopes)
1212
- [Optional Parameters](#optional-parameters)
1313
- [Retrieving User Details](#retrieving-user-details)
14+
- [Testing](#testing)
1415

1516
<a name="introduction"></a>
1617
## Introduction
@@ -229,3 +230,66 @@ use Laravel\Socialite\Socialite;
229230

230231
return Socialite::driver('google')->stateless()->user();
231232
```
233+
234+
<a name="testing"></a>
235+
## Testing
236+
237+
Laravel Socialite provides a convenient way to test OAuth authentication flows without making actual requests to OAuth providers. The `fake` method allows you to mock the OAuth provider's behavior and define the user data that should be returned.
238+
239+
<a name="faking-the-redirect"></a>
240+
#### Faking the Redirect
241+
242+
To test that your application correctly redirects users to an OAuth provider, you may invoke the `fake` method before making a request to your redirect route. This will cause Socialite to return a redirect to a fake authorization URL instead of redirecting to the actual OAuth provider:
243+
244+
```php
245+
use Laravel\Socialite\Socialite;
246+
247+
test('user is redirected to github', function () {
248+
Socialite::fake('github');
249+
250+
$response = $this->get('/auth/github/redirect');
251+
252+
$response->assertRedirect();
253+
});
254+
```
255+
256+
<a name="faking-the-callback"></a>
257+
#### Faking the Callback
258+
259+
To test your application's callback route, you may invoke the `fake` method and provide a `User` instance that should be returned when your application requests the user's details from the provider. The `User` instance may be created using the `map` method:
260+
261+
```php
262+
use Laravel\Socialite\Socialite;
263+
use Laravel\Socialite\Two\User;
264+
265+
test('user can login with github', function () {
266+
Socialite::fake('github', (new User)->map([
267+
'id' => 'github-123',
268+
'name' => 'Jason Beggs',
269+
'email' => '[email protected]',
270+
]));
271+
272+
$response = $this->get('/auth/github/callback');
273+
274+
$response->assertRedirect('/dashboard');
275+
276+
$this->assertDatabaseHas('users', [
277+
'name' => 'Jason Beggs',
278+
'email' => '[email protected]',
279+
'github_id' => 'github-123',
280+
]);
281+
});
282+
```
283+
284+
By default, the `User` instance will also include a `token` property. If needed, you may manually specify additional properties on the `User` instance:
285+
286+
```php
287+
$fakeUser = (new User)->map([
288+
'id' => 'github-123',
289+
'name' => 'Jason Beggs',
290+
'email' => '[email protected]',
291+
])->setToken('fake-token')
292+
->setRefreshToken('fake-refresh-token')
293+
->setExpiresIn(3600)
294+
->setApprovedScopes(['read', 'write'])
295+
```

0 commit comments

Comments
 (0)