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

203 lines
6.7 KiB
PHP
Raw Normal View History

2021-03-03 11:50:27 +01:00
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
2021-09-15 11:28:32 +02:00
use Requests_Utility_CaseInsensitiveDictionary;
2021-03-03 11:50:27 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use function Brain\Monkey\Functions\expect;
class PaymentTokenEndpointTest extends TestCase
{
use MockeryPHPUnitIntegration;
private $host;
private $bearer;
private $factory;
private $logger;
private $prefix;
private $sut;
public function setUp(): void
{
parent::setUp();
$this->host = 'https://example.com/';
$this->bearer = Mockery::mock(Bearer::class);
$this->factory = Mockery::mock(PaymentTokenFactory::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->prefix = 'prefix';
$this->sut = new PaymentTokenEndpoint(
$this->host,
$this->bearer,
$this->factory,
$this->logger,
$this->prefix
);
}
public function testForUserReturnsToken()
{
$id = 1;
$token = Mockery::mock(Token::class);
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = [
'body' => '{"payment_tokens":[{"id": "123abc"}]}',
'headers' => $headers,
];
2021-04-01 13:48:10 +03:00
$paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken->shouldReceive('id')
->andReturn('foo');
2021-03-03 11:50:27 +01:00
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
->andReturn('bearer');
$this->ensureRequestForUser($rawResponse, $id);
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
$this->factory->shouldReceive('from_paypal_response')
->andReturn($paymentToken);
2021-09-15 11:28:32 +02:00
$this->logger->shouldReceive('debug');
2021-03-03 11:50:27 +01:00
$result = $this->sut->for_user($id);
$this->assertInstanceOf(PaymentToken::class, $result[0]);
}
public function testForUserFailsBecauseOfWpError()
{
$id = 1;
$token = Mockery::mock(Token::class);
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$rawResponse = ['headers' => $headers,];
2021-03-03 11:50:27 +01:00
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
->andReturn('bearer');
$this->ensureRequestForUser($rawResponse, $id);
expect('wp_remote_get')->andReturn($rawResponse);
expect('is_wp_error')->with($rawResponse)->andReturn(true);
$this->logger->shouldReceive('log');
2021-09-15 11:28:32 +02:00
$this->logger->shouldReceive('debug');
2021-03-03 11:50:27 +01:00
$this->expectException(RuntimeException::class);
$this->sut->for_user($id);
}
public function testForUserFailsBecauseResponseCodeIsNot200()
{
$id = 1;
$token = Mockery::mock(Token::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,
];
2021-03-03 11:50:27 +01:00
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
->andReturn('bearer');
$this->ensureRequestForUser($rawResponse, $id);
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->logger->shouldReceive('log');
2021-09-15 11:28:32 +02:00
$this->logger->shouldReceive('debug');
2021-03-03 11:50:27 +01:00
$this->expectException(PayPalApiException::class);
$this->sut->for_user($id);
}
public function testDeleteToken()
{
2021-09-15 11:28:32 +02:00
$paymentToken = Mockery::mock(PaymentToken::class);
2021-04-01 13:48:10 +03:00
$paymentToken->shouldReceive('id')
->andReturn('foo');
2021-03-03 11:50:27 +01:00
$token = Mockery::mock(Token::class);
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
->andReturn('bearer');
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'headers' => $headers,
]);
2021-03-03 11:50:27 +01:00
expect('is_wp_error')->andReturn(false);
expect('wp_remote_retrieve_response_code')->andReturn(204);
2021-09-15 11:28:32 +02:00
$this->logger->shouldReceive('debug');
2021-03-03 11:50:27 +01:00
$this->sut->delete_token($paymentToken);
}
public function testDeleteTokenFails()
{
2021-04-01 13:48:10 +03:00
$paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken->shouldReceive('id')
->andReturn('foo');
2021-03-03 11:50:27 +01:00
$token = Mockery::mock(Token::class);
$this->bearer->shouldReceive('bearer')
->andReturn($token);
$token->shouldReceive('token')
->andReturn('bearer');
2021-09-15 11:28:32 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'headers' => $headers,
]);
2021-03-03 11:50:27 +01:00
expect('is_wp_error')->andReturn(true);
$this->logger->shouldReceive('log');
2021-09-15 11:28:32 +02:00
$this->logger->shouldReceive('debug');
2021-03-03 11:50:27 +01:00
$this->expectException(RuntimeException::class);
$this->sut->delete_token($paymentToken);
}
/**
* @param array $rawResponse
* @param int $id
* @throws \Brain\Monkey\Expectation\Exception\ExpectationArgsRequired
*/
private function ensureRequestForUser(array $rawResponse, int $id): void
{
$host = $this->host;
$prefix = $this->prefix;
expect('wp_remote_get')->andReturnUsing(
function ($url, $args) use ($rawResponse, $host, $prefix, $id) {
if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id) {
return false;
}
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
return false;
}
if ($args['headers']['Content-Type'] !== 'application/json') {
return false;
}
return $rawResponse;
}
);
}
}