|
| 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