Fix unit tests (WIP)

This commit is contained in:
dinamiko 2021-09-14 17:46:33 +02:00
parent 5ca2f9d69f
commit 12b9905c60
3 changed files with 48 additions and 17 deletions

View file

@ -40,13 +40,7 @@ trait RequestTrait {
} }
$response = wp_remote_get( $url, $args ); $response = wp_remote_get( $url, $args );
$this->logger->debug( $this->request_response_string( $url, $args, $response ) );
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 ) );
return $response; return $response;
} }

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication; namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\TestCase; use WooCommerce\PayPalCommerce\ApiClient\TestCase;
@ -28,10 +29,12 @@ class PayPalBearerTest extends TestCase
$key = 'key'; $key = 'key';
$secret = 'secret'; $secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log'); $logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class); $settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true); $settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn(''); $settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings); $bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -40,7 +43,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/'); ->andReturn($host . '/');
expect('wp_remote_get') expect('wp_remote_get')
->andReturnUsing( ->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') { if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false; return false;
} }
@ -53,6 +56,7 @@ class PayPalBearerTest extends TestCase
return [ return [
'body' => $json, 'body' => $json,
'headers' => $headers
]; ];
} }
); );
@ -80,10 +84,12 @@ class PayPalBearerTest extends TestCase
$key = 'key'; $key = 'key';
$secret = 'secret'; $secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log'); $logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class); $settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true); $settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn(''); $settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings); $bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -92,7 +98,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/'); ->andReturn($host . '/');
expect('wp_remote_get') expect('wp_remote_get')
->andReturnUsing( ->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') { if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false; return false;
} }
@ -105,6 +111,7 @@ class PayPalBearerTest extends TestCase
return [ return [
'body' => $json, 'body' => $json,
'headers' => $headers,
]; ];
} }
); );
@ -153,9 +160,12 @@ class PayPalBearerTest extends TestCase
$secret = 'secret'; $secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $logger->shouldReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class); $settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true); $settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn(''); $settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings); $bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -164,7 +174,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/'); ->andReturn($host . '/');
expect('wp_remote_get') expect('wp_remote_get')
->andReturnUsing( ->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') { if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false; return false;
} }
@ -177,6 +187,7 @@ class PayPalBearerTest extends TestCase
return [ return [
'body' => $json, 'body' => $json,
'headers' => $headers,
]; ];
} }
); );
@ -199,9 +210,12 @@ class PayPalBearerTest extends TestCase
$secret = 'secret'; $secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class); $logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log'); $logger->shouldReceive('log');
$logger->shouldReceive('debug');
$settings = Mockery::mock(Settings::class); $settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true); $settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn(''); $settings->shouldReceive('get')->andReturn('');
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings); $bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
@ -210,7 +224,7 @@ class PayPalBearerTest extends TestCase
->andReturn($host . '/'); ->andReturn($host . '/');
expect('wp_remote_get') expect('wp_remote_get')
->andReturnUsing( ->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') { if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false; return false;
} }
@ -223,6 +237,7 @@ class PayPalBearerTest extends TestCase
return [ return [
'body' => $json, 'body' => $json,
'headers' => $headers,
]; ];
} }
); );

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\Token; use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -11,6 +12,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\TestCase; use WooCommerce\PayPalCommerce\ApiClient\TestCase;
use Mockery; use Mockery;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class IdentityTokenTest extends TestCase class IdentityTokenTest extends TestCase
{ {
@ -40,10 +42,18 @@ class IdentityTokenTest extends TestCase
$this->bearer $this->bearer
->expects('bearer')->andReturn($token); ->expects('bearer')->andReturn($token);
$rawResponse = ['body' => '{"client_token":"abc123", "expires_in":3600}'];
$host = $this->host; $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') 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') { if ($url !== $host . 'v1/identity/generate-token') {
return false; return false;
} }
@ -65,6 +75,7 @@ class IdentityTokenTest 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);
when('wc_print_r')->returnArg();
$result = $this->sut->generate_for_customer(1); $result = $this->sut->generate_for_customer(1);
$this->assertInstanceOf(Token::class, $result); $this->assertInstanceOf(Token::class, $result);
@ -78,9 +89,13 @@ class IdentityTokenTest extends TestCase
$this->bearer $this->bearer
->expects('bearer')->andReturn($token); ->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); expect('is_wp_error')->andReturn(true);
when('wc_print_r')->returnArg();
$this->logger->shouldReceive('log'); $this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$this->sut->generate_for_customer(1); $this->sut->generate_for_customer(1);
@ -94,10 +109,17 @@ class IdentityTokenTest extends TestCase
$this->bearer $this->bearer
->expects('bearer')->andReturn($token); ->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('is_wp_error')->andReturn(false);
expect('wp_remote_retrieve_response_code')->andReturn(500); expect('wp_remote_retrieve_response_code')->andReturn(500);
when('wc_print_r')->returnArg();
$this->logger->shouldReceive('log'); $this->logger->shouldReceive('log');
$this->logger->shouldReceive('debug');
$this->expectException(PayPalApiException::class); $this->expectException(PayPalApiException::class);
$this->sut->generate_for_customer(1); $this->sut->generate_for_customer(1);