Fix merge conflict

This commit is contained in:
dinamiko 2021-09-16 12:34:05 +02:00
commit 17de4ac1de
33 changed files with 946 additions and 341 deletions

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
@ -28,10 +29,12 @@ class PayPalBearerTest extends TestCase
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -40,7 +43,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
function ($url, $args) use ($json, $key, $secret, $host) {
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
@ -53,6 +56,7 @@ class PayPalBearerTest extends TestCase
return [
'body' => $json,
'headers' => $headers
];
}
);
@ -80,10 +84,12 @@ class PayPalBearerTest extends TestCase
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -92,7 +98,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
function ($url, $args) use ($json, $key, $secret, $host) {
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
@ -105,6 +111,7 @@ class PayPalBearerTest extends TestCase
return [
'body' => $json,
'headers' => $headers,
];
}
);
@ -153,9 +160,12 @@ class PayPalBearerTest extends TestCase
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -164,7 +174,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
function ($url, $args) use ($json, $key, $secret, $host) {
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
@ -177,6 +187,7 @@ class PayPalBearerTest extends TestCase
return [
'body' => $json,
'headers' => $headers,
];
}
);
@ -199,9 +210,12 @@ class PayPalBearerTest extends TestCase
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -210,7 +224,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
function ($url, $args) use ($json, $key, $secret, $host) {
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
@ -223,6 +237,7 @@ class PayPalBearerTest extends TestCase
return [
'body' => $json,
'headers' => $headers,
];
}
);

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -11,6 +12,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use Mockery;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class IdentityTokenTest extends TestCase
{
@ -40,10 +42,18 @@ class IdentityTokenTest extends TestCase
$this->bearer
->expects('bearer')->andReturn($token);
$rawResponse = ['body' => '{"client_token":"abc123", "expires_in":3600}'];
$host = $this->host;
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$this->logger->shouldReceive('debug');
$rawResponse = [
'body' => '{"client_token":"abc123", "expires_in":3600}',
'headers' => $headers,
];
expect('wp_remote_get')
->andReturnUsing(function ($url, $args) use ($rawResponse, $host) {
->andReturnUsing(function ($url, $args) use ($rawResponse, $host, $headers) {
if ($url !== $host . 'v1/identity/generate-token') {
return false;
}
@ -65,6 +75,7 @@ class IdentityTokenTest extends TestCase
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
when('wc_print_r')->returnArg();
$result = $this->sut->generate_for_customer(1);
$this->assertInstanceOf(Token::class, $result);
@ -78,9 +89,13 @@ class IdentityTokenTest extends TestCase
$this->bearer
->expects('bearer')->andReturn($token);
expect('wp_remote_get')->andReturn();
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn(['headers' => $headers,]);
expect('is_wp_error')->andReturn(true);
when('wc_print_r')->returnArg();
$this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(RuntimeException::class);
$this->sut->generate_for_customer(1);
@ -94,10 +109,17 @@ class IdentityTokenTest extends TestCase
$this->bearer
->expects('bearer')->andReturn($token);
expect('wp_remote_get')->andReturn(['body' => '',]);
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'body' => '',
'headers' => $headers,
]);
expect('is_wp_error')->andReturn(false);
expect('wp_remote_retrieve_response_code')->andReturn(500);
when('wc_print_r')->returnArg();
$this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(PayPalApiException::class);
$this->sut->generate_for_customer(1);

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Hamcrest\Matchers;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use Woocommerce\PayPalCommerce\ApiClient\Entity\Capture;
@ -22,14 +23,20 @@ use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use Mockery;
use Psr\Log\LoggerInterface;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class OrderEndpointTest extends TestCase
{
public function testOrderDefault()
public function setUp(): void
{
parent::setUp();
when('wc_print_r')->returnArg();
}
public function testOrderDefault()
{
expect('wp_json_encode')->andReturnUsing('json_encode');
$orderId = 'id';
@ -51,10 +58,14 @@ class OrderEndpointTest extends TestCase
$intent = 'CAPTURE';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
->expects('get_for_order_id')->with($orderId)->andReturn('uniqueRequestId');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$testee = new OrderEndpoint(
$host,
$bearer,
@ -66,7 +77,10 @@ class OrderEndpointTest extends TestCase
$paypalRequestIdRepository
);
$rawResponse = ['body' => '{"is_correct":true}'];
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
expect('wp_remote_get')
->andReturnUsing(function ($url, $args) use ($rawResponse, $host, $orderId) {
if ($url !== $host . 'v2/checkout/orders/' . $orderId) {
@ -103,10 +117,14 @@ class OrderEndpointTest extends TestCase
$intent = 'CAPTURE';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
->expects('get_for_order_id')->with($orderId)->andReturn('uniqueRequestId');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$testee = new OrderEndpoint(
$host,
$bearer,
@ -118,7 +136,10 @@ class OrderEndpointTest extends TestCase
$paypalRequestIdRepository
);
$rawResponse = ['body' => '{"is_correct":true}'];
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
@ -140,9 +161,15 @@ class OrderEndpointTest extends TestCase
$orderFactory = Mockery::mock(OrderFactory::class);
$patchCollectionFactory = Mockery::mock(PatchCollectionFactory::class);
$intent = 'CAPTURE';
$rawResponse = ['body' => '{"some_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -176,8 +203,12 @@ class OrderEndpointTest extends TestCase
$orderToCapture = Mockery::mock(Order::class);
$orderToCapture->expects('status')->andReturn($orderToCaptureStatus);
$orderToCapture->expects('id')->andReturn($orderId);
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$expectedOrder = Mockery::mock(Order::class);
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
@ -202,6 +233,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -307,6 +339,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -321,8 +354,12 @@ class OrderEndpointTest extends TestCase
$applicationContextRepository,
$paypalRequestIdRepository
);
$rawResponse = ['body' => '{"is_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_error":true}',
'headers' => $headers,
];
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
$this->expectException(RuntimeException::class);
@ -351,6 +388,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -366,7 +404,12 @@ class OrderEndpointTest extends TestCase
$paypalRequestIdRepository
);
$rawResponse = ['body' => '{"some_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers
];
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(500);
@ -396,6 +439,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -415,8 +459,12 @@ class OrderEndpointTest extends TestCase
)->makePartial();
$orderToExpect = Mockery::mock(Order::class);
$testee->expects('order')->with($orderId)->andReturn($orderToExpect);
$rawResponse = ['body' => '{"some_error": "' . ErrorResponse::ORDER_ALREADY_CAPTURED . '"}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error": "' . ErrorResponse::ORDER_ALREADY_CAPTURED . '"}',
'headers' => $headers,
];
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(500);
@ -436,8 +484,12 @@ class OrderEndpointTest extends TestCase
->shouldReceive('purchase_units')
->andReturn([]);
$orderToCompare = Mockery::mock(Order::class);
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$expectedOrder = Mockery::mock(Order::class);
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
@ -464,6 +516,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -533,8 +586,12 @@ class OrderEndpointTest extends TestCase
->shouldReceive('purchase_units')
->andReturn([]);
$orderToCompare = Mockery::mock(Order::class);
$rawResponse = ['body' => '{"has_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"has_error":true}',
'headers' => $headers,
];
$expectedOrder = Mockery::mock(Order::class);
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
@ -561,6 +618,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
$paypalRequestIdRepository
@ -624,8 +682,12 @@ class OrderEndpointTest extends TestCase
->shouldReceive('purchase_units')
->andReturn([]);
$orderToCompare = Mockery::mock(Order::class);
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$expectedOrder = Mockery::mock(Order::class);
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
@ -652,6 +714,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContextRepository = Mockery::mock(ApplicationContextRepository::class);
$paypalRequestIdRepository = Mockery::mock(PayPalRequestIdRepository::class);
@ -748,7 +811,12 @@ class OrderEndpointTest extends TestCase
public function testCreateForPurchaseUnitsDefault()
{
expect('wp_json_encode')->andReturnUsing('json_encode');
$rawResponse = ['body' => '{"success":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"success":true}',
'headers' => $headers,
];
$host = 'https://example.com/';
$bearer = Mockery::mock(Bearer::class);
$token = Mockery::mock(Token::class);
@ -772,6 +840,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContext = Mockery::mock(ApplicationContext::class);
$applicationContext
->expects('to_array')
@ -844,7 +913,12 @@ class OrderEndpointTest extends TestCase
public function testCreateForPurchaseUnitsWithPayer()
{
expect('wp_json_encode')->andReturnUsing('json_encode');
$rawResponse = ['body' => '{"success":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"success":true}',
'headers' => $headers,
];
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
$token
@ -868,6 +942,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$applicationContext = Mockery::mock(ApplicationContext::class);
$applicationContext
->expects('to_array')
@ -928,7 +1003,12 @@ class OrderEndpointTest extends TestCase
public function testCreateForPurchaseUnitsIsWpError()
{
expect('wp_json_encode')->andReturnUsing('json_encode');
$rawResponse = ['body' => '{"success":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"success":true}',
'headers' => $headers,
];
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
$token
@ -943,6 +1023,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContext = Mockery::mock(ApplicationContext::class);
$applicationContext
->expects('to_array')
@ -1006,7 +1087,12 @@ class OrderEndpointTest extends TestCase
public function testCreateForPurchaseUnitsIsNot201()
{
expect('wp_json_encode')->andReturnUsing('json_encode');
$rawResponse = ['body' => '{"has_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"has_error":true}',
'headers' => $headers,
];
$host = 'https://example.com/';
$token = Mockery::mock(Token::class);
$token
@ -1021,6 +1107,7 @@ class OrderEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$applicationContext = Mockery::mock(ApplicationContext::class);
$applicationContext
->expects('to_array')

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
@ -48,7 +49,12 @@ class PaymentTokenEndpointTest extends TestCase
{
$id = 1;
$token = Mockery::mock(Token::class);
$rawResponse = ['body' => '{"payment_tokens":[{"id": "123abc"}]}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"payment_tokens":[{"id": "123abc"}]}',
'headers' => $headers,
];
$paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken->shouldReceive('id')
->andReturn('foo');
@ -65,6 +71,8 @@ class PaymentTokenEndpointTest extends TestCase
$this->factory->shouldReceive('from_paypal_response')
->andReturn($paymentToken);
$this->logger->shouldReceive('debug');
$result = $this->sut->for_user($id);
$this->assertInstanceOf(PaymentToken::class, $result[0]);
@ -74,7 +82,9 @@ class PaymentTokenEndpointTest extends TestCase
{
$id = 1;
$token = Mockery::mock(Token::class);
$rawResponse = [];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = ['headers' => $headers,];
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
@ -84,6 +94,7 @@ class PaymentTokenEndpointTest extends TestCase
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
$this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(RuntimeException::class);
$this->sut->for_user($id);
@ -93,7 +104,12 @@ class PaymentTokenEndpointTest extends TestCase
{
$id = 1;
$token = Mockery::mock(Token::class);
$rawResponse = ['body' => '{"some_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
@ -105,6 +121,7 @@ class PaymentTokenEndpointTest extends TestCase
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(500);
$this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(PayPalApiException::class);
$this->sut->for_user($id);
@ -112,7 +129,7 @@ class PaymentTokenEndpointTest extends TestCase
public function testDeleteToken()
{
$paymentToken = $paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken->shouldReceive('id')
->andReturn('foo');
$token = Mockery::mock(Token::class);
@ -121,9 +138,14 @@ class PaymentTokenEndpointTest extends TestCase
$token->shouldReceive('token')
->andReturn('bearer');
expect('wp_remote_get')->andReturn();
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'headers' => $headers,
]);
expect('is_wp_error')->andReturn(false);
expect('wp_remote_retrieve_response_code')->andReturn(204);
$this->logger->shouldReceive('debug');
$this->sut->delete_token($paymentToken);
}
@ -139,9 +161,14 @@ class PaymentTokenEndpointTest extends TestCase
$token->shouldReceive('token')
->andReturn('bearer');
expect('wp_remote_get')->andReturn();
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'headers' => $headers,
]);
expect('is_wp_error')->andReturn(true);
$this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(RuntimeException::class);
$this->sut->delete_token($paymentToken);

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ErrorResponseCollection;
@ -40,8 +41,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$testee = new PaymentsEndpoint(
$host,
@ -88,8 +95,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$testee = new PaymentsEndpoint(
$host,
@ -119,10 +132,16 @@ class PaymentsEndpointTest extends TestCase
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$rawResponse = ['body' => '{"some_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
$logger->shouldReceive('debug');
$testee = new PaymentsEndpoint(
$host,
@ -161,8 +180,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
$logger->shouldReceive('debug');
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$testee = new PaymentsEndpoint(
$host,
@ -212,8 +237,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
$logger->expects('debug');
$rawResponse = ['body' => '{"is_correct":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"is_correct":true}',
'headers' => $headers,
];
$testee = new PaymentsEndpoint(
$host,
@ -243,10 +274,16 @@ class PaymentsEndpointTest extends TestCase
$authorizationFactory = Mockery::mock(AuthorizationFactory::class);
$rawResponse = ['body' => '{"some_error":true}'];
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"some_error":true}',
'headers' => $headers,
];
$logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log');
$logger->expects('debug');
$testee = new PaymentsEndpoint(
$host,

View file

@ -32,7 +32,7 @@ class CreateOrderEndpointTest extends TestCase
{
list($payer_factory, $testee) = $this->mockTestee();
$method = $this->testPrivateMethod(CreateOrderEndpoint::class, 'payer');
$method = $this->makePrivateMethod(CreateOrderEndpoint::class, 'payer');
$dataString = wp_json_encode($expectedResult['payer']);
$dataObj = json_decode(wp_json_encode($expectedResult['payer']));
@ -173,11 +173,11 @@ class CreateOrderEndpointTest extends TestCase
* @return \ReflectionMethod
* @throws \ReflectionException
*/
protected function testPrivateMethod($class, $method)
protected function makePrivateMethod($class, $method)
{
$reflector = new ReflectionClass($class);
$method = $reflector->getMethod($method);
$method->setAccessible(true);
return $method;
}
}
}

View file

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce;
use Dhii\Container\CompositeCachingServiceProvider;
use Dhii\Container\DelegatingContainer;
use Dhii\Container\ServiceProvider;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Psr\Container\ContainerInterface;
use function Brain\Monkey\Functions\when;
class ModularTestCase extends TestCase
{
use MockeryPHPUnitIntegration;
public function setUp(): void
{
parent::setUp();
when('get_option')->justReturn(null);
when('plugins_url')->returnArg();
when('plugin_dir_path')->alias(function ($file) { return trailingslashit(dirname($file)); });
when('get_current_blog_id')->justReturn(42);
when('get_site_url')->justReturn('example.com');
when('get_bloginfo')->justReturn('My Shop');
when('wc_get_base_location')->justReturn(['country' => 'US']);
when('get_woocommerce_currency')->justReturn('USD');
when('WC')->justReturn((object) [
'session' => null,
]);
global $wpdb;
$wpdb = \Mockery::mock(\stdClass::class);
$wpdb->shouldReceive('get_var')->andReturn(null);
$wpdb->shouldReceive('prepare')->andReturn(null);
$wpdb->posts = '';
$wpdb->postmeta = '';
!defined('PAYPAL_API_URL') && define('PAYPAL_API_URL', 'https://api.paypal.com');
!defined('PAYPAL_SANDBOX_API_URL') && define('PAYPAL_SANDBOX_API_URL', 'https://api.sandbox.paypal.com');
!defined('PAYPAL_INTEGRATION_DATE') && define('PAYPAL_INTEGRATION_DATE', '2020-10-15');
!defined('PPCP_FLAG_SUBSCRIPTION') && define('PPCP_FLAG_SUBSCRIPTION', true);
!defined('CONNECT_WOO_CLIENT_ID') && define('CONNECT_WOO_CLIENT_ID', 'woo-id');
!defined('CONNECT_WOO_SANDBOX_CLIENT_ID') && define('CONNECT_WOO_SANDBOX_CLIENT_ID', 'woo-id2');
!defined('CONNECT_WOO_MERCHANT_ID') && define('CONNECT_WOO_MERCHANT_ID', 'merchant-id');
!defined('CONNECT_WOO_SANDBOX_MERCHANT_ID') && define('CONNECT_WOO_SANDBOX_MERCHANT_ID', 'merchant-id2');
!defined('CONNECT_WOO_URL') && define('CONNECT_WOO_URL', 'https://connect.woocommerce.com/ppc');
!defined('CONNECT_WOO_SANDBOX_URL') && define('CONNECT_WOO_SANDBOX_URL', 'https://connect.woocommerce.com/ppcsandbox');
}
/**
* @param array<string, callable> $overriddenServices
* @return ContainerInterface
*/
protected function bootstrapModule(array $overriddenServices = []): ContainerInterface
{
$overridingContainer = new DelegatingContainer(new CompositeCachingServiceProvider([
new ServiceProvider($overriddenServices, []),
]));
$rootDir = ROOT_DIR;
$bootstrap = require ("$rootDir/bootstrap.php");
$appContainer = $bootstrap($rootDir, $overridingContainer);
return $appContainer;
}
}

View file

@ -3,26 +3,36 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce;
use function Brain\Monkey\Functions\when;
use function Brain\Monkey\setUp;
use function Brain\Monkey\tearDown;
use function Brain\Monkey\Functions\expect;
use Mockery;
class TestCase extends \PHPUnit\Framework\TestCase
{
public function setUp(): void
{
parent::setUp();
expect('__')->andReturnUsing(function (string $text) {
return $text;
});
setUp();
}
public function setUp(): void
{
parent::setUp();
public function tearDown(): void
{
tearDown();
Mockery::close();
parent::tearDown();
}
when('__')->returnArg();
when('_x')->returnArg();
when('esc_url')->returnArg();
when('esc_attr')->returnArg();
when('esc_attr__')->returnArg();
when('esc_html')->returnArg();
when('esc_html__')->returnArg();
when('esc_textarea')->returnArg();
when('sanitize_text_field')->returnArg();
when('wp_kses_post')->returnArg();
when('wp_unslash')->returnArg();
setUp();
}
public function tearDown(): void
{
tearDown();
Mockery::close();
parent::tearDown();
}
}

View file

@ -68,7 +68,8 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
expect('wc_get_order')
@ -116,7 +117,8 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
expect('wc_get_order')
@ -181,7 +183,8 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
expect('wc_get_order')
@ -254,7 +257,8 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
@ -311,7 +315,8 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
@ -362,12 +367,13 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
$this->assertFalse($testee->capture_authorized_payment($wcOrder));
}
/**
* @dataProvider dataForTestNeedsSetup
*/
@ -390,7 +396,7 @@ class WcGatewayTest extends TestCase
->andReturn($currentState);
$transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class);
$subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
$testee = new PayPalGateway(
$settingsRenderer,
$orderProcessor,
@ -401,9 +407,10 @@ class WcGatewayTest extends TestCase
$refundProcessor,
$onboardingState,
$transactionUrlProvider,
$subscriptionHelper
$subscriptionHelper,
PayPalGateway::ID
);
$this->assertSame($needSetup, $testee->needs_setup());
}
@ -424,7 +431,7 @@ class WcGatewayTest extends TestCase
],
];
}
public function dataForTestNeedsSetup(): array
{
return [

View file

@ -4,63 +4,77 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ModularTestCase;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
use function Brain\Monkey\Functions\when;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
class SettingsListenerTest extends TestCase
class SettingsListenerTest extends ModularTestCase
{
use MockeryPHPUnitIntegration;
private $appContainer;
public function testListen()
{
$settings = Mockery::mock(Settings::class);
$setting_fields = [];
$webhook_registrar = Mockery::mock(WebhookRegistrar::class);
$cache = Mockery::mock(Cache::class);
$state = Mockery::mock(State::class);
$bearer = Mockery::mock(Bearer::class);
public function setUp(): void
{
parent::setUp();
$testee = new SettingsListener(
$settings,
$setting_fields,
$webhook_registrar,
$cache,
$state,
$bearer
);
$this->appContainer = $this->bootstrapModule();
}
$_REQUEST['section'] = 'ppcp-gateway';
$_POST['ppcp-nonce'] = 'foo';
$_POST['ppcp'] = [
'client_id' => 'client_id',
];
$_GET['ppcp-tab'] = 'just-a-tab';
public function testListen()
{
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('set');
when('sanitize_text_field')->justReturn('ppcp-gateway');
when('wp_unslash')->justReturn('ppcp-gateway');
when('current_user_can')->justReturn(true);
when('wp_verify_nonce')->justReturn(true);
$setting_fields = $this->appContainer->get('wcgateway.settings.fields');
$settings->shouldReceive('has')
->with('client_id')
->andReturn('client_id');
$settings->shouldReceive('get')
->with('client_id')
->andReturn('client_id');
$settings->shouldReceive('has')
->with('client_secret')
->andReturn('client_secret');
$settings->shouldReceive('get')
->with('client_secret')
->andReturn('client_secret');
$settings->shouldReceive('persist');
$cache->shouldReceive('has')
->andReturn(false);
$webhook_registrar = Mockery::mock(WebhookRegistrar::class);
$webhook_registrar->shouldReceive('unregister')->andReturnTrue();
$webhook_registrar->shouldReceive('register')->andReturnTrue();
$testee->listen();
}
$cache = Mockery::mock(Cache::class);
$state = Mockery::mock(State::class);
$state->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$bearer = Mockery::mock(Bearer::class);
$testee = new SettingsListener(
$settings,
$setting_fields,
$webhook_registrar,
$cache,
$state,
$bearer,
PayPalGateway::ID
);
$_GET['section'] = PayPalGateway::ID;
$_POST['ppcp-nonce'] = 'foo';
$_POST['ppcp'] = [
'client_id' => 'client_id',
];
$_GET['ppcp-tab'] = PayPalGateway::ID;
when('current_user_can')->justReturn(true);
when('wp_verify_nonce')->justReturn(true);
$settings->shouldReceive('has')
->with('client_id')
->andReturn('client_id');
$settings->shouldReceive('get')
->with('client_id')
->andReturn('client_id');
$settings->shouldReceive('has')
->with('client_secret')
->andReturn('client_secret');
$settings->shouldReceive('get')
->with('client_secret')
->andReturn('client_secret');
$settings->shouldReceive('persist');
$cache->shouldReceive('has')
->andReturn(false);
$testee->listen();
}
}

View file

@ -1,6 +1,10 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../stubs/WC_Payment_Gateway.php';
require_once __DIR__ . '/../stubs/WC_Payment_Gateway_CC.php';
require_once __DIR__ . '/../stubs/WC_Ajax.php';
define('TESTS_ROOT_DIR', dirname(__DIR__));
define('ROOT_DIR', dirname(TESTS_ROOT_DIR));
require_once ROOT_DIR . '/vendor/autoload.php';
require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway.php';
require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway_CC.php';
require_once TESTS_ROOT_DIR . '/stubs/WC_Ajax.php';