Skip to content

Commit

Permalink
Avoid external HTTP calls in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed May 6, 2024
1 parent 6489de5 commit a6f1836
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions tests/phpunit/integration/tests/AMP/Optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,62 @@
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\Transformer\MinifyHtml;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\Transformer\OptimizeViewport;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\Transformer\ReorderHead;
use WP_Error;

/**
* @coversDefaultClass \Google\Web_Stories\AMP\Optimization
*/
class Optimization extends TestCase {
public function set_up(): void {
parent::set_up();

add_filter( 'pre_http_request', [ $this, 'mock_http_request' ], 10, 3 );
}

public function tear_down(): void {
remove_filter( 'pre_http_request', [ $this, 'mock_http_request' ] );

parent::tear_down();
}

/**
* Intercept link processing requests and mock responses.
*
* @param mixed $preempt Whether to preempt an HTTP request's return value. Default false.
* @param mixed $r HTTP request arguments.
* @param string $url The request URL.
* @return mixed|WP_Error Response data.
*/
public function mock_http_request( $preempt, $r, string $url ) {
if ( 'https://cdn.ampproject.org/rtv/metadata' === $url ) {
return [
'headers' => [
'content-type' => 'application/json',
'content-length' => 100,
],
'response' => [
'code' => 200,
'body' => file_get_contents( WEBSTORIES_PLUGIN_DIR_PATH . '/third-party/vendor/ampproject/amp-toolbox/resources/local_fallback/rtv/metadata' ),
],
];
}

if ( 'https://cdn.ampproject.org/v0.css' === $url ) {
return [
'headers' => [
'content-type' => 'text/css',
'content-length' => 100,
],
'response' => [
'code' => 200,
'body' => file_get_contents( WEBSTORIES_PLUGIN_DIR_PATH . '/third-party/vendor/ampproject/amp-toolbox/resources/local_fallback/v0.css' ),
],
];
}

return $preempt;
}

/**
* @covers ::optimize_document
* @covers ::get_optimizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function mock_http_request( $preempt, $r, string $url ) {
}

// URL_500
if ( 'https://93.184.216.34.com/500/test.jpg' === $url ) {
if ( self::URL_500 === $url ) {
return [
'headers' => [
'content-type' => 'image/jpeg',
Expand Down

0 comments on commit a6f1836

Please sign in to comment.