Skip to content

Instantly share code, notes, and snippets.

@denrad
Created January 12, 2023 13:22
Show Gist options
  • Select an option

  • Save denrad/adbb10051ea12df1a6212234b66ced7c to your computer and use it in GitHub Desktop.

Select an option

Save denrad/adbb10051ea12df1a6212234b66ced7c to your computer and use it in GitHub Desktop.
<?php
namespace tests\domain\unit\drivers\paymentsystems\connectum;
use domain\drivers\paymentsystems\connectum\Client;
use domain\drivers\paymentsystems\connectum\interfaces\Client as ClientInterface;
use domain\drivers\paymentsystems\connectum\interfaces\Command;
use domain\drivers\paymentsystems\interfaces\CurlWrapper;
use domain\drivers\paymentsystems\interfaces\Logger;
use GuzzleHttp\Psr7\Response;
use tests\components\UnitTestCase;
class ClientTest extends UnitTestCase
{
/**
* @expectedException \Exception
* @expectedExceptionMessage POST request: Request parameters can not be empty.
*/
public function testExecutePostRequestWithoutParams()
{
$methodType = Command::POST;
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getRequestParams')->willReturn([]);
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects($this->never())->method('post');
$logger = $this->createMock(Logger::class);
$logger->expects(self::never())->method('info');
$client = new Client($guzzle, $logger);
$client->execute($command);
}
public function testExecutePostRequestWithRequestParams()
{
$authParams = [
'apiUrl' => '::api url::',
'login' => '::login::',
'password' => '::password::',
'certificateFile' => '::certificate file::',
'certificatePassword' => '::certificate password::',
];
$methodType = Command::POST;
$methodName = '::method name::';
$requestParams = ['::request params::'];
$securityParams = ['::security params::'];
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getMethod')->willReturn($methodName);
$command->method('getAuthorizationParams')->willReturn($authParams);
$command->method('getRequestParams')->willReturn($requestParams);
$command->method('getSecurityParams')->willReturn($securityParams);
$response = '::response::';
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::once())
->method('post')
->with(
$authParams['apiUrl'] . $methodName,
[]
)
->willReturn(new Response(200, [], $response));
$logger = $this->createMock(Logger::class);
$logger
->expects(self::exactly(2))
->method('info')
->withConsecutive(
[
array_merge(
['method_type' => $methodType],
['method_name' => $methodName],
$requestParams),
ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM, $securityParams],
[$response, ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM]
);
$client = new Client($guzzle, $logger);
$client->execute($command);
}
public function testExecuteGetRequestWithoutParameters()
{
$authParams = [
'apiUrl' => '::api url::',
'login' => '::login::',
'password' => '::password::',
'certificateFile' => '::certificate file::',
'certificatePassword' => '::certificate password::',
];
$methodType = Command::GET;
$methodName = '::method name::';
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getMethod')->willReturn($methodName);
$command->method('getAuthorizationParams')->willReturn($authParams);
$command->method('getRequestParams')->willReturn([]);
$response = '::response::';
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::once())
->method('get')
->with(
$authParams['apiUrl'] . $methodName,
[]
)->willReturn(new Response(200, [], $response));
$logger = $this->createMock(Logger::class);
$logger
->expects(self::exactly(2))
->method('info')
->withConsecutive(
[
array_merge(
['method_type' => $methodType],
['method_name' => $methodName],
[]),
ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM],
[$response, ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM]
);
$client = new Client($guzzle, $logger);
$client->execute($command);
}
public function testExecuteGetRequestWithParameters()
{
$authParams = [
'apiUrl' => '::api url::',
'login' => '::login::',
'password' => '::password::',
'certificateFile' => '::certificate file::',
'certificatePassword' => '::certificate password::',
];
$methodType = Command::GET;
$methodName = '::method name::';
$requestParams = ['::param1::' => '::value1::', '::param2::' => '::value2::'];
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getMethod')->willReturn($methodName);
$command->method('getAuthorizationParams')->willReturn($authParams);
$command->method('getRequestParams')->willReturn($requestParams);
$response = '::response::';
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::once())
->method('get')
->with(
$authParams['apiUrl'] . $methodName,
['query' => $requestParams]
)->willReturn(new Response(200, [], $response));
$logger = $this->createMock(Logger::class);
$logger
->expects(self::exactly(2))
->method('info')
->withConsecutive(
[
array_merge(
['method_type' => $methodType],
['method_name' => $methodName],
$requestParams),
ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM],
[$response, ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM]
);
$client = new Client($guzzle, $logger);
$client->execute($command);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage PUT request: Request parameters can not be empty.
*/
public function testExecutePutRequestWithoutParams()
{
$methodType = Command::PUT;
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getRequestParams')->willReturn([]);
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::never())->method('put');
$logger = $this->createMock(Logger::class);
$logger->expects(self::never())->method('info');
$client = new Client($guzzle, $logger);
$client->execute($command);
}
public function testExecutePutRequestWithRequestParams()
{
$authParams = [
'apiUrl' => '::api url::',
'login' => '::login::',
'password' => '::password::',
'certificateFile' => '::certificate file::',
'certificatePassword' => '::certificate password::',
];
$methodType = Command::PUT;
$methodName = '::method name::';
$requestParams = ['::request params::'];
$command = $this->createMock(Command::class);
$command->method('getMethodType')->willReturn($methodType);
$command->method('getMethod')->willReturn($methodName);
$command->method('getAuthorizationParams')->willReturn($authParams);
$command->method('getRequestParams')->willReturn($requestParams);
$response = '::response::';
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::once())
->method('put')
->with(
$authParams['apiUrl'] . $methodName,
['body' => json_encode($requestParams)]
)->willReturn(new Response(200, [], $response));
$logger = $this->createMock(Logger::class);
$logger
->expects(self::exactly(2))
->method('info')
->withConsecutive(
[
array_merge(
['method_type' => $methodType],
['method_name' => $methodName],
$requestParams),
ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM],
[$response, ClientInterface::LOG_CATEGORY_CLIENT_CONNECTUM]
);
$client = new Client($guzzle, $logger);
$client->execute($command);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage ::error::
*/
public function testExecuteWhenError()
{
$requestParams = ['::request params::'];
$command = $this->createMock(Command::class);
$command->method('getRequestParams')->willReturn($requestParams);
$handle = '::handle source::';
$guzzle = $this->createMock(\GuzzleHttp\Client::class);
$guzzle->expects(self::never())
->method('post');
$logger = $this->createMock(Logger::class);
$client = new Client($guzzle, $logger);
$client->execute($command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment