From 69d1ba9b1d082ba7dbd178eafba4dbd182771eeb Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 15:25:41 +0300 Subject: [PATCH] fix some unit tests --- .../APIHandler/AbstractSafeAPIClient.php | 34 +++++++++++++------ inc/Engine/License/API/PricingClient.php | 2 +- .../API/PricingClient/getPricingData.php | 3 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index d757ddcb36..cbfa5be45d 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -34,8 +34,8 @@ abstract protected function get_api_url(); * @param array $params The request parameters. * @return mixed The response from the API. */ - public function send_get_request( $params = [] ) { - return $this->send_request( 'GET', $params ); + public function send_get_request( $params = [], $safe = false ) { + return $this->send_request( 'GET', $params, $safe ); } /** @@ -44,8 +44,8 @@ public function send_get_request( $params = [] ) { * @param array $params The request parameters. * @return mixed The response from the API. */ - public function send_post_request( $params ) { - return $this->send_request( 'POST', $params ); + public function send_post_request( $params = [], $safe = false ) { + return $this->send_request( 'POST', $params, $safe ); } /** @@ -55,19 +55,16 @@ public function send_post_request( $params ) { * @param array $params The request parameters. * @return mixed The response from the API, or WP_Error if a timeout is active. */ - private function send_request( $method, $params ) { + private function send_request( $method, $params = [], $safe = false ) { $api_url = $this->get_api_url(); if ( true === get_transient( $this->get_transient_key() . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } - if ( empty( $params['body'] ) ) { - $params['body'] = []; - } - $params['method'] = strtoupper( $method ); - $response = wp_remote_request( $api_url, $params ); + + $response = $this->send_remote_request( $api_url, $method, $params, $safe ); if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); @@ -112,4 +109,21 @@ protected function delete_timeout_transients() { delete_transient( $transient_key . '_timeout_active' ); delete_transient( $transient_key . '_timeout' ); } + + private function send_remote_request( $api_url, $method, $params, $safe ) { + if ( ! $safe ) { + return wp_remote_request( $api_url, $params ); + } + + unset( $params['method'] ); + + switch ( $method ) { + case 'GET': + return wp_safe_remote_get( $api_url, $params ); + case 'POST': + return wp_safe_remote_post( $api_url, $params ); + } + + new WP_Error( 400, __( 'Not valid request type.', 'rocket' ) ); + } } diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index ab9d3cfc50..0a0601cdbf 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -64,7 +64,7 @@ public function get_pricing_data() { * @return bool|object */ private function get_raw_pricing_data() { - $response = $this->send_get_request(); + $response = $this->send_get_request( [], true ); if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { return false; diff --git a/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php b/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php index 72595b52de..50c9defa49 100644 --- a/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php +++ b/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php @@ -17,6 +17,7 @@ class GetPricingData extends TestCase { * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $expected ) { + $this->stubTranslationFunctions(); $client = new PricingClient(); Functions\expect( 'get_transient' ) @@ -48,7 +49,7 @@ public function testShouldReturnExpected( $config, $expected ) { if ( false !== $config['response'] ) { Functions\expect( 'wp_safe_remote_get' ) ->once() - ->with( PricingClient::PRICING_ENDPOINT ) + ->with( PricingClient::PRICING_ENDPOINT, [] ) ->andReturn( $config['response'] ); if (