Skip to content

Commit ef4ad79

Browse files
committed
fix: rename enums
1 parent 8249f2b commit ef4ad79

12 files changed

+741
-25
lines changed

src/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Client implements HttpClientInterface
3131

3232
private ConfigInterface $config;
3333
private CacheInterface $cache;
34+
private int $ttlMarginInSeconds = 60;
3435

3536
public function __construct(ConfigInterface $config, ClientInterface $httpClient, CacheInterface $cache)
3637
{
@@ -63,7 +64,7 @@ public function getAuthToken(): ?string
6364
$this->cache->set(
6465
$this->authTokenCacheKey(),
6566
$responseJson['access_token'],
66-
(int) $responseJson['expires_in']
67+
(int) $responseJson['expires_in'] - $this->ttlMarginInSeconds
6768
);
6869

6970
return $responseJson['access_token'];
@@ -133,7 +134,7 @@ public function sendDomesticTransaction(BankTransactionInterface $bankTransactio
133134
'beneficiaryName' => $bankTransaction->getRecipientName(),
134135
'amount' => $bankTransaction->getAmount(),
135136
'currency' => $bankTransaction->getCurrencyCode(),
136-
'narration' => $bankTransaction->getDescription() ?? 'transaction',
137+
'narration' => $bankTransaction->getDescription(),
137138
'auditId' => $bankTransaction->getReference(),
138139
'appId' => $this->config->getAppId(),
139140
]);

src/Enums/ErrorCode.php renamed to src/Enums/ErrorCodeEnum.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at https://mozilla.org/MPL/2.0/.
88

9-
namespace App\Enums\AccessBank;
9+
namespace BrokeYourBike\AccessBank\Enums;
1010

