Skip to content

[8.x] Fix 'actingAsClient' testing method #1117

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

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 34 additions & 2 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Laravel\Passport;

use Mockery;
use DateInterval;
use Carbon\Carbon;
use DateInterval;
use DateTimeInterface;
use Illuminate\Support\Facades\Route;
use League\OAuth2\Server\ResourceServer;
use Mockery;

class Passport
{
Expand Down Expand Up @@ -405,6 +406,37 @@ public static function actingAs($user, $scopes = [], $guard = 'api')
return $user;
}

/**
* Set the current client for the application with the given scopes.
*
* @param  \Laravel\Passport\Client  $client
* @param  array  $scopes
* @return \Laravel\Passport\Client
*/
public static function actingAsClient($client, $scopes = [])
{
$token = app(self::tokenModel());
$token->client = $client;
$token->scopes = $scopes;

$mock = Mockery::mock(ResourceServer::class);
$mock->shouldReceive('validateAuthenticatedRequest')
->andReturnUsing(function ($request) use ($token) {
return $request->withAttribute('oauth_client_id', $token->client->id)
->withAttribute('oauth_access_token_id', $token->id)
->withAttribute('oauth_scopes', $token->scopes);
});

app()->instance(ResourceServer::class, $mock);

$mock = Mockery::mock(TokenRepository::class);
$mock->shouldReceive('find')->andReturn($token);

app()->instance(TokenRepository::class, $mock);

return $client;
}

/**
* Set the storage location of the encryption keys.
*
Expand Down