Skip to content

Commit

Permalink
fix!: it can handle error response
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Feb 8, 2022
1 parent 0168aa7 commit 8cb7289
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 24 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ jobs:
timeout-minutes: 5

steps:
- name: Checkout the code
uses: actions/checkout@v2

- uses: google-github-actions/release-please-action@v2
with:
release-type: php
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
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/[email protected]
continue-on-error: true
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
with:
Expand Down
14 changes: 10 additions & 4 deletions src/Models/AccountInformationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
*/
class AccountInformationResponse extends JsonResponse
{
#[MapFrom('ErrorCode')]
public ?string $errorCode;

#[MapFrom('ErrorDescription')]
public ?string $errorDescription;

#[MapFrom('UBATransactionId')]
public string $requestId;
public ?string $requestId;

#[MapFrom('accountInformation.responseCode')]
public string $responseCode;
public ?string $responseCode;

#[MapFrom('accountInformation.responseMessage')]
public string $responseMessage;
public ?string $responseMessage;

#[MapFrom('accountInformation.balanceCurrency')]
public string $balanceCurrency;
public ?string $balanceCurrency;

#[MapFrom('accountInformation.accountName')]
public ?string $accountName;
Expand Down
14 changes: 10 additions & 4 deletions src/Models/CreditTransactionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
*/
class CreditTransactionResponse extends JsonResponse
{
#[MapFrom('ErrorCode')]
public ?string $errorCode;

#[MapFrom('ErrorDescription')]
public ?string $errorDescription;

#[MapFrom('UBATransactionId')]
public string $requestId;
public ?string $requestId;

#[MapFrom('transactionId')]
public string $transactionId;
public ?string $transactionId;

#[MapFrom('hsTransactionId')]
public string $reference;
public ?string $reference;

#[MapFrom('provisionalResponse.state')]
public string $state;
public ?string $state;

#[MapFrom('provisionalResponse.label')]
public ?string $stateLabel;
Expand Down
8 changes: 7 additions & 1 deletion src/Models/TransactionStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
*/
class TransactionStatusResponse extends JsonResponse
{
#[MapFrom('ErrorCode')]
public ?string $errorCode;

#[MapFrom('ErrorDescription')]
public ?string $errorDescription;

#[MapFrom('status.state')]
public string $state;
public ?string $state;

#[MapFrom('status.stateLabel')]
public ?string $stateLabel;
Expand Down
85 changes: 85 additions & 0 deletions tests/CreditTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,89 @@ public function it_can_prepare_request(): void

$this->assertInstanceOf(CreditTransactionResponse::class, $requestResult);
}

/** @test */
public function it_can_handle_error_response()
{
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();
$transaction->method('getDestinationId')->willReturn('TRX1234');
$transaction->method('getReference')->willReturn('REF-5554');
$transaction->method('getSourceSwiftCode')->willReturn('source-1234');
$transaction->method('getDestinationAccountNumber')->willReturn('001234');
$transaction->method('getDestinationSwiftCode')->willReturn('dest-1234');
$transaction->method('getRoutingTag')->willReturn('route-66');
$transaction->method('getSenderName')->willReturn('John Doe');
$transaction->method('getCurrencyCode')->willReturn('NGN');
$transaction->method('getAmount')->willReturn(10.0);

/** @var TransactionInterface $transaction */
$this->assertInstanceOf(TransactionInterface::class, $transaction);

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getToken')->willReturn($this->authToken);
$mockedConfig->method('getClientId')->willReturn($this->clientId);
$mockedConfig->method('getClientName')->willReturn($this->clientName);
$mockedConfig->method('getUsername')->willReturn($this->username);
$mockedConfig->method('getPassword')->willReturn($this->password);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"ErrorCode": "999",
"ErrorDescription": "Invalid Credentials"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/credit/v1.0',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->authToken}",
],
\GuzzleHttp\RequestOptions::JSON => [
'Security' => [
'login' => $this->username,
'Password' => $this->password,
],
'transactionId' => 'TRX1234',
'hstransactionId' => 'REF-5554',
'Stan' => 'REF-5554',
'sourceUri' => "bic=source-1234",
'destinationUri' => "ban:001234;bic=dest-1234",
'amountInformation' => [
'currency' => 'NGN',
'amount' => (string) 10.0,
],
'senderInformation' => [
'Name' => 'John Doe',
],
'routingTag' => 'route-66',
'description' => '',
'vendorSpecificFields' => [
'ClientId' => $this->clientId,
'ClientName' => $this->clientName,
'BenefName' => '',
],
],
],
])->once()->andReturn($mockedResponse);

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

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

$this->assertInstanceOf(CreditTransactionResponse::class, $requestResult);
$this->assertSame('999', $requestResult->errorCode);
$this->assertSame('Invalid Credentials', $requestResult->errorDescription);
}
}
68 changes: 68 additions & 0 deletions tests/FetchAccountInformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,72 @@ public function it_can_prepare_request(): void

$this->assertInstanceOf(AccountInformationResponse::class, $requestResult);
}

/** @test */
public function it_can_handle_error_response()
{
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();
$transaction->method('getSourceSwiftCode')->willReturn($this->sourceSwiftCode);
$transaction->method('getDestinationAccountNumber')->willReturn($this->destinationAccountNumber);
$transaction->method('getDestinationSwiftCode')->willReturn($this->destinationSwiftCode);
$transaction->method('getRoutingTag')->willReturn($this->routingTag);

/** @var TransactionInterface $transaction */
$this->assertInstanceOf(TransactionInterface::class, $transaction);

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getToken')->willReturn($this->authToken);
$mockedConfig->method('getClientId')->willReturn($this->clientId);
$mockedConfig->method('getClientName')->willReturn($this->clientName);
$mockedConfig->method('getUsername')->willReturn($this->username);
$mockedConfig->method('getPassword')->willReturn($this->password);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"ErrorCode": "999",
"ErrorDescription": "Invalid Credentials"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/accountinformation/v1.0',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->authToken}",
],
\GuzzleHttp\RequestOptions::JSON => [
'Security' => [
'login' => $this->username,
'Password' => $this->password,
],
'sourceUri' => "bic={$this->sourceSwiftCode}",
'destinationUri' => "ban:{$this->destinationAccountNumber};bic={$this->destinationSwiftCode}",
'routingTag' => $this->routingTag,
'vendorSpecificFields' => [
'ClientId' => $this->clientId,
'ClientName' => $this->clientName,
],
],
],
])->once()->andReturn($mockedResponse);

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

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

$this->assertInstanceOf(AccountInformationResponse::class, $requestResult);
$this->assertSame('999', $requestResult->errorCode);
$this->assertSame('Invalid Credentials', $requestResult->errorDescription);
}
}
20 changes: 8 additions & 12 deletions tests/FetchTransactionStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function it_can_prepare_request(): void
}

/** @test */
public function it_will_pass_source_model_as_option(): void
public function it_can_handle_error_response()
{
$transaction = $this->getMockBuilder(SourceTransactionFixture::class)->getMock();
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();
$transaction->method('getDestinationId')->willReturn($this->destinationId);

/** @var SourceTransactionFixture $transaction */
$this->assertInstanceOf(SourceTransactionFixture::class, $transaction);
/** @var TransactionInterface $transaction */
$this->assertInstanceOf(TransactionInterface::class, $transaction);

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
Expand All @@ -114,13 +114,8 @@ public function it_will_pass_source_model_as_option(): void
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"status": {
"transactionId": "'. $this->destinationId .'",
"hsTransactionId": "234123412341",
"state": "Success",
"stateLabel": "SUCCESS",
"statusCode": "000"
}
"ErrorCode": "999",
"ErrorDescription": "Invalid Credentials"
}');

/** @var \Mockery\MockInterface $mockedClient */
Expand All @@ -145,7 +140,6 @@ public function it_will_pass_source_model_as_option(): void
'ClientName' => $this->clientName,
],
],
\BrokeYourBike\HasSourceModel\Enums\RequestOptions::SOURCE_MODEL => $transaction,
],
])->once()->andReturn($mockedResponse);

Expand All @@ -158,5 +152,7 @@ public function it_will_pass_source_model_as_option(): void
$requestResult = $api->fetchTransactionStatus($transaction);

$this->assertInstanceOf(TransactionStatusResponse::class, $requestResult);
$this->assertSame('999', $requestResult->errorCode);
$this->assertSame('Invalid Credentials', $requestResult->errorDescription);
}
}

0 comments on commit 8cb7289

Please sign in to comment.