Skip to content

Commit

Permalink
Speedup PHPUnit tests (#9018)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladolaru authored Jun 25, 2024
1 parent d28a69e commit 261a1ec
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog/update-speedup-phpunit-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: These changes only affect PHPUnit tests.


62 changes: 62 additions & 0 deletions tests/WCPAY_UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,68 @@
* Class WP_UnitTestCase
*/
class WCPAY_UnitTestCase extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

// Use a priority of 9 to ensure that these filters will allow tests that want to mock external requests
// to hook in with the regular 10 priority and do their thing.
// But by default we will intercept all external requests.
add_filter( 'pre_http_request', [ $this, 'filter_intercept_external_requests' ], 9, 3 );
add_filter( 'woocommerce_get_geolocation', [ $this, 'filter_mock_wc_geolocation' ], 9, 2 );
}

public function tear_down() {
remove_filter( 'pre_http_request', [ $this, 'filter_intercept_external_requests' ], 9, 3 );
remove_filter( 'woocommerce_get_geolocation', [ $this, 'filter_mock_wc_geolocation' ], 9, 2 );

parent::tear_down();
}

/**
* Intercept external requests and return a service unavailable response.
*
* This way we don't allow relying on external services for tests (fragile and slow tests) and force those tests
* that care about a response to mock the request response.
*
* @see WP_Http::request()
*
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*
* @return array
*/
public function filter_intercept_external_requests( $response, $parsed_args, $url ) {
// Return a service unavailable response.
return [
'body' => '',
'response' => [
'code' => WP_Http::SERVICE_UNAVAILABLE,
],
'headers' => [],
'cookies' => [],
'http_response' => null,
];
}

/**
* Intercept geolocation requests and return mock data.
*
* @param array $geolocation
* @param string $ip_address
*
* @return array
*/
public function filter_mock_wc_geolocation( $geolocation, $ip_address ) {
$ip_geolocation_test_data = json_decode( file_get_contents( __DIR__ . '/unit/test-data/ip-geolocation.json' ), true );

if ( ! empty( $ip_geolocation_test_data[ $ip_address ] ) ) {
$geolocation = array_merge( $geolocation, $ip_geolocation_test_data[ $ip_address ] );
}

return $geolocation;
}

protected function is_wpcom() {
return defined( 'IS_WPCOM' ) && IS_WPCOM;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test-data/ip-geolocation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"206.71.50.230": {
"country": "US"
},
"187.34.8.193": {
"country": "BR"
},
"127.0.0.1": {
"country": "US"
}
}
12 changes: 7 additions & 5 deletions tests/unit/woopay/test-class-woopay-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function tear_down() {
/**
* Data provider for test_should_enable_woopay.
*
* @return boolean
* @return array
*/
public function should_enable_woopay_data_provider() {
return [
Expand Down Expand Up @@ -61,9 +61,11 @@ public function test_should_enable_woopay( $woopay_eligible, $gateway_woopay_ena
}

/**
* Data provider for test_should_enable_woopay.
* Data provider for test_is_country_available.
*
* @see test-data/ip_geolocation.json
*
* @return boolean
* @return array
*/
public function is_country_available_data_provider() {
return [
Expand All @@ -84,15 +86,15 @@ public function test_is_country_available( $ip_address, $expected ) {
WC_Payments::mode()->live();

$woopay_utilities = new WooPay_Utilities();
$actual = $woopay_utilities->is_country_available( $this->gateway_mock );
$actual = $woopay_utilities->is_country_available();
$this->assertSame( $expected, $actual );
}

public function test_is_country_available_in_test_mode_return_true() {
WC_Payments::mode()->test();

$woopay_utilities = new WooPay_Utilities();
$actual = $woopay_utilities->is_country_available( $this->gateway_mock );
$actual = $woopay_utilities->is_country_available();
$this->assertSame( true, $actual );
}

Expand Down

0 comments on commit 261a1ec

Please sign in to comment.