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

149 lines
5.2 KiB
PHP
Raw Normal View History

2021-03-03 15:08:27 +01:00
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
2021-09-14 17:46:33 +02:00
use Requests_Utility_CaseInsensitiveDictionary;
2021-03-03 15:08:27 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
2022-01-05 12:49:49 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
2021-03-03 15:08:27 +01:00
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use Mockery;
2021-11-12 17:10:00 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2021-03-03 15:08:27 +01:00
use function Brain\Monkey\Functions\expect;
2021-09-14 17:46:33 +02:00
use function Brain\Monkey\Functions\when;
2021-03-03 15:08:27 +01:00
class IdentityTokenTest extends TestCase
{
private $host;
private $bearer;
private $logger;
2021-11-12 17:10:00 +01:00
private $settings;
2022-01-05 12:49:49 +01:00
private $customer_repository;
2021-03-03 15:08:27 +01:00
private $sut;
public function setUp(): void
{
parent::setUp();
$this->host = 'https://example.com/';
$this->bearer = Mockery::mock(Bearer::class);
$this->logger = Mockery::mock(LoggerInterface::class);
2021-11-12 17:10:00 +01:00
$this->settings = Mockery::mock(Settings::class);
2022-01-05 12:49:49 +01:00
$this->customer_repository = Mockery::mock(CustomerRepository::class);
$this->sut = new IdentityToken(
$this->host,
$this->bearer,
$this->logger,
$this->settings,
$this->customer_repository
);
2021-03-03 15:08:27 +01:00
}
public function testGenerateForCustomerReturnsToken()
{
define( 'PPCP_FLAG_SUBSCRIPTION', true );
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$this->bearer
->expects('bearer')->andReturn($token);
$host = $this->host;
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$this->logger->shouldReceive('debug');
2021-11-12 17:10:00 +01:00
$this->settings->shouldReceive('has')->andReturn(true);
$this->settings->shouldReceive('get')->andReturn(true);
2022-01-05 12:49:49 +01:00
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('prefix1');
2021-09-14 17:46:33 +02:00
$rawResponse = [
'body' => '{"client_token":"abc123", "expires_in":3600}',
'headers' => $headers,
];
2021-03-03 15:08:27 +01:00
expect('wp_remote_get')
2021-09-14 17:46:33 +02:00
->andReturnUsing(function ($url, $args) use ($rawResponse, $host, $headers) {
2021-03-03 15:08:27 +01:00
if ($url !== $host . 'v1/identity/generate-token') {
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;
}
if ($args['body'] !== '{"customer_id":"prefix1"}') {
return false;
}
return $rawResponse;
});
expect('is_wp_error')->with($rawResponse)->andReturn(false);
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
2021-09-14 17:46:33 +02:00
when('wc_print_r')->returnArg();
2022-01-04 14:59:35 +01:00
when('get_user_meta')->justReturn('');
2021-03-03 15:08:27 +01:00
$result = $this->sut->generate_for_customer(1);
$this->assertInstanceOf(Token::class, $result);
}
public function testGenerateForCustomerFailsBecauseWpError()
{
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$this->bearer
->expects('bearer')->andReturn($token);
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn(['headers' => $headers,]);
2021-03-03 15:08:27 +01:00
expect('is_wp_error')->andReturn(true);
2021-09-14 17:46:33 +02:00
when('wc_print_r')->returnArg();
2021-03-03 15:08:27 +01:00
$this->logger->shouldReceive('log');
2021-09-14 17:46:33 +02:00
$this->logger->shouldReceive('debug');
2021-11-12 17:10:00 +01:00
$this->settings->shouldReceive('has')->andReturn(true);
$this->settings->shouldReceive('get')->andReturn(true);
2022-01-05 12:49:49 +01:00
$this->customer_repository->shouldReceive('customer_id_for_user');
2021-03-03 15:08:27 +01:00
$this->expectException(RuntimeException::class);
$this->sut->generate_for_customer(1);
}
public function testGenerateForCustomerFailsBecauseResponseCodeIsNot200()
{
$token = Mockery::mock(Token::class);
$token
->expects('token')->andReturn('bearer');
$this->bearer
->expects('bearer')->andReturn($token);
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
expect('wp_remote_get')->andReturn([
'body' => '',
'headers' => $headers,
]);
2021-03-03 15:08:27 +01:00
expect('is_wp_error')->andReturn(false);
expect('wp_remote_retrieve_response_code')->andReturn(500);
2021-09-14 17:46:33 +02:00
when('wc_print_r')->returnArg();
2021-03-03 15:08:27 +01:00
$this->logger->shouldReceive('log');
2021-09-14 17:46:33 +02:00
$this->logger->shouldReceive('debug');
2021-11-12 17:10:00 +01:00
$this->settings->shouldReceive('has')->andReturn(true);
$this->settings->shouldReceive('get')->andReturn(true);
2022-01-05 12:49:49 +01:00
$this->customer_repository->shouldReceive('customer_id_for_user');
2021-03-03 15:08:27 +01:00
$this->expectException(PayPalApiException::class);
$this->sut->generate_for_customer(1);
}
}