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 diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index b8e49c068..615999b7b 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( '\WpOrg\Requests\Transport\Curl' ) ) { + 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 ) { @@ -145,6 +148,16 @@ public static function test( \$capabilities = array() ) { } } +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 \Requests_Transport { + use WP_CLI_Tests_Mock_Requests_Trait; + } +} + WP_CLI::add_hook( 'http_request_options', static function( \$options ) { @@ -165,20 +178,40 @@ 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( '\WpOrg\Requests\Requests' ) ) { + WpOrg\Requests\Requests::parse_multiple( + \$response, + array( + 'url' => \$url, + 'headers' => array(), + 'data' => array(), + 'options' => array_merge( + WpOrg\Requests\Requests::OPTION_DEFAULTS, + array( + 'hooks' => new WpOrg\Requests\Hooks(), + ) + ), + ) + ); + } else { + \Requests::parse_multiple( + \$response, + array( + 'url' => \$url, + 'headers' => array(), + 'data' => array(), + 'options' => array( + 'blocking' => true, + 'filename' => false, + 'follow_redirects' => true, + 'redirected' => 0, + 'redirects' => 10, + 'hooks' => new Requests_Hooks(), + ), + ) + ); + } return array( 'headers' => \$response->headers->getAll(),