Skip to content

Commit 0ebd313

Browse files
committed
Support Requests v1 for request mocking
1 parent 7756a91 commit 0ebd313

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

src/Context/GivenStepDefinitions.php

Lines changed: 52 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( 'Requests_Transport_cURL' ) ) {
136+
return ( new Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options );
137+
}
138+
139+
return ( new WpOrg\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( 'Requests_Transport' ) ) {
152+
class WP_CLI_Tests_Mock_Requests_Transport implements Requests_Transport {
153+
use WP_CLI_Tests_Mock_Requests_Trait;
154+
}
155+
} else {
156+
class WP_CLI_Tests_Mock_Requests_Transport implements WpOrg\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,39 @@ 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( '\Requests' ) ) {
183+
\Requests::parse_multiple(
184+
\$response,
185+
array(
186+
'url' => \$url,
187+
'headers' => array(),
188+
'data' => array(),
189+
'options' => array_merge(
190+
Requests::OPTION_DEFAULTS,
191+
array(
192+
'hooks' => new Requests_Hooks(),
193+
)
194+
),
195+
)
196+
);
197+
} else {
198+
WpOrg\Requests\Requests::parse_multiple(
199+
\$response,
200+
array(
201+
'url' => \$url,
202+
'headers' => array(),
203+
'data' => array(),
204+
'options' => array_merge(
205+
Requests::OPTION_DEFAULTS,
206+
array(
207+
'hooks' => new WpOrg\Requests\Hooks(),
208+
)
209+
),
210+
)
211+
);
212+
}
213+
182214
183215
return array(
184216
'headers' => \$response->headers->getAll(),

0 commit comments

Comments
 (0)