@@ -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+
148161WP_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