From 5e954cc84e676dd94f08989c3e95c0675adcd314 Mon Sep 17 00:00:00 2001 From: Ivan Stasiuk Date: Thu, 22 Feb 2024 22:38:01 +0000 Subject: [PATCH] fix: test --- ...okenRawTest.php => FetchAuthTokenTest.php} | 0 ...alanceRawTest.php => FetchBalanceTest.php} | 0 ...wTest.php => FetchDomesticAccountTest.php} | 0 tests/FetchDomesticTransactionTest.php | 1 + ...onRawTest.php => FetchTransactionTest.php} | 1 + tests/SendDomesticTransactionTest.php | 1 + tests/SendOtherBankTransactionTest.php | 94 +++++++++++++++++++ tests/SendTransactionTest.php | 92 ++++++++++++++++++ 8 files changed, 189 insertions(+) rename tests/{FetchAuthTokenRawTest.php => FetchAuthTokenTest.php} (100%) rename tests/{FetchBalanceRawTest.php => FetchBalanceTest.php} (100%) rename tests/{FetchDomesticAccountRawTest.php => FetchDomesticAccountTest.php} (100%) rename tests/{FetchTransactionRawTest.php => FetchTransactionTest.php} (98%) create mode 100644 tests/SendOtherBankTransactionTest.php create mode 100644 tests/SendTransactionTest.php diff --git a/tests/FetchAuthTokenRawTest.php b/tests/FetchAuthTokenTest.php similarity index 100% rename from tests/FetchAuthTokenRawTest.php rename to tests/FetchAuthTokenTest.php diff --git a/tests/FetchBalanceRawTest.php b/tests/FetchBalanceTest.php similarity index 100% rename from tests/FetchBalanceRawTest.php rename to tests/FetchBalanceTest.php diff --git a/tests/FetchDomesticAccountRawTest.php b/tests/FetchDomesticAccountTest.php similarity index 100% rename from tests/FetchDomesticAccountRawTest.php rename to tests/FetchDomesticAccountTest.php diff --git a/tests/FetchDomesticTransactionTest.php b/tests/FetchDomesticTransactionTest.php index 1480f50..8850afe 100644 --- a/tests/FetchDomesticTransactionTest.php +++ b/tests/FetchDomesticTransactionTest.php @@ -79,5 +79,6 @@ public function it_can_prepare_request(): void $requestResult = $api->fetchDomesticTransaction($this->transactionReference); $this->assertInstanceOf(FetchDomesticTransactionResponse::class, $requestResult); + $this->assertFalse($requestResult->paid()); } } diff --git a/tests/FetchTransactionRawTest.php b/tests/FetchTransactionTest.php similarity index 98% rename from tests/FetchTransactionRawTest.php rename to tests/FetchTransactionTest.php index 0ac49f5..6576fe3 100644 --- a/tests/FetchTransactionRawTest.php +++ b/tests/FetchTransactionTest.php @@ -69,5 +69,6 @@ public function it_can_prepare_request(): void $requestResult = $api->fetchTransaction($this->transactionReference); $this->assertInstanceOf(FetchTransactionResponse::class, $requestResult); + $this->assertFalse($requestResult->paid()); } } diff --git a/tests/SendDomesticTransactionTest.php b/tests/SendDomesticTransactionTest.php index c7b1794..adaa4a6 100644 --- a/tests/SendDomesticTransactionTest.php +++ b/tests/SendDomesticTransactionTest.php @@ -92,5 +92,6 @@ public function it_can_prepare_request(): void $requestResult = $api->sendDomesticTransaction($transaction); $this->assertInstanceOf(SendTransactionResponse::class, $requestResult); + $this->assertFalse($requestResult->paid()); } } diff --git a/tests/SendOtherBankTransactionTest.php b/tests/SendOtherBankTransactionTest.php new file mode 100644 index 0000000..2aff226 --- /dev/null +++ b/tests/SendOtherBankTransactionTest.php @@ -0,0 +1,94 @@ +. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +namespace BrokeYourBike\ZenithBank\Tests; + +use Psr\SimpleCache\CacheInterface; +use Psr\Http\Message\ResponseInterface; +use BrokeYourBike\ZenithBank\Models\SendTransactionResponse; +use BrokeYourBike\ZenithBank\Interfaces\TransactionInterface; +use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class SendOtherBankTransactionTest extends TestCase +{ + private string $authToken = 'secure-token'; + + /** @test */ + public function it_can_prepare_request(): void + { + $transaction = $this->getMockBuilder(TransactionInterface::class)->getMock(); + $transaction->method('getReference')->willReturn('REF-1234'); + $transaction->method('getRecipientAccount')->willReturn('556890'); + $transaction->method('getRecipientBankCode')->willReturn('B123'); + $transaction->method('getDebitAccount')->willReturn('448000'); + $transaction->method('getAmount')->willReturn(100.01); + + /** @var TransactionInterface $transaction */ + $this->assertInstanceOf(TransactionInterface::class, $transaction); + + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + $mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock(); + $mockedResponse->method('getStatusCode')->willReturn(200); + $mockedResponse->method('getBody') + ->willReturn('{ + "responseCode": "01", + "responseDescription": "DUPLICATE TRANSACTION REFERENCE", + "description": "Suspected Duplicate with transaction Reference 405", + "transactionReference": "405", + "posted": "N", + "transactionStatus": null, + "postingDate": null, + "postingReference": null + }'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/transaction/otherBankTransfer', + [ + + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'amount' => 100.01, + 'bankCode' => 'B123', + 'bankName' => 'zenith', + 'crAccount' => '556890', + 'drAccount' => '448000', + 'transactionReference' => 'REF-1234', + 'description' => 'REF-1234', + ], + ], + ])->once()->andReturn($mockedResponse); + + $mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock(); + $mockedCache->method('has')->willReturn(true); + $mockedCache->method('get')->willReturn($this->authToken); + + /** + * @var ConfigInterface $mockedConfig + * @var \GuzzleHttp\Client $mockedClient + * @var CacheInterface $mockedCache + * */ + $api = new Client($mockedConfig, $mockedClient, $mockedCache); + + $requestResult = $api->sendOtherBankTransaction($transaction); + + $this->assertInstanceOf(SendTransactionResponse::class, $requestResult); + $this->assertFalse($requestResult->paid()); + } +} diff --git a/tests/SendTransactionTest.php b/tests/SendTransactionTest.php new file mode 100644 index 0000000..76fb3ad --- /dev/null +++ b/tests/SendTransactionTest.php @@ -0,0 +1,92 @@ +. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +namespace BrokeYourBike\ZenithBank\Tests; + +use Psr\SimpleCache\CacheInterface; +use Psr\Http\Message\ResponseInterface; +use BrokeYourBike\ZenithBank\Models\SendTransactionResponse; +use BrokeYourBike\ZenithBank\Interfaces\TransactionInterface; +use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class SendTransactionTest extends TestCase +{ + private string $authToken = 'secure-token'; + + /** @test */ + public function it_can_prepare_request(): void + { + $transaction = $this->getMockBuilder(TransactionInterface::class)->getMock(); + $transaction->method('getReference')->willReturn('REF-1234'); + $transaction->method('getRecipientAccount')->willReturn('556890'); + $transaction->method('getDebitAccount')->willReturn('448000'); + $transaction->method('getAmount')->willReturn(100.01); + + /** @var TransactionInterface $transaction */ + $this->assertInstanceOf(TransactionInterface::class, $transaction); + + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + $mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock(); + $mockedResponse->method('getStatusCode')->willReturn(200); + $mockedResponse->method('getBody') + ->willReturn('{ + "responseCode": "01", + "responseDescription": "DUPLICATE TRANSACTION REFERENCE", + "description": "Suspected Duplicate with transaction Reference 405", + "transactionReference": "405", + "posted": "N", + "transactionStatus": null, + "postingDate": null, + "postingReference": null + }'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/transaction/zenithTransfer', + [ + + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'amount' => 100.01, + 'bankName' => 'zenith', + 'crAccount' => '556890', + 'drAccount' => '448000', + 'transactionReference' => 'REF-1234', + 'description' => 'REF-1234', + ], + ], + ])->once()->andReturn($mockedResponse); + + $mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock(); + $mockedCache->method('has')->willReturn(true); + $mockedCache->method('get')->willReturn($this->authToken); + + /** + * @var ConfigInterface $mockedConfig + * @var \GuzzleHttp\Client $mockedClient + * @var CacheInterface $mockedCache + * */ + $api = new Client($mockedConfig, $mockedClient, $mockedCache); + + $requestResult = $api->sendTransaction($transaction); + + $this->assertInstanceOf(SendTransactionResponse::class, $requestResult); + $this->assertFalse($requestResult->paid()); + } +}