getAuthorizationUrl(['beneficiaries']); expect($url)->toContain('https://id-sandbox.alfabank.ru/oidc/authorize') ->toContain('client_id=test-client-id') ->toContain('redirect_uri=' . urlencode('https://callback.url')) ->toContain('scope=beneficiaries'); }); test('can exchange code for token', function () { $mockClient = new \Saloon\Http\Faking\MockClient([ GetAccessTokenRequest::class => MockResponse::make([ 'access_token' => 'new-access-token', 'refresh_token' => 'new-refresh-token', 'expires_in' => 3600, 'token_type' => 'Bearer', ], 200), ]); $config = new AlfaConfig( baseUrl: 'https://sandbox.alfabank.ru', certificatePath: __DIR__ . '/../../test_key.pem', privateKeyPath: __DIR__ . '/../../test_key.pem', clientId: 'test-client-id', clientSecret: 'test-client-secret', redirectUri: 'https://callback.url' ); $connector = new AlfaBankConnector($config); $connector->withMockClient($mockClient); $authenticator = $connector->getAccessToken('auth-code-123'); expect($authenticator->getAccessToken())->toBe('new-access-token') ->and($authenticator->getRefreshToken())->toBe('new-refresh-token'); }); test('can refresh token', function () { $mockClient = new \Saloon\Http\Faking\MockClient([ GetRefreshTokenRequest::class => MockResponse::make([ 'access_token' => 'refreshed-access-token', 'refresh_token' => 'refreshed-refresh-token', 'expires_in' => 3600, 'token_type' => 'Bearer', ], 200), ]); $config = new AlfaConfig( baseUrl: 'https://sandbox.alfabank.ru', certificatePath: __DIR__ . '/../../test_key.pem', privateKeyPath: __DIR__ . '/../../test_key.pem', clientId: 'test-client-id', clientSecret: 'test-client-secret' ); $connector = new AlfaBankConnector($config); $connector->withMockClient($mockClient); $authenticator = $connector->refreshAccessToken('old-refresh-token'); expect($authenticator->getAccessToken())->toBe('refreshed-access-token') ->and($authenticator->getRefreshToken())->toBe('refreshed-refresh-token'); }); test('can login with client credentials', function () { $mockClient = new \Saloon\Http\Faking\MockClient([ \Saloon\Http\OAuth2\GetClientCredentialsTokenRequest::class => MockResponse::make([ 'access_token' => 'cc-access-token', 'expires_in' => 3600, 'token_type' => 'Bearer', ], 200), ]); $config = new AlfaConfig( baseUrl: 'https://sandbox.alfabank.ru', certificatePath: __DIR__ . '/../../test_key.pem', privateKeyPath: __DIR__ . '/../../test_key.pem', clientId: 'test-client-id', clientSecret: 'test-client-secret' ); $connector = new AlfaBankConnector($config); $connector->withMockClient($mockClient); $connector->login(); expect($connector->getAuthenticator())->not->toBeNull() ->and($connector->getAuthenticator()->getAccessToken())->toBe('cc-access-token'); });