Skip to content

Commit f1d4366

Browse files
authored
Defer mock Requests transport instantiation until http_request_options (#432)
1 parent 791f3c1 commit f1d4366

2 files changed

Lines changed: 15 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
);

0 commit comments

Comments
 (0)