diff --git a/src/Client.php b/src/Client.php index afd7275..ab8a45b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -98,4 +98,91 @@ public function fetchAuthTokenRaw(): ResponseInterface $options ); } + + public function fetchBalanceRaw(string $accountNumber): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/balance', [ + 'accountNumber' => $accountNumber, + ]); + } + + public function fetchAccountRaw(string $bankCode, string $accountNumber): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/accountEnquiry', [ + 'destinationBankCode' => $bankCode, + 'accountNumber' => $accountNumber, + ]); + } + + public function fetchDomesticAccountRaw(string $accountNumber): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/domAccountEnquiry', [ + 'accountNumber' => $accountNumber, + ]); + } + + public function fetchDomesticTransactionRaw(string $reference): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/domTransaction', [ + 'transactionReference' => $reference, + ]); + } + + public function sendDomesticTransaction(TransactionInterface $transaction): ResponseInterface + { + if ($transaction instanceof SourceModelInterface) { + $this->setSourceModel($transaction); + } + + return $this->performRequest(HttpMethodEnum::POST(), 'api/transaction/zenithDomTransfer', [ + 'transactionReference' => $transaction->getReference(), + 'paymentReference' => $transaction->getReference(), + 'senderName' => $transaction->getSenderName(), + 'beneficiaryName' => $transaction->getRecipientName(), + 'crAccount' => $transaction->getRecipientAccount(), + 'drAccount' => $transaction->getDebitAccount(), + 'amount' => $transaction->getAmount(), + 'resend' => $transaction->shouldResend(), + ]); + } + + public function fetchTransactionRaw(string $reference): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/transaction', [ + 'transactionReference' => $reference, + ]); + } + + public function transactionLookupRaw(string $accountNumber, \DateTime $transactionDate): ResponseInterface + { + return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/transactionLookup', [ + 'accountNumber' => $accountNumber, + 'transactionDate' => $transactionDate->format('Y-m-d'), + ]); + } + + /** + * @param HttpMethodEnum $method + * @param string $uri + * @param array $data + * @return ResponseInterface + */ + private function performRequest(HttpMethodEnum $method, string $uri, array $data): ResponseInterface + { + $options = [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . (string) $this->getAuthToken(), + ], + \GuzzleHttp\RequestOptions::JSON => $data, + ]; + + if ($this->getSourceModel()) { + $options[\BrokeYourBike\HasSourceModel\Enums\RequestOptions::SOURCE_MODEL] = $this->getSourceModel(); + } + + $uri = (string) $this->resolveUriFor($this->config->getUrl(), $uri); + return $this->httpClient->request((string) $method, $uri, $options); + } } diff --git a/src/Interfaces/TransactionInterface.php b/src/Interfaces/TransactionInterface.php new file mode 100644 index 0000000..cff0478 --- /dev/null +++ b/src/Interfaces/TransactionInterface.php @@ -0,0 +1,23 @@ +. +// +// 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\Interfaces; + +/** + * @author Ivan Stasiuk + */ +interface TransactionInterface +{ + public function getReference(): string; + public function getSenderName(): string; + public function getRecipientName(): string; + public function getRecipientAccount(): string; + public function getDebitAccount(): string; + public function getAmount(): float; + public function shouldResend(): bool; +} diff --git a/tests/FetchAccountRawTest.php b/tests/FetchAccountRawTest.php new file mode 100644 index 0000000..d65dbf1 --- /dev/null +++ b/tests/FetchAccountRawTest.php @@ -0,0 +1,68 @@ +. +// +// 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\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class FetchAccountRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $bankCode = '12345'; + private string $accountNumber = '654987'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/accountEnquiry', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'destinationBankCode' => $this->bankCode, + 'accountNumber' => $this->accountNumber, + ], + ], + ])->once(); + + $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->fetchAccountRaw($this->bankCode, $this->accountNumber); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/FetchAuthTokenRawTest.php b/tests/FetchAuthTokenRawTest.php index 84b47ac..df18d3d 100644 --- a/tests/FetchAuthTokenRawTest.php +++ b/tests/FetchAuthTokenRawTest.php @@ -33,10 +33,6 @@ public function it_can_prepare_request(bool $isLive): void $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('{}'); - /** @var \Mockery\MockInterface $mockedClient */ $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); $mockedClient->shouldReceive('request')->withArgs([ @@ -52,7 +48,7 @@ public function it_can_prepare_request(bool $isLive): void 'userProtector' => $this->password, ], ], - ])->once()->andReturn($mockedResponse); + ])->once(); $mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock(); diff --git a/tests/FetchBalanceRawTest.php b/tests/FetchBalanceRawTest.php new file mode 100644 index 0000000..bf2d6de --- /dev/null +++ b/tests/FetchBalanceRawTest.php @@ -0,0 +1,66 @@ +. +// +// 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\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class FetchBalanceRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $accountNumber = '654987'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/balance', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'accountNumber' => $this->accountNumber, + ], + ], + ])->once(); + + $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->fetchBalanceRaw($this->accountNumber); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/FetchDomesticAccountRawTest.php b/tests/FetchDomesticAccountRawTest.php new file mode 100644 index 0000000..775257a --- /dev/null +++ b/tests/FetchDomesticAccountRawTest.php @@ -0,0 +1,66 @@ +. +// +// 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\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class FetchDomesticAccountRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $accountNumber = '654987'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/domAccountEnquiry', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'accountNumber' => $this->accountNumber, + ], + ], + ])->once(); + + $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->fetchDomesticAccountRaw($this->accountNumber); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/FetchDomesticTransactionRawTest.php b/tests/FetchDomesticTransactionRawTest.php new file mode 100644 index 0000000..4227c5a --- /dev/null +++ b/tests/FetchDomesticTransactionRawTest.php @@ -0,0 +1,66 @@ +. +// +// 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\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class FetchDomesticTransactionRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $transactionReference = 'TRX-1234'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/domTransaction', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'transactionReference' => $this->transactionReference, + ], + ], + ])->once(); + + $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->fetchDomesticTransactionRaw($this->transactionReference); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/FetchTransactionRawTest.php b/tests/FetchTransactionRawTest.php new file mode 100644 index 0000000..0a49550 --- /dev/null +++ b/tests/FetchTransactionRawTest.php @@ -0,0 +1,66 @@ +. +// +// 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\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class FetchTransactionRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $transactionReference = 'TRX-1234'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/transaction', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'transactionReference' => $this->transactionReference, + ], + ], + ])->once(); + + $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->fetchTransactionRaw($this->transactionReference); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/SendDomesticTransactionTest.php b/tests/SendDomesticTransactionTest.php new file mode 100644 index 0000000..3300ba0 --- /dev/null +++ b/tests/SendDomesticTransactionTest.php @@ -0,0 +1,139 @@ +. +// +// 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\Interfaces\TransactionInterface; +use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class SendDomesticTransactionTest extends TestCase +{ + private string $authToken = 'secure-token'; + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $transaction = $this->getMockBuilder(TransactionInterface::class)->getMock(); + $transaction->method('getReference')->willReturn('REF-1234'); + $transaction->method('getSenderName')->willReturn('John Doe'); + $transaction->method('getRecipientName')->willReturn('Jane Doe'); + $transaction->method('getRecipientAccount')->willReturn('556890'); + $transaction->method('getDebitAccount')->willReturn('448000'); + $transaction->method('getAmount')->willReturn(100.01); + $transaction->method('shouldResend')->willReturn(false); + + /** @var TransactionInterface $transaction */ + $this->assertInstanceOf(TransactionInterface::class, $transaction); + + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/transaction/zenithDomTransfer', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'transactionReference' => 'REF-1234', + 'paymentReference' => 'REF-1234', + 'senderName' => 'John Doe', + 'beneficiaryName' => 'Jane Doe', + 'crAccount' => '556890', + 'drAccount' => '448000', + 'amount' => 100.01, + 'resend' => false, + ], + ], + ])->once(); + + $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->sendDomesticTransaction($transaction); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_will_pass_source_model_as_option(bool $isLive): void + { + /** @var SourceTransactionFixture $transaction */ + $transaction = $this->getMockBuilder(SourceTransactionFixture::class)->getMock(); + + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/transaction/zenithDomTransfer', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'transactionReference' => $transaction->getReference(), + 'paymentReference' => $transaction->getReference(), + 'senderName' => $transaction->getSenderName(), + 'beneficiaryName' => $transaction->getRecipientName(), + 'crAccount' => $transaction->getRecipientAccount(), + 'drAccount' => $transaction->getDebitAccount(), + 'amount' => $transaction->getAmount(), + 'resend' => $transaction->shouldResend(), + ], + \BrokeYourBike\HasSourceModel\Enums\RequestOptions::SOURCE_MODEL => $transaction, + ], + ])->once(); + + $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->sendDomesticTransaction($transaction); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +} diff --git a/tests/SourceTransactionFixture.php b/tests/SourceTransactionFixture.php new file mode 100644 index 0000000..c25fa7e --- /dev/null +++ b/tests/SourceTransactionFixture.php @@ -0,0 +1,18 @@ +. +// +// 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 BrokeYourBike\ZenithBank\Interfaces\TransactionInterface; +use BrokeYourBike\HasSourceModel\SourceModelInterface; + +/** + * @author Ivan Stasiuk + */ +abstract class SourceTransactionFixture implements TransactionInterface, SourceModelInterface +{} diff --git a/tests/TransactionLookupRawTest.php b/tests/TransactionLookupRawTest.php new file mode 100644 index 0000000..6beb3d0 --- /dev/null +++ b/tests/TransactionLookupRawTest.php @@ -0,0 +1,77 @@ +. +// +// 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 Carbon\Carbon; +use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface; +use BrokeYourBike\ZenithBank\Client; + +/** + * @author Ivan Stasiuk + */ +class TransactionLookupRawTest extends TestCase +{ + private string $authToken = 'secure-token'; + private string $accountNumber = '123456'; + private \DateTime $transactionDate; + + /** + * This method is called before each test. + */ + protected function setUp(): void + { + $this->transactionDate = Carbon::create(2020, 1, 5, 23, 30, 59); + } + + /** + * @test + * @dataProvider isLiveProvider + */ + public function it_can_prepare_request(bool $isLive): void + { + $mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock(); + $mockedConfig->method('isLive')->willReturn($isLive); + $mockedConfig->method('getUrl')->willReturn('https://api.example/'); + + /** @var \Mockery\MockInterface $mockedClient */ + $mockedClient = \Mockery::mock(\GuzzleHttp\Client::class); + $mockedClient->shouldReceive('request')->withArgs([ + 'POST', + 'https://api.example/api/enquiry/transactionLookup', + [ + \GuzzleHttp\RequestOptions::HTTP_ERRORS => false, + \GuzzleHttp\RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => "Bearer {$this->authToken}", + ], + \GuzzleHttp\RequestOptions::JSON => [ + 'accountNumber' => $this->accountNumber, + 'transactionDate' => '2020-01-05', + ], + ], + ])->once(); + + $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->transactionLookupRaw($this->accountNumber, $this->transactionDate); + + $this->assertInstanceOf(ResponseInterface::class, $requestResult); + } +}