@@ -113,12 +113,11 @@ public function given_a_request_to_a_url_respond_with_file( $url_or_pattern, PyS
113
113
114
114
$ mock_file_contents = <<<FILE
115
115
<?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
+ */
120
119
121
- class WP_CLI_Tests_Mock_Requests_Transport implements Transport {
120
+ trait WP_CLI_Tests_Mock_Requests_Trait {
122
121
public function request( \$url, \$headers = array(), \$data = array(), \$options = array() ) {
123
122
\$mocked_requests = $ mocked_requests;
124
123
@@ -133,7 +132,11 @@ public function request( \$url, \$headers = array(), \$data = array(), \$options
133
132
}
134
133
}
135
134
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 );
137
140
}
138
141
139
142
public function request_multiple( \$requests, \$options ) {
@@ -145,6 +148,16 @@ public static function test( \$capabilities = array() ) {
145
148
}
146
149
}
147
150
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
+
148
161
WP_CLI::add_hook(
149
162
'http_request_options',
150
163
static function( \$options ) {
@@ -165,20 +178,40 @@ static function( \$pre, \$parsed_args, \$url ) {
165
178
if ( false !== \$pos ) {
166
179
\$response = substr( \$response, 0, \$pos ) . " \r\n\r\n" . substr( \$response, \$pos + 2 );
167
180
}
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
+ }
182
215
183
216
return array(
184
217
'headers' => \$response->headers->getAll(),
0 commit comments