-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
704f06b
commit c40a8d6
Showing
6 changed files
with
408 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.