mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
add api client to main repository
This commit is contained in:
parent
7c5638764e
commit
f5bb4048cd
100 changed files with 10765 additions and 2 deletions
220
tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php
Normal file
220
tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php
Normal file
|
@ -0,0 +1,220 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Authentication;
|
||||
|
||||
use Brain\Monkey\Expectation\Exception\ExpectationArgsRequired;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use Mockery;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
|
||||
class PayPalBearerTest extends TestCase
|
||||
{
|
||||
|
||||
public function testDefault()
|
||||
{
|
||||
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
|
||||
$cache = Mockery::mock(CacheInterface::class);
|
||||
$cache
|
||||
->expects('get')
|
||||
->andReturn('{"access_token":"abc","expires_in":100, "created":100}');
|
||||
$cache
|
||||
->expects('set');
|
||||
$host = 'https://example.com';
|
||||
$key = 'key';
|
||||
$secret = 'secret';
|
||||
$logger = Mockery::mock(LoggerInterface::class);
|
||||
$logger->shouldNotReceive('log');
|
||||
|
||||
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger);
|
||||
|
||||
expect('trailingslashit')
|
||||
->with($host)
|
||||
->andReturn($host . '/');
|
||||
expect('wp_remote_get')
|
||||
->andReturnUsing(
|
||||
function ($url, $args) use ($json, $key, $secret, $host) {
|
||||
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
|
||||
return false;
|
||||
}
|
||||
if ($args['method'] !== 'POST') {
|
||||
return false;
|
||||
}
|
||||
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'body' => $json,
|
||||
];
|
||||
}
|
||||
);
|
||||
expect('is_wp_error')
|
||||
->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')
|
||||
->andReturn(200);
|
||||
|
||||
$token = $bearer->bearer();
|
||||
$this->assertEquals("abc", $token->token());
|
||||
$this->assertTrue($token->isValid());
|
||||
}
|
||||
|
||||
public function testNoTokenCached()
|
||||
{
|
||||
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
|
||||
$cache = Mockery::mock(CacheInterface::class);
|
||||
$cache
|
||||
->expects('get')
|
||||
->andReturn('');
|
||||
$cache
|
||||
->expects('set');
|
||||
$host = 'https://example.com';
|
||||
$key = 'key';
|
||||
$secret = 'secret';
|
||||
$logger = Mockery::mock(LoggerInterface::class);
|
||||
$logger->shouldNotReceive('log');
|
||||
|
||||
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger);
|
||||
|
||||
expect('trailingslashit')
|
||||
->with($host)
|
||||
->andReturn($host . '/');
|
||||
expect('wp_remote_get')
|
||||
->andReturnUsing(
|
||||
function ($url, $args) use ($json, $key, $secret, $host) {
|
||||
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
|
||||
return false;
|
||||
}
|
||||
if ($args['method'] !== 'POST') {
|
||||
return false;
|
||||
}
|
||||
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'body' => $json,
|
||||
];
|
||||
}
|
||||
);
|
||||
expect('is_wp_error')
|
||||
->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')
|
||||
->andReturn(200);
|
||||
|
||||
$token = $bearer->bearer();
|
||||
$this->assertEquals("abc", $token->token());
|
||||
$this->assertTrue($token->isValid());
|
||||
}
|
||||
|
||||
public function testCachedTokenIsStillValid()
|
||||
{
|
||||
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
|
||||
$cache = Mockery::mock(CacheInterface::class);
|
||||
$cache
|
||||
->expects('get')
|
||||
->andReturn($json);
|
||||
$host = 'https://example.com';
|
||||
$key = 'key';
|
||||
$secret = 'secret';
|
||||
$logger = Mockery::mock(LoggerInterface::class);
|
||||
$logger->shouldNotReceive('log');
|
||||
|
||||
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger);
|
||||
|
||||
$token = $bearer->bearer();
|
||||
$this->assertEquals("abc", $token->token());
|
||||
$this->assertTrue($token->isValid());
|
||||
}
|
||||
|
||||
public function testExceptionThrownOnError()
|
||||
{
|
||||
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
|
||||
$cache = Mockery::mock(CacheInterface::class);
|
||||
$cache
|
||||
->expects('get')
|
||||
->andReturn('');
|
||||
$host = 'https://example.com';
|
||||
$key = 'key';
|
||||
$secret = 'secret';
|
||||
$logger = Mockery::mock(LoggerInterface::class);
|
||||
$logger->shouldReceive('log');
|
||||
|
||||
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger);
|
||||
|
||||
expect('trailingslashit')
|
||||
->with($host)
|
||||
->andReturn($host . '/');
|
||||
expect('wp_remote_get')
|
||||
->andReturnUsing(
|
||||
function ($url, $args) use ($json, $key, $secret, $host) {
|
||||
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
|
||||
return false;
|
||||
}
|
||||
if ($args['method'] !== 'POST') {
|
||||
return false;
|
||||
}
|
||||
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'body' => $json,
|
||||
];
|
||||
}
|
||||
);
|
||||
expect('is_wp_error')
|
||||
->andReturn(true);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$bearer->bearer();
|
||||
}
|
||||
|
||||
public function testExceptionThrownBecauseOfHttpStatusCode()
|
||||
{
|
||||
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
|
||||
$cache = Mockery::mock(CacheInterface::class);
|
||||
$cache
|
||||
->expects('get')
|
||||
->andReturn('');
|
||||
$host = 'https://example.com';
|
||||
$key = 'key';
|
||||
$secret = 'secret';
|
||||
$logger = Mockery::mock(LoggerInterface::class);
|
||||
$logger->shouldReceive('log');
|
||||
|
||||
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger);
|
||||
|
||||
expect('trailingslashit')
|
||||
->with($host)
|
||||
->andReturn($host . '/');
|
||||
expect('wp_remote_get')
|
||||
->andReturnUsing(
|
||||
function ($url, $args) use ($json, $key, $secret, $host) {
|
||||
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
|
||||
return false;
|
||||
}
|
||||
if ($args['method'] !== 'POST') {
|
||||
return false;
|
||||
}
|
||||
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'body' => $json,
|
||||
];
|
||||
}
|
||||
);
|
||||
expect('is_wp_error')
|
||||
->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')
|
||||
->andReturn(500);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$bearer->bearer();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue