woocommerce-paypal-payments/tests/PHPUnit/ApiClient/Endpoint/PaymentsEndpointTest.php

303 lines
9.9 KiB
PHP
Raw Normal View History

2020-08-31 13:38:54 +03:00
<?php
declare(strict_types=1);
2020-09-14 07:51:45 +03:00
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
2020-08-31 13:38:54 +03:00
2021-09-15 11:28:32 +02:00
use Requests_Utility_CaseInsensitiveDictionary;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ErrorResponseCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
2020-08-31 13:38:54 +03:00
use Mockery;
use Psr\Log\LoggerInterface;
use function Brain\Monkey\Functions\expect;
class PaymentsEndpointTest extends TestCase
{
public function testAuthorizationDefault()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$bearer = Mockery::mock(Bearer::class);
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer
->expects('bearer')->andReturn($token);
$authorization = Mockery::mock(Authorization::class);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$authorizationFactory
2020-09-01 09:47:36 +03:00
->expects('from_paypal_response')
2020-08-31 13:38:54 +03:00
->andReturn($authorization);
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
2021-09-15 11:28:32 +02:00
$logger->shouldReceive('debug');
2020-08-31 13:38:54 +03:00
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturnUsing(
function ($url, $args) use ($rawResponse, $host, $authorizationId) {
if ($url !== $host . 'v2/payments/authorizations/' . $authorizationId) {
return false;
}
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
return false;
}
if ($args['headers']['Content-Type'] !== 'application/json') {
return false;
}
return $rawResponse;
}
);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
$result = $testee->authorization($authorizationId);
$this->assertEquals($authorization, $result);
}
public function testAuthorizationWpError()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer = Mockery::mock(Bearer::class);
$bearer->expects('bearer')->andReturn($token);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
2021-09-15 11:28:32 +02:00
$logger->shouldReceive('debug');
2020-08-31 13:38:54 +03:00
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
$this->expectException(RuntimeException::class);
$testee->authorization($authorizationId);
}
public function testAuthorizationIsNot200()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer = Mockery::mock(Bearer::class);
$bearer->expects('bearer')->andReturn($token);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
2021-09-15 11:28:32 +02:00
$logger->shouldReceive('debug');
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(500);
$this->expectException(RuntimeException::class);
$testee->authorization($authorizationId);
}
public function testCaptureDefault()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer = Mockery::mock(Bearer::class);
$bearer
->expects('bearer')->andReturn($token);
$authorization = Mockery::mock(Authorization::class);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$authorizationFactory
2020-09-01 09:47:36 +03:00
->expects('from_paypal_response')
2020-08-31 13:38:54 +03:00
->andReturn($authorization);
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
2021-09-15 11:28:32 +02:00
$logger->shouldReceive('debug');
2020-08-31 13:38:54 +03:00
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturnUsing(
function ($url, $args) use ($rawResponse, $host, $authorizationId) {
if ($url !== $host . 'v2/payments/authorizations/' . $authorizationId . '/capture') {
return false;
}
if ($args['method'] !== 'POST') {
return false;
}
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
return false;
}
if ($args['headers']['Content-Type'] !== 'application/json') {
return false;
}
return $rawResponse;
}
);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(201);
$result = $testee->capture($authorizationId);
$this->assertEquals($authorization, $result);
}
public function testCaptureIsWpError()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer = Mockery::mock(Bearer::class);
$bearer->expects('bearer')->andReturn($token);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
2021-09-15 11:28:32 +02:00
$logger->expects('debug');
2020-08-31 13:38:54 +03:00
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
$this->expectException(RuntimeException::class);
$testee->capture($authorizationId);
}
public function testAuthorizationIsNot201()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$host = 'https://example.com/';
$authorizationId = 'somekindofid';
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$bearer = Mockery::mock(Bearer::class);
$bearer->expects('bearer')->andReturn($token);
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
2020-08-31 13:38:54 +03:00
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
2021-09-15 11:28:32 +02:00
$logger->expects('debug');
2020-08-31 13:38:54 +03:00
$testee = new PaymentsEndpoint(
$host,
$bearer,
$authorizationFactory,
$logger
);
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(500);
$this->expectException(RuntimeException::class);
$testee->capture($authorizationId);
}
}