Skip to content

Commit d47175a

Browse files
authored
Merge pull request #1083 from gdebrauwer/testing-acting-as-client
[7.x] 'actingAsClient' method for tests
2 parents cc39dc6 + 0ce1d8c commit d47175a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Passport.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DateInterval;
77
use DateTimeInterface;
88
use Illuminate\Support\Facades\Route;
9+
use League\OAuth2\Server\ResourceServer;
910
use Mockery;
1011

1112
class Passport
@@ -405,6 +406,29 @@ public static function actingAs($user, $scopes = [], $guard = 'api')
405406
return $user;
406407
}
407408

409+
/**
410+
* Set the current client for the application with the given scopes.
411+
*
412+
* @param  \Laravel\Passport\Client  $client
413+
* @param  array  $scopes
414+
* @return \Laravel\Passport\Client
415+
*/
416+
public static function actingAsClient($client, $scopes = [])
417+
{
418+
$mock = Mockery::mock(ResourceServer::class);
419+
420+
$mock->shouldReceive('validateAuthenticatedRequest')
421+
->andReturnUsing(function ($request) use ($client, $scopes) {
422+
return $request
423+
->withAttribute('oauth_client_id', $client->id)
424+
->withAttribute('oauth_scopes', $scopes);
425+
});
426+
427+
app()->instance(ResourceServer::class, $mock);
428+
429+
return $client;
430+
}
431+
408432
/**
409433
* Set the storage location of the encryption keys.
410434
*

0 commit comments

Comments
 (0)