Skip to content

Commit

Permalink
feat!: use response model
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Jan 2, 2022
1 parent 40c41cb commit 4e832af
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 71 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"php": "^8.1",
"ext-json": "*",
"brokeyourbike/http-client": "^1.0",
"brokeyourbike/resolve-uri": "^1.0"
"brokeyourbike/resolve-uri": "^1.0",
"brokeyourbike/data-transfer-object": "^0.1.1"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
parameters:
checkMissingIterableValueType: false
level: max
paths:
- src
14 changes: 10 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace BrokeYourBike\Paystack;

use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\Paystack\ResolveAccountNumberResponse;
use BrokeYourBike\Paystack\ConfigInterface;
use BrokeYourBike\HttpClient\HttpClientTrait;
use BrokeYourBike\HttpClient\HttpClientInterface;
Expand All @@ -30,10 +30,14 @@ public function __construct(ConfigInterface $config, \GuzzleHttp\ClientInterface
$this->httpClient = $httpClient;
}

public function resolveAccountNumberRaw(string $bankCode, string $accountNumber): ResponseInterface
public function getConfig(): ConfigInterface
{
return $this->config;
}

public function resolveAccountNumberRaw(string $bankCode, string $accountNumber): ResolveAccountNumberResponse
{
$options = [
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->config->getSecretKey(),
Expand All @@ -45,6 +49,8 @@ public function resolveAccountNumberRaw(string $bankCode, string $accountNumber)
];

$uri = (string) $this->resolveUriFor($this->config->getUrl(), 'bank/resolve');
return $this->httpClient->request('GET', $uri, $options);
$response = $this->httpClient->request('GET', $uri, $options);

return new ResolveAccountNumberResponse($response);
}
}
27 changes: 27 additions & 0 deletions src/ResolveAccountNumberResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// 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\Paystack;

use Spatie\DataTransferObject\Attributes\MapFrom;
use BrokeYourBike\DataTransferObject\JsonResponse;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class ResolveAccountNumberResponse extends JsonResponse
{
public bool $status;
public string $message;

#[MapFrom('data.account_number')]
public ?string $accountNumber;

#[MapFrom('data.account_name')]
public ?string $accountName;
}
9 changes: 8 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ class ClientTest extends TestCase
/** @test */
public function it_implemets_http_client_interface(): void
{
$api = new Client(new ConfigFixture(), new \GuzzleHttp\Client());
/** @var ConfigInterface */
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();

/** @var \GuzzleHttp\ClientInterface */
$mockedHttpClient = $this->getMockBuilder(\GuzzleHttp\ClientInterface::class)->getMock();

$api = new Client($mockedConfig, $mockedHttpClient);

$this->assertInstanceOf(HttpClientInterface::class, $api);
$this->assertSame($mockedConfig, $api->getConfig());
}

/** @test */
Expand Down
32 changes: 0 additions & 32 deletions tests/ConfigFixture.php

This file was deleted.

24 changes: 0 additions & 24 deletions tests/ConfigTest.php

This file was deleted.

26 changes: 26 additions & 0 deletions tests/ResolveAccountNumberResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// 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\Paystack\Tests;

use PHPUnit\Framework\TestCase;
use BrokeYourBike\Paystack\ResolveAccountNumberResponse;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class ResolveAccountNumberResponseTest extends TestCase
{
/** @test */
public function it_extends_json_response()
{
$parent = get_parent_class(ResolveAccountNumberResponse::class);

$this->assertSame(\BrokeYourBike\DataTransferObject\JsonResponse::class, $parent);
}
}
14 changes: 6 additions & 8 deletions tests/ResolveAccountNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@

namespace BrokeYourBike\Paystack\Tests;

use Psr\Http\Message\UriInterface;
use Psr\Http\Message\ResponseInterface;
use PHPUnit\Framework\TestCase;
use BrokeYourBike\Paystack\ResolveAccountNumberResponse;
use BrokeYourBike\Paystack\ConfigInterface;
use BrokeYourBike\Paystack\Client;

class ResolveAccountNumberTest extends TestCase
{
private string $secretKey = 'super-secure-secret-key';

/**
* @test
*/
public function it_can_prepare_request(): void
{
$secretKey = 'super-secure-secret-key';

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getSecretKey')->willReturn($secretKey);
$mockedConfig->method('getSecretKey')->willReturn($this->secretKey);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
Expand All @@ -46,10 +46,9 @@ public function it_can_prepare_request(): void
'GET',
'https://api.example/bank/resolve',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$secretKey}",
'Authorization' => "Bearer {$this->secretKey}",
],
\GuzzleHttp\RequestOptions::QUERY => [
'account_number' => '123456789',
Expand All @@ -65,7 +64,6 @@ public function it_can_prepare_request(): void
$api = new Client($mockedConfig, $mockedClient);
$requestResult = $api->resolveAccountNumberRaw('111', '123456789');

$this->assertInstanceOf(ResponseInterface::class, $requestResult);
$this->assertSame(200, $requestResult->getStatusCode());
$this->assertInstanceOf(ResolveAccountNumberResponse::class, $requestResult);
}
}

0 comments on commit 4e832af

Please sign in to comment.