Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/CoMagic/CallApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CallApiClient
{
private string $version = 'v4.0';
private ?string $accessToken = null;
private ?string $accessTokenExpires = null;
private ?int $accessTokenExpires = null;
private ?string $login = null;
private ?string $password = null;
private ?Client $client = null;
Expand Down Expand Up @@ -40,6 +40,11 @@ public function __construct(CallApiConfig $config, ?Client $client = null)
'/' . $this->version;
}

public function getAccessToken(): ?string
{
return $this->accessToken;
}

private function refreshAccessToken(): void
{
// Check if access token is not expired
Expand All @@ -50,16 +55,16 @@ private function refreshAccessToken(): void
return;
}

$data = $this->doRequest(
$response = $this->doRequest(
'login.user',
[
'login' => $this->login,
'password' => $this->password,
]
);

$this->accessToken = $data->access_token;
$this->accessTokenExpires = $data->expire_at;
$this->accessToken = $response->access_token;
$this->accessTokenExpires = $response->expire_at;
}

/**
Expand Down Expand Up @@ -113,7 +118,7 @@ private function doRequest(string $method, array $params)
$responseBody = json_decode($response->getBody()->getContents());

if (isset($responseBody->result)) {
$this->metadata = $responseBody->result->metadata;
$this->metadata = $responseBody->result->metadata ?? null;
}

if (isset($responseBody->error)) {
Expand Down
65 changes: 65 additions & 0 deletions tests/CallApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,71 @@ public function testCanBeCreated(): void
);
}

public function testCanObtainAccessTokenByLoginAndPassword(): void
{
$config = new CallApiConfig('login', 'password');
$httpClient = $this->createMock(Client::class);

$expectedNewAccessToken = 'new-access-token';
$expectedBaseUri = rtrim($config->getEntryPoint(), '/') . '/v4.0';
$expectedHttpLoginPayload = [
'jsonrpc' => '2.0',
'id' => time(),
'method' => 'login.user',
'params' => [
'login' => $config->getLogin(),
'password' => $config->getPassword(),
],
];
$expectedHttpListCallsPayload = [
'jsonrpc' => '2.0',
'id' => time(),
'method' => 'list.calls',
'params' => [
'access_token' => $expectedNewAccessToken,
],
];
$httpClient->expects($this->exactly(2))
->method('post')
->with(
$expectedBaseUri,
$this->logicalOr(
$this->equalTo(['json' => $expectedHttpLoginPayload]),
$this->equalTo(['json' => $expectedHttpListCallsPayload])
)
)
->willReturnOnConsecutiveCalls(
new Response(
200,
[],
json_encode([
'result' => [
'data' => [
'access_token' => $expectedNewAccessToken,
'expire_at' => time() + 3600,
],
],
])
),
new Response(
200,
[],
json_encode(['result' => ['data' => []]])
)
);

$client = new CallApiClient($config, $httpClient);

$this->assertNull($client->getAccessToken());

$client->listCalls();

$this->assertEquals(
$expectedNewAccessToken,
$client->getAccessToken()
);
}


/** @dataProvider apiCallsProvider */
public function testCanDoApiCalls(
Expand Down