Skip to content

Commit ee32eb7

Browse files
committed
Add response mocking to speed up PHPUnit tests
1 parent 7606643 commit ee32eb7

20 files changed

+139
-23
lines changed

includes/options/class-amp-options-manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public static function validate_options( $new_options ) {
344344
$options[ OPTION::ANALYTICS ] = $new_analytics_option;
345345
}
346346

347-
if ( isset( $new_options[ Option::READER_THEME ] ) ) {
347+
if ( isset( $new_options[ Option::READER_THEME ] ) && $new_options[ Option::READER_THEME ] !== $options[ Option::READER_THEME ] ) {
348348
$reader_theme_slugs = wp_list_pluck( ( new ReaderThemes() )->get_themes(), 'slug' );
349349
if ( in_array( $new_options[ Option::READER_THEME ], $reader_theme_slugs, true ) ) {
350350
$options[ Option::READER_THEME ] = $new_options[ Option::READER_THEME ];

src/Admin/SiteHealth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public function has_page_caching( $use_previous_result = false ) {
392392
if ( $use_previous_result ) {
393393
$has_page_caching = get_transient( self::HAS_PAGE_CACHING_TRANSIENT_KEY );
394394
if ( is_wp_error( $has_page_caching ) ) {
395-
return $has_page_caching;
395+
return $has_page_caching;
396396
} elseif ( $has_page_caching ) {
397397
return ( 'yes' === $has_page_caching );
398398
}
@@ -428,7 +428,7 @@ private function check_for_page_caching() {
428428
}
429429

430430
for ( $i = 1; $i <= 3; $i++ ) {
431-
$http_response = wp_remote_get( home_url(), compact( 'sslverify', 'headers' ) );
431+
$http_response = wp_remote_get( home_url( '/' ), compact( 'sslverify', 'headers' ) );
432432
if ( is_wp_error( $http_response ) ) {
433433
return $http_response;
434434
}

tests/php/src/Admin/OnboardingWizardSubmenuPageTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use AmpProject\AmpWP\Infrastructure\Registerable;
1414
use AmpProject\AmpWP\Infrastructure\Service;
1515
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
16+
use AmpProject\AmpWP\Tests\Helpers\ThemesApiRequestMocking;
1617
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
1718
use AMP_Options_Manager;
1819

@@ -28,6 +29,7 @@
2829
class OnboardingWizardSubmenuPageTest extends DependencyInjectedTestCase {
2930

3031
use PrivateAccess;
32+
use ThemesApiRequestMocking;
3133

3234
/**
3335
* Test instance.
@@ -52,6 +54,8 @@ public function setUp() {
5254
$this->onboarding_wizard_submenu_page = $this->injector->make( OnboardingWizardSubmenuPage::class );
5355

5456
$this->options_menu = $this->injector->make( OptionsMenu::class );
57+
58+
$this->add_reader_themes_request_filter();
5559
}
5660

5761
/** @covers ::__construct() */

tests/php/src/Admin/OptionsMenuTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use AmpProject\AmpWP\Infrastructure\Service;
1919
use AmpProject\AmpWP\LoadingError;
2020
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
21+
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
2122
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
23+
use AmpProject\AmpWP\Tests\Helpers\ThemesApiRequestMocking;
2224
use AMP_Options_Manager;
2325

2426
/**
@@ -29,6 +31,8 @@
2931
*/
3032
class OptionsMenuTest extends DependencyInjectedTestCase {
3133

34+
use HomeUrlLoopbackRequestMocking;
35+
use ThemesApiRequestMocking;
3236
use PrivateAccess;
3337

3438
/**
@@ -49,6 +53,9 @@ public function setUp() {
4953
$site_health = $this->injector->make( SiteHealth::class );
5054

5155
$this->instance = new OptionsMenu( new GoogleFonts(), new ReaderThemes(), new RESTPreloader(), new DependencySupport(), new LoadingError(), $site_health );
56+
57+
$this->add_reader_themes_request_filter();
58+
$this->add_home_url_loopback_request_mocking();
5259
}
5360

5461
/**

tests/php/src/Admin/SiteHealthTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use AmpProject\AmpWP\AmpWpPluginFactory;
1414
use AmpProject\AmpWP\Option;
1515
use AmpProject\AmpWP\QueryVar;
16+
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
1617
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
1718
use AmpProject\AmpWP\Tests\TestCase;
1819
use WP_REST_Server;
@@ -25,6 +26,7 @@
2526
*/
2627
class SiteHealthTest extends TestCase {
2728

29+
use HomeUrlLoopbackRequestMocking;
2830
use PrivateAccess;
2931

3032
/**
@@ -68,6 +70,8 @@ public function setUp() {
6870

6971
$this->original_wp_rest_server = isset( $GLOBALS['wp_rest_server'] ) ? $GLOBALS['wp_rest_server'] : null;
7072
$GLOBALS['wp_rest_server'] = null;
73+
74+
$this->add_home_url_loopback_request_mocking();
7175
}
7276

7377
/**
@@ -816,7 +820,7 @@ function ( $r, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_
816820
],
817821
];
818822
},
819-
10,
823+
20,
820824
2
821825
);
822826

@@ -852,7 +856,7 @@ public function test_has_page_caching() {
852856
];
853857
};
854858

855-
add_filter( 'pre_http_request', $callback );
859+
add_filter( 'pre_http_request', $callback, 20 );
856860

857861
// Test 1: Assert for fresh result. (Even cached result is exist.)
858862
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'no', DAY_IN_SECONDS );
@@ -861,7 +865,7 @@ public function test_has_page_caching() {
861865

862866
$this->assertTrue( $this->instance->has_page_caching() );
863867

864-
remove_filter( 'pre_http_request', $callback );
868+
remove_filter( 'pre_http_request', $callback, 20 );
865869

866870
// Test 2: Test for cached result.
867871
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'yes', DAY_IN_SECONDS );
@@ -894,16 +898,16 @@ public function test_has_page_caching_with_error() {
894898
];
895899
};
896900

897-
add_filter( 'pre_http_request', $return_error );
901+
add_filter( 'pre_http_request', $return_error, 20 );
898902

899903
// Test 1: Assert for fresh result (which is then cached).
900904
$this->assertEquals(
901905
$error_object,
902906
$this->instance->has_page_caching()
903907
);
904908

905-
remove_filter( 'pre_http_request', $return_error );
906-
add_filter( 'pre_http_request', $return_cached_response );
909+
remove_filter( 'pre_http_request', $return_error, 20 );
910+
add_filter( 'pre_http_request', $return_cached_response, 20 );
907911

908912
// Test 2: Test for cached result.
909913
$this->assertEquals(
@@ -917,8 +921,8 @@ public function test_has_page_caching_with_error() {
917921
$this->instance->has_page_caching( false )
918922
);
919923

920-
remove_filter( 'pre_http_request', $return_cached_response );
921-
add_filter( 'pre_http_request', $return_error );
924+
remove_filter( 'pre_http_request', $return_cached_response, 20 );
925+
add_filter( 'pre_http_request', $return_error, 20 );
922926

923927
// Test 4: Test for non-cached result again now that no error is returned.
924928
$this->assertSame(

tests/php/src/Admin/SupportScreenTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use AmpProject\AmpWP\Admin\SupportScreen;
1111
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
12+
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
1213

1314
/**
1415
* Tests for SupportMenu.
@@ -18,6 +19,8 @@
1819
*/
1920
class SupportScreenTest extends DependencyInjectedTestCase {
2021

22+
use HomeUrlLoopbackRequestMocking;
23+
2124
/**
2225
* Instance of SupportMenu
2326
*
@@ -36,6 +39,7 @@ public function setUp() {
3639

3740
$this->instance = $this->injector->make( SupportScreen::class );
3841

42+
$this->add_home_url_loopback_request_mocking();
3943
}
4044

4145
/** @covers ::__construct() */
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Trait HomeUrlLoopbackRequestMocking.
4+
*
5+
* @package AmpProject\AmpWP
6+
*/
7+
8+
namespace AmpProject\AmpWP\Tests\Helpers;
9+
10+
/**
11+
* Helper trait for to mock HTTP requests to the WordPress.org themes API.
12+
*
13+
* @package AmpProject\AmpWP
14+
*/
15+
trait HomeUrlLoopbackRequestMocking {
16+
17+
/**
18+
* Add filter to mock that loopback requests succeed.
19+
*/
20+
public function add_home_url_loopback_request_mocking() {
21+
add_filter(
22+
'pre_http_request',
23+
static function ( $pre, $args, $url ) {
24+
if ( set_url_scheme( untrailingslashit( home_url() ), 'https' ) === set_url_scheme( untrailingslashit( $url ), 'https' ) ) {
25+
$pre = [
26+
'body' => '',
27+
'headers' => [],
28+
'response' => [
29+
'code' => 200,
30+
'status' => 'ok',
31+
],
32+
];
33+
}
34+
return $pre;
35+
},
36+
10,
37+
3
38+
);
39+
40+
}
41+
}

tests/php/src/Helpers/ThemesApiRequestMocking.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ trait ThemesApiRequestMocking {
1818
* Reader themes from wordpress.org.
1919
*/
2020
protected static $api_reader_themes = [
21+
[
22+
'name' => 'Twenty Twenty One',
23+
'slug' => 'twentytwentyone',
24+
'version' => '1.3',
25+
'preview_url' => 'https://wp-themes.com/twentytwenty',
26+
'author' =>
27+
[
28+
'user_nicename' => 'wordpressdotorg',
29+
'profile' => 'https://profiles.wordpress.org/wordpressdotorg',
30+
'avatar' => 'https://secure.gravatar.com/avatar/61ee2579b8905e62b4b4045bdc92c11a?s=96&d=monsterid&r=g',
31+
'display_name' => 'WordPress.org',
32+
],
33+
'screenshot_url' => '//ts.w.org/wp-content/themes/twentytwentyone/screenshot.png?ver=1.3',
34+
'rating' => 86,
35+
'num_ratings' => '37',
36+
'homepage' => 'https://wordpress.org/themes/twentytwentyone/',
37+
'description' => '...',
38+
'requires' => '4.7',
39+
'requires_php' => '5.2.4',
40+
],
2141
[
2242
'name' => 'Twenty Twenty',
2343
'slug' => 'twentytwenty',

tests/php/src/Helpers/ValidationRequestMocking.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,23 @@ static function( $post ) {
3434
);
3535
}
3636

37+
/**
38+
* Add filter to mock validate responses.
39+
*/
40+
public function add_validate_response_mocking_filter() {
41+
add_filter( 'pre_http_request', [ $this, 'get_validate_response' ], 10, 3 );
42+
}
43+
3744
/**
3845
* Construct a WP HTTP response for a validation request.
3946
*
4047
* @return array The response.
4148
*/
42-
public function get_validate_response() {
49+
public function get_validate_response( $r, /** @noinspection PhpUnusedParameterInspection */ $args, $url ) {
50+
if ( false === strpos( $url, 'amp_validate' ) ) {
51+
return $r;
52+
}
53+
4354
$mock_validation = [
4455
'results' => [
4556
[

tests/php/src/Helpers/WithBlockEditorSupport.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use AMP_Options_Manager;
1111
use AmpProject\AmpWP\Option;
12+
use WP_Error;
1213
use WP_User;
1314

1415
/**
@@ -25,7 +26,12 @@ trait WithBlockEditorSupport {
2526
*/
2627
public function setup_environment( $post_type_uses_block_editor, $post_type_supports_amp, $post_type = 'foo' ) {
2728
if ( $post_type_uses_block_editor ) {
29+
$block_http_request = static function () {
30+
return new WP_Error( 'request_blocked', 'Request blocked' );
31+
};
32+
add_filter( 'pre_http_request', $block_http_request );
2833
set_current_screen( 'post.php' );
34+
remove_filter( 'pre_http_request', $block_http_request );
2935
add_filter( 'replace_editor', '__return_false' );
3036
add_filter( 'use_block_editor_for_post', '__return_true' );
3137
}

tests/php/src/Support/SupportDataTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use AMP_Validated_URL_Post_Type;
1111
use AmpProject\AmpWP\Support\SupportData;
1212
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
13+
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
1314
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
1415
use WP_Error;
1516
use stdClass;
@@ -22,7 +23,7 @@
2223
*/
2324
class SupportDataTest extends DependencyInjectedTestCase {
2425

25-
use PrivateAccess;
26+
use PrivateAccess, HomeUrlLoopbackRequestMocking;
2627

2728
/**
2829
* Instance of OptionsMenu
@@ -51,6 +52,8 @@ public function setUp() {
5152
foreach ( array_keys( $this->previous_ini_config ) as $key ) {
5253
$this->previous_ini_config[ $key ] = ini_get( $key );
5354
}
55+
56+
$this->add_home_url_loopback_request_mocking();
5457
}
5558

5659
/**

tests/php/src/Validation/SavePostValidationEventTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class SavePostValidationEventTest extends TestCase {
4040
public function setUp() {
4141
$this->test_instance = new SavePostValidationEvent( new BackgroundTaskDeactivator(), new UserAccess(), new URLValidationProvider() );
4242
$this->dev_tools_user_access = new UserAccess();
43-
add_filter( 'pre_http_request', [ $this, 'get_validate_response' ] );
43+
$this->add_validate_response_mocking_filter();
4444
}
4545

4646
/** @covers ::__construct() */

tests/php/src/Validation/ScannableURLProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class ScannableURLProviderTest extends TestCase {
3232
public function setUp() {
3333
parent::setUp();
3434
$this->scannable_url_provider = new ScannableURLProvider( [], 20 );
35-
add_filter( 'pre_http_request', [ $this, 'get_validate_response' ] );
35+
$this->add_validate_response_mocking_filter();
3636
}
3737

3838
/** @covers ::__construct() */

tests/php/src/Validation/ScannableURLsRestControllerTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace AmpProject\AmpWP\Tests\Validation;
99

10+
use AMP_Options_Manager;
1011
use AMP_Validation_Manager;
1112
use AmpProject\AmpWP\Infrastructure\Delayed;
13+
use AmpProject\AmpWP\Option;
1214
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
1315
use AmpProject\AmpWP\Tests\Helpers\ValidationRequestMocking;
1416
use AmpProject\AmpWP\Validation\ScannableURLsRestController;
@@ -39,7 +41,7 @@ public function setUp() {
3941

4042
do_action( 'rest_api_init' );
4143
$this->controller = $this->injector->make( ScannableURLsRestController::class );
42-
add_filter( 'pre_http_request', [ $this, 'get_validate_response' ] );
44+
$this->add_validate_response_mocking_filter();
4345
}
4446

4547
/** @covers ::get_registration_action() */
@@ -144,6 +146,12 @@ public function test_get_items() {
144146
}
145147

146148
// Test `force_standard_mode` query parameter.
149+
AMP_Options_Manager::update_options(
150+
[
151+
Option::ALL_TEMPLATES_SUPPORTED => false,
152+
Option::SUPPORTED_TEMPLATES => [ 'is_singular' ],
153+
]
154+
);
147155
$request_with_forced_standard_mode = new WP_REST_Request( 'GET', '/amp/v1/scannable-urls' );
148156
$request_with_forced_standard_mode->set_param( ScannableURLsRestController::FORCE_STANDARD_MODE, 'true' );
149157
$response_with_forced_standard_mode = rest_get_server()->dispatch( $request_with_forced_standard_mode );

tests/php/src/Validation/URLValidationCronTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public function setUp() {
3838
$this->test_instance = $this->injector->make( URLValidationCron::class );
3939
add_filter(
4040
'pre_http_request',
41-
function () {
41+
function ( $pre ) {
4242
$this->request_count++;
43-
return $this->get_validate_response();
43+
return $pre;
4444
}
4545
);
46+
47+
$this->add_validate_response_mocking_filter();
4648
}
4749

4850
/**

0 commit comments

Comments
 (0)