From 53ed891fd2c176800c5453283ece581d6722d4d9 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 10:50:29 +0100 Subject: [PATCH 1/6] Fix HTTP mocking test The expected output changed because there are new table columns. Figured just changing it to CSV output makes the test more stable --- features/http-mocking.feature | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/features/http-mocking.feature b/features/http-mocking.feature index ac5a42c91..e8891a802 100644 --- a/features/http-mocking.feature +++ b/features/http-mocking.feature @@ -77,10 +77,11 @@ Feature: HTTP request mocking ] """ - When I run `wp cli check-update` - Then STDOUT should be a table containing rows: - | version | update_type | package_url | - | 999.9.9 | major | https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar | + When I try `wp cli check-update --format=csv` + Then STDOUT should contain: + """ + 999.9.9,major,https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar,available + """ Scenario: Mock HTTP request in WordPress Given a WP install From 43e975db438b1a1a910e3aa9bacfffa7373720ab Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 11:04:46 +0100 Subject: [PATCH 2/6] Support Requests v1 for request mocking --- src/Context/GivenStepDefinitions.php | 72 ++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index b8e49c068..60a13d28d 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -113,12 +113,11 @@ public function given_a_request_to_a_url_respond_with_file( $url_or_pattern, PyS $mock_file_contents = <<request( \$url, \$headers, \$data, \$options ); + if ( class_exists( 'Requests_Transport_cURL' ) ) { + return ( new Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options ); + } + + return ( new WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options ); } public function request_multiple( \$requests, \$options ) { @@ -145,6 +148,16 @@ public static function test( \$capabilities = array() ) { } } +if ( interface_exists( 'Requests_Transport' ) ) { + class WP_CLI_Tests_Mock_Requests_Transport implements Requests_Transport { + use WP_CLI_Tests_Mock_Requests_Trait; + } +} else { + class WP_CLI_Tests_Mock_Requests_Transport implements WpOrg\Requests\Transport { + use WP_CLI_Tests_Mock_Requests_Trait; + } +} + WP_CLI::add_hook( 'http_request_options', static function( \$options ) { @@ -165,20 +178,39 @@ static function( \$pre, \$parsed_args, \$url ) { if ( false !== \$pos ) { \$response = substr( \$response, 0, \$pos ) . "\r\n\r\n" . substr( \$response, \$pos + 2 ); } - Requests::parse_multiple( - \$response, - array( - 'url' => \$url, - 'headers' => array(), - 'data' => array(), - 'options' => array_merge( - Requests::OPTION_DEFAULTS, - array( - 'hooks' => new Hooks(), - ) - ), - ) - ); + + if ( class_exists( '\Requests' ) ) { + \Requests::parse_multiple( + \$response, + array( + 'url' => \$url, + 'headers' => array(), + 'data' => array(), + 'options' => array_merge( + Requests::OPTION_DEFAULTS, + array( + 'hooks' => new Requests_Hooks(), + ) + ), + ) + ); + } else { + WpOrg\Requests\Requests::parse_multiple( + \$response, + array( + 'url' => \$url, + 'headers' => array(), + 'data' => array(), + 'options' => array_merge( + Requests::OPTION_DEFAULTS, + array( + 'hooks' => new WpOrg\Requests\Hooks(), + ) + ), + ) + ); + } + return array( 'headers' => \$response->headers->getAll(), From 2668bbcf0dfd15df29325ac07aa2b70b8d2bd266 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 11:11:08 +0100 Subject: [PATCH 3/6] Swap around --- src/Context/GivenStepDefinitions.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 60a13d28d..6699ec60c 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -132,11 +132,11 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options } } - if ( class_exists( 'Requests_Transport_cURL' ) ) { - return ( new Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options ); + if ( class_exists( '\WpOrg\Requests\Transport\Curl' ) ) { + return ( new WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options ); } - return ( new WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options ); + return ( new Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options ); } public function request_multiple( \$requests, \$options ) { @@ -148,12 +148,12 @@ public static function test( \$capabilities = array() ) { } } -if ( interface_exists( 'Requests_Transport' ) ) { - class WP_CLI_Tests_Mock_Requests_Transport implements Requests_Transport { +if ( interface_exists( '\WpOrg\Requests\Transport' ) ) { + class WP_CLI_Tests_Mock_Requests_Transport implements WpOrg\Requests\Transport { use WP_CLI_Tests_Mock_Requests_Trait; } } else { - class WP_CLI_Tests_Mock_Requests_Transport implements WpOrg\Requests\Transport { + class WP_CLI_Tests_Mock_Requests_Transport implements Requests_Transport { use WP_CLI_Tests_Mock_Requests_Trait; } } @@ -179,8 +179,8 @@ static function( \$pre, \$parsed_args, \$url ) { \$response = substr( \$response, 0, \$pos ) . "\r\n\r\n" . substr( \$response, \$pos + 2 ); } - if ( class_exists( '\Requests' ) ) { - \Requests::parse_multiple( + if ( class_exists( '\WpOrg\Requests\Requests' ) ) { + WpOrg\Requests\Requests::parse_multiple( \$response, array( 'url' => \$url, @@ -189,13 +189,13 @@ static function( \$pre, \$parsed_args, \$url ) { 'options' => array_merge( Requests::OPTION_DEFAULTS, array( - 'hooks' => new Requests_Hooks(), + 'hooks' => new WpOrg\Requests\Hooks(), ) ), ) ); } else { - WpOrg\Requests\Requests::parse_multiple( + \Requests::parse_multiple( \$response, array( 'url' => \$url, @@ -204,14 +204,13 @@ static function( \$pre, \$parsed_args, \$url ) { 'options' => array_merge( Requests::OPTION_DEFAULTS, array( - 'hooks' => new WpOrg\Requests\Hooks(), + 'hooks' => new Requests_Hooks(), ) ), ) ); } - return array( 'headers' => \$response->headers->getAll(), 'body' => \$response->body, From 0eee344f19824f31da6022359e11ade6d7094f6f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 11:15:43 +0100 Subject: [PATCH 4/6] Full namespace --- src/Context/GivenStepDefinitions.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 6699ec60c..3a23a565d 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -133,10 +133,10 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options } if ( class_exists( '\WpOrg\Requests\Transport\Curl' ) ) { - return ( new WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options ); + return ( new \WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options ); } - return ( new Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options ); + return ( new \Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options ); } public function request_multiple( \$requests, \$options ) { @@ -149,11 +149,11 @@ public static function test( \$capabilities = array() ) { } if ( interface_exists( '\WpOrg\Requests\Transport' ) ) { - class WP_CLI_Tests_Mock_Requests_Transport implements WpOrg\Requests\Transport { + class WP_CLI_Tests_Mock_Requests_Transport implements \WpOrg\Requests\Transport { use WP_CLI_Tests_Mock_Requests_Trait; } } else { - class WP_CLI_Tests_Mock_Requests_Transport implements Requests_Transport { + class WP_CLI_Tests_Mock_Requests_Transport implements \Requests_Transport { use WP_CLI_Tests_Mock_Requests_Trait; } } @@ -187,7 +187,7 @@ static function( \$pre, \$parsed_args, \$url ) { 'headers' => array(), 'data' => array(), 'options' => array_merge( - Requests::OPTION_DEFAULTS, + WpOrg\Requests\Requests::OPTION_DEFAULTS, array( 'hooks' => new WpOrg\Requests\Hooks(), ) From 9dfb1f2891d08ad48b15fcbf586f0de62d6732b7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 11:19:51 +0100 Subject: [PATCH 5/6] Remove non-existent constant --- src/Context/GivenStepDefinitions.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 3a23a565d..61fc114f1 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -201,11 +201,8 @@ static function( \$pre, \$parsed_args, \$url ) { 'url' => \$url, 'headers' => array(), 'data' => array(), - 'options' => array_merge( - Requests::OPTION_DEFAULTS, - array( - 'hooks' => new Requests_Hooks(), - ) + 'options' => array( + 'hooks' => new Requests_Hooks(), ), ) ); From a65c45cd0c6cfe2dc48fe2f55aef8698b6c26396 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 5 Mar 2025 11:25:00 +0100 Subject: [PATCH 6/6] Manually add defaults --- src/Context/GivenStepDefinitions.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 61fc114f1..615999b7b 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -202,7 +202,12 @@ static function( \$pre, \$parsed_args, \$url ) { 'headers' => array(), 'data' => array(), 'options' => array( - 'hooks' => new Requests_Hooks(), + 'blocking' => true, + 'filename' => false, + 'follow_redirects' => true, + 'redirected' => 0, + 'redirects' => 10, + 'hooks' => new Requests_Hooks(), ), ) );