Fix unit tests

This commit is contained in:
dinamiko 2021-09-15 11:28:32 +02:00
parent 12b9905c60
commit 804ff8e526
3 changed files with 192 additions and 35 deletions

View file

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

View file

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

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization; use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ErrorResponseCollection; use WooCommerce\PayPalCommerce\ApiClient\Entity\ErrorResponseCollection;
@ -40,8 +41,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log'); $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( $testee = new PaymentsEndpoint(
$host, $host,
@ -88,8 +95,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $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( $testee = new PaymentsEndpoint(
$host, $host,
@ -119,10 +132,16 @@ class PaymentsEndpointTest extends TestCase
$authorizationFactory = Mockery::mock(AuthorizationFactory::class); $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 = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $logger->shouldReceive('log');
$logger->shouldReceive('debug');
$testee = new PaymentsEndpoint( $testee = new PaymentsEndpoint(
$host, $host,
@ -161,8 +180,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log'); $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( $testee = new PaymentsEndpoint(
$host, $host,
@ -212,8 +237,14 @@ class PaymentsEndpointTest extends TestCase
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->expects('log'); $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( $testee = new PaymentsEndpoint(
$host, $host,
@ -243,10 +274,16 @@ class PaymentsEndpointTest extends TestCase
$authorizationFactory = Mockery::mock(AuthorizationFactory::class); $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 = Mockery::mock(LoggerInterface::class);
$logger->expects('log'); $logger->expects('log');
$logger->expects('debug');
$testee = new PaymentsEndpoint( $testee = new PaymentsEndpoint(
$host, $host,