mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
Fix unit tests (WIP)
This commit is contained in:
parent
5ca2f9d69f
commit
12b9905c60
3 changed files with 48 additions and 17 deletions
|
@ -40,13 +40,7 @@ trait RequestTrait {
|
|||
}
|
||||
|
||||
$response = wp_remote_get( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$this->logger->error( $response->get_error_code() . ' ' . $response->get_error_message() );
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->logger->info( $this->request_response_string( $url, $args, $response ) );
|
||||
$this->logger->debug( $this->request_response_string( $url, $args, $response ) );
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue