Files
alfa-bank-sdk/tests/Feature/Auth/AuthenticationTest.php

55 lines
1.8 KiB
PHP

<?php
use Apoloz\AlfaBank\Core\AlfaBankConnector;
use Apoloz\AlfaBank\Core\AlfaConfig;
use Apoloz\AlfaBank\Requests\Auth\GetTokenRequest;
use Saloon\Http\Faking\MockResponse;
test('can authenticate and get token', function () {
$mockClient = new \Saloon\Http\Faking\MockClient([
GetTokenRequest::class => MockResponse::make([
'access_token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 3600,
'token_type' => 'Bearer',
], 200),
]);
$config = new AlfaConfig(
baseUrl: 'https://test.com',
certificatePath: __DIR__ . '/../../test_key.pem', // Use any existing file
privateKeyPath: __DIR__ . '/../../test_key.pem'
);
$connector = new AlfaBankConnector($config);
$connector->withMockClient($mockClient);
$request = new GetTokenRequest('client-id', 'client-secret', null, null, 'client_credentials');
$response = $connector->send($request);
expect($response->status())->toBe(200)
->and($response->dto()->access_token)->toBe('test-access-token');
});
test('can login and set token', function () {
$mockClient = new \Saloon\Http\Faking\MockClient([
GetTokenRequest::class => MockResponse::make([
'access_token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 3600,
'token_type' => 'Bearer',
], 200),
]);
$config = new AlfaConfig(
baseUrl: 'https://test.com',
certificatePath: __DIR__ . '/../../test_key.pem',
privateKeyPath: __DIR__ . '/../../test_key.pem'
);
$connector = new AlfaBankConnector($config);
$connector->withMockClient($mockClient);
$connector->login('client-id', 'client-secret');
expect($connector->getAuthenticator())->not->toBeNull();
});