Skip to content

Commit

Permalink
fix: no need for country
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Feb 8, 2024
1 parent 704f06b commit c40a8d6
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function transferCash(TransactionInterface $transaction): TransferCashRes
return new TransferCashResponse($response);
}

public function cashStatus(string $transactionId, string $country): TransferCashStatusResponse
public function transferCashStatus(string $transactionId): TransferCashStatusResponse
{
$options = [
\GuzzleHttp\RequestOptions::HEADERS => [
Expand All @@ -150,7 +150,7 @@ public function cashStatus(string $transactionId, string $country): TransferCash
'agent_msisdn' => $this->config->getAgentMsisdn(),
'agent_pin' => $this->config->getAgentPin(),
'transaction_id' => $transactionId,
'country' => $country,
'country' => $this->config->getCountry(),
],
];

Expand Down
3 changes: 3 additions & 0 deletions src/Responses/TransferCashStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
class TransferCashStatusResponse extends JsonResponse
{
public ?string $code;
public ?string $error;

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

Expand Down
76 changes: 76 additions & 0 deletions tests/FetchAuthTokenRawTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

// Copyright (C) 2024 Ivan Stasiuk <ivan@stasi.uk>.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

namespace BrokeYourBike\Wizall\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\Wizall\Responses\TokenResponse;
use BrokeYourBike\Wizall\Interfaces\ConfigInterface;
use BrokeYourBike\Wizall\Client;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
class FetchAuthTokenRawTest extends TestCase
{
/** @test */
public function it_can_prepare_request(): void
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://auth.example/');
$mockedConfig->method('getClientId')->willReturn('client-id');
$mockedConfig->method('getClientSecret')->willReturn('super-secret-value');
$mockedConfig->method('getUsername')->willReturn('john');
$mockedConfig->method('getPassword')->willReturn('p@ssword');
$mockedConfig->method('getCountry')->willReturn('SN');

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"access_token": "token123",
"expires_in": 86400,
"token_type": "Bearer",
"country": "SN"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://auth.example/token/',
[
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
],
\GuzzleHttp\RequestOptions::JSON => [
'grant_type' => 'password',
'country' => 'SN',
'client_id' => 'client-id',
'client_secret' => 'super-secret-value',
'client_type' => 'client_type',
'username' => 'john',
'password' => 'p@ssword',
],
],
])->once()->andReturn($mockedResponse);

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();

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

$this->assertInstanceOf(TokenResponse::class, $requestResult);
$this->assertEquals('token123', $requestResult->access_token);
$this->assertEquals(86400, $requestResult->expires_in);
}
}
89 changes: 89 additions & 0 deletions tests/GetAuthTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

// Copyright (C) 2024 Ivan Stasiuk <ivan@stasi.uk>.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

namespace BrokeYourBike\Wizall\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\Wizall\Interfaces\ConfigInterface;
use BrokeYourBike\Wizall\Client;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
class GetAuthTokenTest extends TestCase
{
private string $tokenValue = 'super-secure-token';

/** @test */
public function it_can_cache_and_return_auth_token()
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"expires_in": "3600",
"access_token": "' . $this->tokenValue . '"
}');

/** @var \GuzzleHttp\Client $mockedClient */
$mockedClient = $this->createPartialMock(\GuzzleHttp\Client::class, ['request']);
$mockedClient->method('request')->willReturn($mockedResponse);

/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::spy(CacheInterface::class);

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

$this->assertSame($this->tokenValue, $requestResult);

/** @var \Mockery\MockInterface $mockedCache */
$mockedCache->shouldHaveReceived('set')
->once()
->with(
$api->authTokenCacheKey(),
$this->tokenValue,
1800
);
}

/** @test */
public function it_can_return_cached_value()
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldNotReceive('request');

/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::mock(CacheInterface::class);
$mockedCache->shouldReceive('has')
->once()
->andReturn(true);
$mockedCache->shouldReceive('get')
->once()
->andReturn($this->tokenValue);

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

$this->assertSame($this->tokenValue, $requestResult);
}
}
113 changes: 113 additions & 0 deletions tests/TransferCashStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

// Copyright (C) 2024 Ivan Stasiuk <ivan@stasi.uk>.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

namespace BrokeYourBike\Wizall\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\Wizall\Responses\TransferCashStatusResponse;
use BrokeYourBike\Wizall\Responses\TransferCashResponse;
use BrokeYourBike\Wizall\Interfaces\TransactionInterface;
use BrokeYourBike\Wizall\Interfaces\ConfigInterface;
use BrokeYourBike\Wizall\Enums\TransactionStatusEnum;
use BrokeYourBike\Wizall\Client;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
class TransferCashStatusTest extends TestCase
{
/** @test */
public function it_can_prepare_request(): void
{
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();

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

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getAgentMsisdn')->willReturn('123456789');
$mockedConfig->method('getAgentPin')->willReturn('0088');

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"Operation": "Check_status_bon_cash",
"transactionid": "274126155",
"SenderPhone": "0182491234",
"Amount": "100.000000",
"ReedemWalletType": "",
"ReedemTransactionID": "",
"ReedemPhone": "",
"ReceiverPhone": "0182491234",
"TokenStatus": "Initiated",
"ReedemTime": "",
"code": "200"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->once()->andReturn($mockedResponse);

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

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

$requestResult = $api->transferCashStatus('274126155');
$this->assertInstanceOf(TransferCashStatusResponse::class, $requestResult);
$this->assertEquals(TransactionStatusEnum::INITIATED->value, $requestResult->status);
}

/** @test */
public function it_can_handle_failure(): void
{
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();

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

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getAgentMsisdn')->willReturn('123456789');
$mockedConfig->method('getAgentPin')->willReturn('0088');

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(400);
$mockedResponse->method('getBody')
->willReturn('{
"code": 400,
"error": "transaction_id not valide"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->once()->andReturn($mockedResponse);

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

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

$requestResult = $api->transferCashStatus('274126155');
$this->assertInstanceOf(TransferCashStatusResponse::class, $requestResult);
$this->assertEquals('transaction_id not valide', $requestResult->error);
}
}
Loading

0 comments on commit c40a8d6

Please sign in to comment.