Skip to content

Commit

Permalink
fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Jun 27, 2024
1 parent ccf47a3 commit 69d1ba9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
34 changes: 24 additions & 10 deletions inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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 ) {

Check notice on line 113 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L113

Missing doc comment for function send_remote_request()
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' ) );
}
}
2 changes: 1 addition & 1 deletion inc/Engine/License/API/PricingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GetPricingData extends TestCase {
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected ) {
$this->stubTranslationFunctions();
$client = new PricingClient();

Functions\expect( 'get_transient' )
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 69d1ba9

Please sign in to comment.