1111
/**
1212
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
1313
*
14-
* @method static ErrorCode NO_ERROR()
15-
* @method static ErrorCode UNAUTHORIZED()
16-
* @method static ErrorCode DUPLICATE_REQUEST()
17-
* @method static ErrorCode NO_RECORD()
18-
* @method static ErrorCode INVALID_DEBIT_ACCOUNT()
19-
* @method static ErrorCode RECONFIRM_BENEFICIARY_ACCOUNT()
20-
* @method static ErrorCode UNABLE_TO_PROCESS_REQUEST()
21-
* @method static ErrorCode BENEFICIARY_ACCOUNT_NO_PERMITTED()
22-
* @method static ErrorCode INSUFFICIENT_FUNDS()
23-
* @method static ErrorCode INVALID_ACCOUNT_NUMBER()
24-
* @method static ErrorCode UNABLE_TO_PROCESS_ON_NIBSS()
25-
* @method static ErrorCode UNABLE_TO_DEBIT()
26-
* @method static ErrorCode INVALID_CREDIT_ACCOUNT()
27-
* @method static ErrorCode NOT_PERMITTED()
14+
* @method static ErrorCodeEnum NO_ERROR()
15+
* @method static ErrorCodeEnum UNAUTHORIZED()
16+
* @method static ErrorCodeEnum DUPLICATE_REQUEST()
17+
* @method static ErrorCodeEnum NO_RECORD()
18+
* @method static ErrorCodeEnum INVALID_DEBIT_ACCOUNT()
19+
* @method static ErrorCodeEnum RECONFIRM_BENEFICIARY_ACCOUNT()
20+
* @method static ErrorCodeEnum UNABLE_TO_PROCESS_REQUEST()
21+
* @method static ErrorCodeEnum BENEFICIARY_ACCOUNT_NO_PERMITTED()
22+
* @method static ErrorCodeEnum INSUFFICIENT_FUNDS()
23+
* @method static ErrorCodeEnum INVALID_ACCOUNT_NUMBER()
24+
* @method static ErrorCodeEnum UNABLE_TO_PROCESS_ON_NIBSS()
25+
* @method static ErrorCodeEnum UNABLE_TO_DEBIT()
26+
* @method static ErrorCodeEnum INVALID_CREDIT_ACCOUNT()
27+
* @method static ErrorCodeEnum NOT_PERMITTED()
2828
* @psalm-immutable
2929
*/
30-
final class ErrorCode extends \MyCLabs\Enum\Enum
30+
final class ErrorCodeEnum extends \MyCLabs\Enum\Enum
3131
{
3232
/**
3333
* No error.

src/Enums/StatusCode.php renamed to src/Enums/StatusCodeEnum.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at https://mozilla.org/MPL/2.0/.
88

9-
namespace App\Enums\AccessBank;
9+
namespace BrokeYourBike\AccessBank\Enums;
1010

1111
/**
1212
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
1313
*
14-
* @method static StatusCode PENDING()
15-
* @method static StatusCode SUCCESS()
16-
* @method static StatusCode PROCESSING()
17-
* @method static StatusCode FAILED()
18-
* @method static StatusCode UNKNOWN()
14+
* @method static StatusCodeEnum PENDING()
15+
* @method static StatusCodeEnum SUCCESS()
16+
* @method static StatusCodeEnum PROCESSING()
17+
* @method static StatusCodeEnum FAILED()
18+
* @method static StatusCodeEnum UNKNOWN()
1919
* @psalm-immutable
2020
*/
21-
final class StatusCode extends \MyCLabs\Enum\Enum
21+
final class StatusCodeEnum extends \MyCLabs\Enum\Enum
2222
{
2323
/**
2424
* Transaction queued for processing.

tests/Enums/ErrorCodeEnumTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
namespace BrokeYourBike\AccessBank\Tests\Enums;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use BrokeYourBike\AccessBank\Enums\ErrorCodeEnum;
13+
14+
/**
15+
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
16+
*/
17+
class ErrorCodeEnumTest extends TestCase
18+
{
19+
/** @test */
20+
public function it_extends_myclabs_enum(): void
21+
{
22+
$parent = get_parent_class(ErrorCodeEnum::class);
23+
24+
$this->assertSame(\MyCLabs\Enum\Enum::class, $parent);
25+
}
26+
27+
/** @test */
28+
public function it_has_not_duplicate_values()
29+
{
30+
$allValuesRaw = ErrorCodeEnum::toArray();
31+
$this->assertNotEmpty($allValuesRaw);
32+
33+
$uniqueValuesraw = array_unique($allValuesRaw, SORT_STRING);
34+
35+
$this->assertEquals($allValuesRaw, $uniqueValuesraw);
36+
}
37+
}

tests/Enums/StatusCodeEnumTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
namespace BrokeYourBike\AccessBank\Tests\Enums;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use BrokeYourBike\AccessBank\Enums\StatusCodeEnum;
13+
14+
/**
15+
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
16+
*/
17+
class StatusCodeEnumTest extends TestCase
18+
{
19+
/** @test */
20+
public function it_extends_myclabs_enum(): void
21+
{
22+
$parent = get_parent_class(StatusCodeEnum::class);
23+
24+
$this->assertSame(\MyCLabs\Enum\Enum::class, $parent);
25+
}
26+
27+
/** @test */
28+
public function it_has_not_duplicate_values()
29+
{
30+
$allValuesRaw = StatusCodeEnum::toArray();
31+
$this->assertNotEmpty($allValuesRaw);
32+
33+
$uniqueValuesraw = array_unique($allValuesRaw, SORT_STRING);
34+
35+
$this->assertEquals($allValuesRaw, $uniqueValuesraw);
36+
}
37+
}

tests/FetchAccountBalanceRawTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
namespace BrokeYourBike\AccessBank\Tests;
10+
11+
use Psr\SimpleCache\CacheInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
use BrokeYourBike\AccessBank\Interfaces\ConfigInterface;
14+
use BrokeYourBike\AccessBank\Client;
15+
16+
/**
17+
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
18+
*/
19+
class FetchAccountBalanceRawTest extends TestCase
20+
{
21+
private string $appId = 'app-id';
22+
private string $clientSecret = 'secure-token';
23+
private string $subscriptionKey = 'subscription-key';
24+
private string $auditId = '12345';
25+
26+
/**
27+
* @test
28+
* @dataProvider isLiveProvider
29+
*/
30+
public function it_can_prepare_request(bool $isLive): void
31+
{
32+
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
33+
$mockedConfig->method('isLive')->willReturn($isLive);
34+
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
35+
$mockedConfig->method('getAppId')->willReturn($this->appId);
36+
$mockedConfig->method('getClientSecret')->willReturn($this->clientSecret);
37+
$mockedConfig->method('getSubscriptionKey')->willReturn($this->subscriptionKey);
38+
39+
$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
40+
$mockedResponse->method('getStatusCode')->willReturn(200);
41+
$mockedResponse->method('getBody')
42+
->willReturn('{
43+
"accountNumber": "123456789",
44+
"accountCurrency": "USD",
45+
"clearedBalance": 1000000000.00,
46+
"unclearedBalance": 1000000000.00,
47+
"errorCode": 0,
48+
"message": "Success - Approved or successfully processed",
49+
"success": true
50+
}');
51+
52+
/** @var \Mockery\MockInterface $mockedClient */
53+
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
54+
$mockedClient->shouldReceive('request')->withArgs([
55+
'POST',
56+
'https://api.example/getAccountBalance',
57+
[
58+
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
59+
\GuzzleHttp\RequestOptions::HEADERS => [
60+
'Accept' => 'application/json',
61+
'Authorization' => "Bearer {$this->clientSecret}",
62+
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey,
63+
],
64+
\GuzzleHttp\RequestOptions::JSON => [
65+
'accountNumber' => '123456789',
66+
'auditId' => $this->auditId,
67+
'appId' => $this->appId,
68+
],
69+
],
70+
])->once()->andReturn($mockedResponse);
71+
72+
$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
73+
$mockedCache->method('has')->willReturn(true);
74+
$mockedCache->method('get')->willReturn($this->clientSecret);
75+
76+
/**
77+
* @var ConfigInterface $mockedConfig
78+
* @var \GuzzleHttp\Client $mockedClient
79+
* @var CacheInterface $mockedCache
80+
* */
81+
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
82+
83+
$requestResult = $api->fetchAccountBalanceRaw($this->auditId, '123456789');
84+
85+
$this->assertInstanceOf(ResponseInterface::class, $requestResult);
86+
}
87+
}

tests/FetchAuthTokenRawTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
namespace BrokeYourBike\AccessBank\Tests;
10+
11+
use Psr\SimpleCache\CacheInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
use BrokeYourBike\AccessBank\Interfaces\ConfigInterface;
14+
use BrokeYourBike\AccessBank\Client;
15+
16+
/**
17+
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
18+
*/
19+
class FetchAuthTokenRawTest extends TestCase
20+
{
21+
/**
22+
* @test
23+
* @dataProvider isLiveProvider
24+
*/
25+
public function it_can_prepare_request(bool $isLive): void
26+
{
27+
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
28+
$mockedConfig->method('isLive')->willReturn($isLive);
29+
$mockedConfig->method('getAuthUrl')->willReturn('https://auth.example/');
30+
$mockedConfig->method('getClientId')->willReturn('client-id');
31+
$mockedConfig->method('getClientSecret')->willReturn('super-secret-value');
32+
$mockedConfig->method('getResourceId')->willReturn('resource-id');
33+
34+
$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
35+
$mockedResponse->method('getStatusCode')->willReturn(200);
36+
$mockedResponse->method('getBody')
37+
->willReturn('{
38+
"token_type": "Bearer",
39+
"expires_in": "3599",
40+
"ext_expires_in": "3599",
41+
"expires_on": "1625077289",
42+
"not_before": "1625073389",
43+
"resource": "12345",
44+
"access_token": "super-secure-token"
45+
}');
46+
47+
/** @var \Mockery\MockInterface $mockedClient */
48+
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
49+
$mockedClient->shouldReceive('request')->withArgs([
50+
'POST',
51+
'https://auth.example/',
52+
[
53+
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
54+
\GuzzleHttp\RequestOptions::HEADERS => [
55+
'Accept' => 'application/json',
56+
],
57+
\GuzzleHttp\RequestOptions::FORM_PARAMS => [
58+
'grant_type' => 'client_credentials',
59+
'resource' => 'resource-id',
60+
'client_id' => 'client-id',
61+
'client_secret' => 'super-secret-value',
62+
],
63+
],
64+
])->once()->andReturn($mockedResponse);
65+
66+
$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
67+
68+
/**
69+
* @var ConfigInterface $mockedConfig
70+
* @var \GuzzleHttp\Client $mockedClient
71+
* @var CacheInterface $mockedCache
72+
* */
73+
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
74+
$requestResult = $api->fetchAuthTokenRaw();
75+
76+
$this->assertInstanceOf(ResponseInterface::class, $requestResult);
77+
}
78+
}

0 commit comments

Comments
 (0)