Skip to content

Commit

Permalink
fix: transaction can fail
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Feb 4, 2022
1 parent 88ce578 commit 775a1e4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release-please.yml
Expand Up @@ -15,6 +15,3 @@ jobs:
with:
release-type: php
bump-minor-pre-major: true

- uses: actions/checkout@v2
if: ${{ steps.release.outputs.release_created }}
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Expand Up @@ -32,11 +32,13 @@ jobs:

- name: Upload coverage to codecov
uses: codecov/codecov-action@v2
continue-on-error: true
with:
files: ./coverage.xml

- name: Upload coverage to codeclimate
uses: paambaati/codeclimate-action@v3.0.0
continue-on-error: true
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
with:
Expand Down
6 changes: 4 additions & 2 deletions src/Models/TransactionResponse.php
Expand Up @@ -16,9 +16,11 @@
*/
class TransactionResponse extends JsonResponse
{
public bool $success;
public string $errorCode;
public string $message;
public ?bool $success;
public ?string $errorCode;
public ?string $statusCode;
public ?string $activityId;

#[MapFrom('payment.transactionId')]
public ?string $transactionId;
Expand Down
64 changes: 64 additions & 0 deletions tests/SendDomesticTransactionTest.php
Expand Up @@ -89,6 +89,70 @@ public function it_can_prepare_request(): void

$this->assertInstanceOf(TransactionResponse::class, $requestResult);
$this->assertSame('BANKAPI123456789', $requestResult->transactionId);
$this->assertSame('0', $requestResult->errorCode);
$this->assertSame('1', $requestResult->transactionStatus);
$this->assertSame(true, $requestResult->success);
}

/** @test */
public function it_can_handle_failed_reponse()
{
/** @var BankTransactionInterface $transaction */
$transaction = $this->getMockBuilder(BankTransactionInterface::class)->getMock();

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getAppId')->willReturn($this->appId);
$mockedConfig->method('getClientSecret')->willReturn($this->clientSecret);
$mockedConfig->method('getSubscriptionKey')->willReturn($this->subscriptionKey);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(500);
$mockedResponse->method('getBody')
->willReturn('{
"statusCode": 500,
"message": "Internal server error",
"activityId": "c1f6bbd9-096f-4c5f-b04f-fbfae795b0ea"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/bankAccountFT',
[
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->clientSecret}",
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey,
],
\GuzzleHttp\RequestOptions::JSON => [
'debitAccount' => $transaction->getDebitAccount(),
'beneficiaryAccount' => $transaction->getRecipientAccount(),
'beneficiaryName' => $transaction->getRecipientName(),
'amount' => $transaction->getAmount(),
'currency' => $transaction->getCurrencyCode(),
'narration' => $transaction->getDescription(),
'auditId' => $transaction->getReference(),
'appId' => $this->appId,
],
],
])->once()->andReturn($mockedResponse);

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
$mockedCache->method('has')->willReturn(true);
$mockedCache->method('get')->willReturn($this->clientSecret);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);

$requestResult = $api->sendDomesticTransaction($transaction);

$this->assertInstanceOf(TransactionResponse::class, $requestResult);
$this->assertSame('Internal server error', $requestResult->message);
}
}

0 comments on commit 775a1e4

Please sign in to comment.