Skip to content

Commit 558698b

Browse files
authored
Merge pull request #240 from wp-cli/fix/http-mock-test
Fix HTTP mocking test / Requests v1 support
2 parents ca36e3b + a65c45c commit 558698b

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

features/http-mocking.feature

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ Feature: HTTP request mocking
7777
]
7878
"""
7979

80-
When I run `wp cli check-update`
81-
Then STDOUT should be a table containing rows:
82-
| version | update_type | package_url |
83-
| 999.9.9 | major | https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar |
80+
When I try `wp cli check-update --format=csv`
81+
Then STDOUT should contain:
82+
"""
83+
999.9.9,major,https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar,available
84+
"""
8485

8586
Scenario: Mock HTTP request in WordPress
8687
Given a WP install

src/Context/GivenStepDefinitions.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ public function given_a_request_to_a_url_respond_with_file( $url_or_pattern, PyS
113113

114114
$mock_file_contents = <<<FILE
115115
<?php
116-
use WpOrg\Requests\Hooks;
117-
use WpOrg\Requests\Transport;
118-
use WpOrg\Requests\Transport\Curl;
119-
use WpOrg\Requests\Requests;
116+
/**
117+
* HTTP request mocking supporting both Requests v1 and v2.
118+
*/
120119
121-
class WP_CLI_Tests_Mock_Requests_Transport implements Transport {
120+
trait WP_CLI_Tests_Mock_Requests_Trait {
122121
public function request( \$url, \$headers = array(), \$data = array(), \$options = array() ) {
123122
\$mocked_requests = $mocked_requests;
124123
@@ -133,7 +132,11 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options
133132
}
134133
}
135134
136-
return (new Curl())->request( \$url, \$headers, \$data, \$options );
135+
if ( class_exists( '\WpOrg\Requests\Transport\Curl' ) ) {
136+
return ( new \WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options );
137+
}
138+
139+
return ( new \Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options );
137140
}
138141
139142
public function request_multiple( \$requests, \$options ) {
@@ -145,6 +148,16 @@ public static function test( \$capabilities = array() ) {
145148
}
146149
}
147150
151+
if ( interface_exists( '\WpOrg\Requests\Transport' ) ) {
152+
class WP_CLI_Tests_Mock_Requests_Transport implements \WpOrg\Requests\Transport {
153+
use WP_CLI_Tests_Mock_Requests_Trait;
154+
}
155+
} else {
156+
class WP_CLI_Tests_Mock_Requests_Transport implements \Requests_Transport {
157+
use WP_CLI_Tests_Mock_Requests_Trait;
158+
}
159+
}
160+
148161
WP_CLI::add_hook(
149162
'http_request_options',
150163
static function( \$options ) {
@@ -165,20 +178,40 @@ static function( \$pre, \$parsed_args, \$url ) {
165178
if ( false !== \$pos ) {
166179
\$response = substr( \$response, 0, \$pos ) . "\r\n\r\n" . substr( \$response, \$pos + 2 );
167180
}
168-
Requests::parse_multiple(
169-
\$response,
170-
array(
171-
'url' => \$url,
172-
'headers' => array(),
173-
'data' => array(),
174-
'options' => array_merge(
175-
Requests::OPTION_DEFAULTS,
176-
array(
177-
'hooks' => new Hooks(),
178-
)
179-
),
180-
)
181-
);
181+
182+
if ( class_exists( '\WpOrg\Requests\Requests' ) ) {
183+
WpOrg\Requests\Requests::parse_multiple(
184+
\$response,
185+
array(
186+
'url' => \$url,
187+
'headers' => array(),
188+
'data' => array(),
189+
'options' => array_merge(
190+
WpOrg\Requests\Requests::OPTION_DEFAULTS,
191+
array(
192+
'hooks' => new WpOrg\Requests\Hooks(),
193+
)
194+
),
195+
)
196+
);
197+
} else {
198+
\Requests::parse_multiple(
199+
\$response,
200+
array(
201+
'url' => \$url,
202+
'headers' => array(),
203+
'data' => array(),
204+
'options' => array(
205+
'blocking' => true,
206+
'filename' => false,
207+
'follow_redirects' => true,
208+
'redirected' => 0,
209+
'redirects' => 10,
210+
'hooks' => new Requests_Hooks(),
211+
),
212+
)
213+
);
214+
}
182215
183216
return array(
184217
'headers' => \$response->headers->getAll(),

0 commit comments

Comments
 (0)