Skip to content

Commit 9df02af

Browse files
committed
Defer mock Requests transport instantiation until http_request_options
1 parent 791f3c1 commit 9df02af

3 files changed

Lines changed: 174 additions & 19 deletions

File tree

‎phpcs.xml.dist‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,8 @@
105105

106106
<!-- These are stand-alone files that are copied into the test run directory and
107107
executed by the child WP-CLI process, not loaded as part of this package.
108-
They declare a class once per supported Requests major version, and their
109-
method signatures are dictated by the transport interface they implement,
108+
Their method signatures are dictated by the transport interface they implement,
110109
so unused parameters cannot be dropped. -->
111-
<rule ref="Generic.Files.OneObjectStructurePerFile">
112-
<exclude-pattern>*/src/Context/templates/*</exclude-pattern>
113-
</rule>
114-
<rule ref="Generic.Classes.DuplicateClassName">
115-
<exclude-pattern>*/src/Context/templates/*</exclude-pattern>
116-
</rule>
117110
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter">
118111
<exclude-pattern>*/src/Context/templates/*</exclude-pattern>
119112
</rule>

‎src/Context/templates/mock-requests.php‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ public static function test( $capabilities = array() ) {
4646
}
4747
}
4848

49-
if ( interface_exists( '\WpOrg\Requests\Transport' ) ) {
50-
class WP_CLI_Tests_Mock_Requests_Transport implements \WpOrg\Requests\Transport {
51-
use WP_CLI_Tests_Mock_Requests_Trait;
52-
}
53-
} else {
54-
class WP_CLI_Tests_Mock_Requests_Transport implements \Requests_Transport {
55-
use WP_CLI_Tests_Mock_Requests_Trait;
56-
}
57-
}
58-
5949
WP_CLI::add_hook(
6050
'http_request_options',
6151
static function ( $options ) {
62-
$options['transport'] = new WP_CLI_Tests_Mock_Requests_Transport();
52+
if ( class_exists( '\WP_CLI\RequestsLibrary' ) && method_exists( '\WP_CLI\RequestsLibrary', 'register_autoloader' ) ) {
53+
\WP_CLI\RequestsLibrary::register_autoloader();
54+
}
55+
56+
if ( interface_exists( '\WpOrg\Requests\Transport' ) ) {
57+
$options['transport'] = new class() implements \WpOrg\Requests\Transport {
58+
use WP_CLI_Tests_Mock_Requests_Trait;
59+
};
60+
} else {
61+
$options['transport'] = new class() implements \Requests_Transport {
62+
use WP_CLI_Tests_Mock_Requests_Trait;
63+
};
64+
}
65+
6366
return $options;
6467
}
6568
);
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace WP_CLI\Tests\Tests;
4+
5+
use WP_CLI\Tests\TestCase;
6+
7+
class TestMockRequestsTemplate extends TestCase {
8+
9+
private function get_template_path(): string {
10+
return dirname( __DIR__, 2 ) . '/src/Context/templates/mock-requests.php';
11+
}
12+
13+
/**
14+
* Run a PHP snippet in a clean subprocess without any preloaded Requests classes.
15+
*
16+
* @param string $code PHP code to execute.
17+
* @return array{exit_code: int, output: string}
18+
*/
19+
private function run_php_snippet( string $code ): array {
20+
$cmd = escapeshellarg( PHP_BINARY ) . ' -r ' . escapeshellarg( $code ) . ' 2>&1';
21+
exec( $cmd, $lines, $exit_code );
22+
23+
return [
24+
'exit_code' => $exit_code,
25+
'output' => implode( "\n", $lines ),
26+
];
27+
}
28+
29+
public function test_requiring_template_does_not_trigger_requests_autoloading(): void {
30+
$template = var_export( $this->get_template_path(), true );
31+
$code = <<<PHP
32+
class WP_CLI {
33+
public static \$hooks = [];
34+
public static function add_hook( \$when, \$callback ) {
35+
self::\$hooks[ \$when ] = \$callback;
36+
}
37+
public static function add_wp_hook( \$when, \$callback, \$priority = 10, \$accepted_args = 1 ) {}
38+
}
39+
\$autoloaded = [];
40+
spl_autoload_register( static function ( \$class ) use ( &\$autoloaded ) {
41+
\$autoloaded[] = \$class;
42+
} );
43+
require {$template};
44+
echo json_encode( [
45+
'autoloaded' => \$autoloaded,
46+
'has_hook' => isset( WP_CLI::\$hooks['http_request_options'] ),
47+
] );
48+
PHP;
49+
50+
$result = $this->run_php_snippet( $code );
51+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
52+
53+
/** @var array{autoloaded: string[], has_hook: bool}|null $data */
54+
$data = json_decode( $result['output'], true );
55+
$this->assertIsArray( $data );
56+
$this->assertSame( [], $data['autoloaded'] );
57+
$this->assertTrue( $data['has_hook'] );
58+
}
59+
60+
public function test_http_request_options_hook_creates_v2_transport_lazily(): void {
61+
$template = var_export( $this->get_template_path(), true );
62+
$code = <<<PHP
63+
namespace WpOrg\Requests {
64+
interface Transport {
65+
public function request( \$url, \$headers = [], \$data = [], \$options = [] );
66+
public function request_multiple( \$requests, \$options );
67+
public static function test( \$capabilities = [] );
68+
}
69+
}
70+
namespace {
71+
class WP_CLI {
72+
public static \$hooks = [];
73+
public static function add_hook( \$when, \$callback ) {
74+
self::\$hooks[ \$when ] = \$callback;
75+
}
76+
public static function add_wp_hook( \$when, \$callback, \$priority = 10, \$accepted_args = 1 ) {}
77+
}
78+
require {$template};
79+
\$options = ( WP_CLI::\$hooks['http_request_options'] )( [] );
80+
echo ( \$options['transport'] instanceof \WpOrg\Requests\Transport ) ? 'v2-ok' : 'fail';
81+
}
82+
PHP;
83+
84+
$result = $this->run_php_snippet( $code );
85+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
86+
$this->assertSame( 'v2-ok', $result['output'] );
87+
}
88+
89+
public function test_http_request_options_hook_creates_v1_transport_lazily(): void {
90+
$template = var_export( $this->get_template_path(), true );
91+
$code = <<<PHP
92+
interface Requests_Transport {
93+
public function request( \$url, \$headers = [], \$data = [], \$options = [] );
94+
public function request_multiple( \$requests, \$options );
95+
public static function test( \$capabilities = [] );
96+
}
97+
class WP_CLI {
98+
public static \$hooks = [];
99+
public static function add_hook( \$when, \$callback ) {
100+
self::\$hooks[ \$when ] = \$callback;
101+
}
102+
public static function add_wp_hook( \$when, \$callback, \$priority = 10, \$accepted_args = 1 ) {}
103+
}
104+
require {$template};
105+
\$options = ( WP_CLI::\$hooks['http_request_options'] )( [] );
106+
echo ( \$options['transport'] instanceof \Requests_Transport ) ? 'v1-ok' : 'fail';
107+
PHP;
108+
109+
$result = $this->run_php_snippet( $code );
110+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
111+
$this->assertSame( 'v1-ok', $result['output'] );
112+
}
113+
114+
public function test_http_request_options_hook_calls_requests_library_register_autoloader(): void {
115+
$template = var_export( $this->get_template_path(), true );
116+
$code = <<<PHP
117+
namespace WP_CLI {
118+
class RequestsLibrary {
119+
public static \$registered = false;
120+
public static function register_autoloader() {
121+
self::\$registered = true;
122+
spl_autoload_register( static function ( \$class ) {
123+
if ( 'WpOrg\\Requests\\Transport' === \$class ) {
124+
eval( 'namespace WpOrg\\Requests; interface Transport { public function request( \$url, \$headers = [], \$data = [], \$options = [] ); public function request_multiple( \$requests, \$options ); public static function test( \$capabilities = [] ); }' );
125+
}
126+
} );
127+
}
128+
}
129+
}
130+
namespace {
131+
class WP_CLI {
132+
public static \$hooks = [];
133+
public static function add_hook( \$when, \$callback ) {
134+
self::\$hooks[ \$when ] = \$callback;
135+
}
136+
public static function add_wp_hook( \$when, \$callback, \$priority = 10, \$accepted_args = 1 ) {}
137+
}
138+
require {$template};
139+
\$before = \WP_CLI\RequestsLibrary::\$registered;
140+
\$options = ( WP_CLI::\$hooks['http_request_options'] )( [] );
141+
echo json_encode( [
142+
'before' => \$before,
143+
'after' => \WP_CLI\RequestsLibrary::\$registered,
144+
'is_transport' => \$options['transport'] instanceof \WpOrg\Requests\Transport,
145+
] );
146+
}
147+
PHP;
148+
149+
$result = $this->run_php_snippet( $code );
150+
$this->assertSame( 0, $result['exit_code'], $result['output'] );
151+
152+
/** @var array{before: bool, after: bool, is_transport: bool}|null $data */
153+
$data = json_decode( $result['output'], true );
154+
$this->assertIsArray( $data );
155+
$this->assertFalse( $data['before'] );
156+
$this->assertTrue( $data['after'] );
157+
$this->assertTrue( $data['is_transport'] );
158+
}
159+
}

0 commit comments

Comments
 (0)