Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions features/http-mocking.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 53 additions & 20 deletions src/Context/GivenStepDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ public function given_a_request_to_a_url_respond_with_file( $url_or_pattern, PyS

$mock_file_contents = <<<FILE
<?php
use WpOrg\Requests\Hooks;
use WpOrg\Requests\Transport;
use WpOrg\Requests\Transport\Curl;
use WpOrg\Requests\Requests;
/**
* HTTP request mocking supporting both Requests v1 and v2.
*/

class WP_CLI_Tests_Mock_Requests_Transport implements Transport {
trait WP_CLI_Tests_Mock_Requests_Trait {
public function request( \$url, \$headers = array(), \$data = array(), \$options = array() ) {
\$mocked_requests = $mocked_requests;

Expand All @@ -133,7 +132,11 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options
}
}

return (new 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 \Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options );
}

public function request_multiple( \$requests, \$options ) {
Expand All @@ -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 ) {
Expand All @@ -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(),
Expand Down