|
11 | 11 | - [Slack Bot Scopes](#slack-bot-scopes) |
12 | 12 | - [Optional Parameters](#optional-parameters) |
13 | 13 | - [Retrieving User Details](#retrieving-user-details) |
| 14 | +- [Testing](#testing) |
14 | 15 |
|
15 | 16 | <a name="introduction"></a> |
16 | 17 | ## Introduction |
@@ -229,3 +230,66 @@ use Laravel\Socialite\Socialite; |
229 | 230 |
|
230 | 231 | return Socialite::driver('google')->stateless()->user(); |
231 | 232 | ``` |
| 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 | + |
| 270 | + ])); |
| 271 | + |
| 272 | + $response = $this->get('/auth/github/callback'); |
| 273 | + |
| 274 | + $response->assertRedirect('/dashboard'); |
| 275 | + |
| 276 | + $this->assertDatabaseHas('users', [ |
| 277 | + 'name' => 'Jason Beggs', |
| 278 | + |
| 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 | + |
| 291 | +])->setToken('fake-token') |
| 292 | + ->setRefreshToken('fake-refresh-token') |
| 293 | + ->setExpiresIn(3600) |
| 294 | + ->setApprovedScopes(['read', 'write']) |
| 295 | +``` |
0 commit comments