From 12b9905c604c9580d55dd619f49dc8576c4db553 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 14 Sep 2021 17:46:33 +0200 Subject: [PATCH] Fix unit tests (WIP) --- .../src/Endpoint/class-requesttrait.php | 8 +---- .../Authentication/PayPalBearerTest.php | 27 +++++++++++++---- .../ApiClient/Endpoint/IdentityTokenTest.php | 30 ++++++++++++++++--- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php b/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php index de49dd8fa..5e7674a4a 100644 --- a/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php +++ b/modules/ppcp-api-client/src/Endpoint/class-requesttrait.php @@ -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; } diff --git a/tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php b/tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php index 469c337d5..9b423866b 100644 --- a/tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php +++ b/tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php @@ -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, ]; } ); diff --git a/tests/PHPUnit/ApiClient/Endpoint/IdentityTokenTest.php b/tests/PHPUnit/ApiClient/Endpoint/IdentityTokenTest.php index 0e3371b64..1ff901070 100644 --- a/tests/PHPUnit/ApiClient/Endpoint/IdentityTokenTest.php +++ b/tests/PHPUnit/ApiClient/Endpoint/IdentityTokenTest.php @@ -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);