|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Regression test for the image pull WordPress HTTP fallback. |
| 4 | + * |
| 5 | + * @package LiteSpeed |
| 6 | + */ |
| 7 | + |
| 8 | +namespace LiteSpeed; |
| 9 | + |
| 10 | +define( 'WPINC', 'regression-test' ); |
| 11 | + |
| 12 | +require __DIR__ . '/wp-error-stub.php'; |
| 13 | +require dirname( __DIR__, 2 ) . '/src/img-optm-pull.trait.php'; |
| 14 | + |
| 15 | + /** |
| 16 | + * Expose the response normalizer without loading WordPress. |
| 17 | + */ |
| 18 | +final class Img_Optm_Pull_Harness { |
| 19 | + use Img_Optm_Pull; |
| 20 | + |
| 21 | + /** |
| 22 | + * Discard debug output during the regression test. |
| 23 | + * |
| 24 | + * @param string $message Debug message. |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + public static function debug( $message ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Normalize a simulated HTTP response. |
| 32 | + * |
| 33 | + * @param mixed $response Simulated response. |
| 34 | + * @param string $url Requested URL. |
| 35 | + * @return object |
| 36 | + */ |
| 37 | + public function parse( $response, $url ) { |
| 38 | + return $this->_parse_wp_pull_response( $response, $url ); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +$test_error = new class() { |
| 43 | + /** |
| 44 | + * Return the simulated network error. |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + public function get_error_message() { |
| 49 | + return 'simulated network failure'; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +$harness = new Img_Optm_Pull_Harness(); |
| 54 | +$result = $harness->parse( $test_error, 'https://example.test/image.jpg' ); |
| 55 | + |
| 56 | +if ( false !== $result->success || 0 !== $result->status_code || null !== $result->body ) { |
| 57 | + throw new \RuntimeException( 'WP_Error responses must remain failed without array access.' ); |
| 58 | +} |
| 59 | + |
| 60 | +$result = $harness->parse( |
| 61 | + [ |
| 62 | + 'response' => [ 'code' => 200 ], |
| 63 | + 'body' => 'image-data', |
| 64 | + ], |
| 65 | + 'https://example.test/image.jpg' |
| 66 | +); |
| 67 | + |
| 68 | +if ( true !== $result->success || 200 !== $result->status_code || 'image-data' !== $result->body ) { |
| 69 | + throw new \RuntimeException( 'Successful WordPress HTTP responses must retain status and body.' ); |
| 70 | +} |
| 71 | + |
| 72 | +echo "img-optm WP_Error regression: ok\n"; |
0 commit comments