Commit ff94a38c5ad for woocommerce
commit ff94a38c5ada706881bddbd8b439902e7f069081
Author: Christopher Hertel <mail@christopher-hertel.de>
Date: Mon Nov 16 22:01:09 2026 +0100
Speed up PHPUnit by reusing REST servers per class and releasing finished tests (#70949)
* Reuse one REST server per test class in WC_REST_Unit_Test_Case
* Release finished test instances' properties after each test
* Add changelog entry for PHPUnit speed-ups
* Match one-line property docblock style in image upload cleanup test
diff --git a/plugins/woocommerce/changelog/php74-ci-timeout-phpunit-memory-rest b/plugins/woocommerce/changelog/php74-ci-timeout-phpunit-memory-rest
new file mode 100644
index 00000000000..cd8ab1459a2
--- /dev/null
+++ b/plugins/woocommerce/changelog/php74-ci-timeout-phpunit-memory-rest
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Speed up PHPUnit by reusing REST servers per class and releasing finished tests; tests only.
diff --git a/plugins/woocommerce/phpunit.xml b/plugins/woocommerce/phpunit.xml
index 63b7e71210c..8df1fd5ce7d 100644
--- a/plugins/woocommerce/phpunit.xml
+++ b/plugins/woocommerce/phpunit.xml
@@ -37,6 +37,7 @@
</listener>
<listener class="WC_External_HTTP_Guard_Listener" file="./tests/legacy/framework/class-wc-external-http-guard-listener.php" />
<listener class="WC_Early_Exit_Guard_Listener" file="./tests/legacy/framework/class-wc-early-exit-guard-listener.php" />
+ <listener class="WC_Test_Instance_Cleanup_Listener" file="./tests/legacy/framework/class-wc-test-instance-cleanup-listener.php" />
</listeners>
<coverage includeUncoveredFiles="true">
<include>
diff --git a/plugins/woocommerce/tests/legacy/framework/class-wc-rest-unit-test-case.php b/plugins/woocommerce/tests/legacy/framework/class-wc-rest-unit-test-case.php
index b4beb0f8eff..e7812195eb7 100644
--- a/plugins/woocommerce/tests/legacy/framework/class-wc-rest-unit-test-case.php
+++ b/plugins/woocommerce/tests/legacy/framework/class-wc-rest-unit-test-case.php
@@ -178,14 +178,33 @@ class WC_REST_Unit_Test_Case extends WC_Unit_Test_Case {
*/
protected $server;
+ /**
+ * Servers keyed by test class, so routes are registered once per class instead of once per test.
+ *
+ * @var array<string, WC_Lazy_REST_Server>
+ */
+ private static $shared_servers = array();
+
+ /**
+ * Set to false when tests register their own controllers, add filters read at route registration, or need route args built from their own data.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = true;
+
/**
* Setup our test server.
*/
public function setUp(): void {
parent::setUp();
global $wp_rest_server;
- $wp_rest_server = new WC_Lazy_REST_Server();
- $this->server = $wp_rest_server;
+ if ( ! static::$reuse_rest_server ) {
+ $wp_rest_server = new WC_Lazy_REST_Server();
+ } else {
+ self::$shared_servers[ static::class ] = self::$shared_servers[ static::class ] ?? new WC_Lazy_REST_Server();
+ $wp_rest_server = self::$shared_servers[ static::class ];
+ }
+ $this->server = $wp_rest_server;
$this->initialize_rest_api_defaults();
// Reset payment gateways.
@@ -218,6 +237,14 @@ class WC_REST_Unit_Test_Case extends WC_Unit_Test_Case {
$wp_rest_server = null;
}
+ /**
+ * Drop the class's shared server.
+ */
+ public static function tearDownAfterClass(): void {
+ unset( self::$shared_servers[ static::class ] );
+ parent::tearDownAfterClass();
+ }
+
/**
* Perform a REST request.
*
diff --git a/plugins/woocommerce/tests/legacy/framework/class-wc-test-instance-cleanup-listener.php b/plugins/woocommerce/tests/legacy/framework/class-wc-test-instance-cleanup-listener.php
new file mode 100644
index 00000000000..01f8b754f47
--- /dev/null
+++ b/plugins/woocommerce/tests/legacy/framework/class-wc-test-instance-cleanup-listener.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Releases what a finished test left on its own properties.
+ *
+ * @package WooCommerce\Tests
+ */
+
+declare( strict_types = 1 );
+
+use PHPUnit\Framework\Test;
+use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\TestListener;
+use PHPUnit\Framework\TestListenerDefaultImplementation;
+
+/**
+ * PHPUnit keeps every test instance until the run ends, so fixtures, mocks and services stored on
+ * test properties stay reachable and grow the heap the garbage collector scans. Unsets the
+ * properties declared by the test's own classes after each test; PHPUnit and WordPress base class
+ * state is left alone.
+ */
+final class WC_Test_Instance_Cleanup_Listener implements TestListener {
+
+ use TestListenerDefaultImplementation;
+
+ /**
+ * Classes where the walk up the hierarchy stops; their properties and their ancestors' are kept.
+ */
+ private const BOUNDARY_CLASSES = array( TestCase::class, 'WP_UnitTestCase_Base' );
+
+ /**
+ * Releasable property names grouped by declaring class, keyed by test class.
+ *
+ * @var array<class-string, array<class-string, string[]>>
+ */
+ private $properties_by_class = array();
+
+ /**
+ * Unset the finished test's properties.
+ *
+ * @param Test $test The test that just finished.
+ * @param float $time Unused.
+ */
+ public function endTest( Test $test, float $time ): void {
+ if ( ! $test instanceof TestCase ) {
+ return;
+ }
+
+ foreach ( $this->get_releasable_properties( $test ) as $declaring_class => $names ) {
+ Closure::bind(
+ function () use ( $names ) {
+ foreach ( $names as $name ) {
+ unset( $this->$name );
+ }
+ },
+ $test,
+ $declaring_class
+ )();
+ }
+ }
+
+ /**
+ * Non-static, non-readonly properties declared below the boundary classes; readonly ones cannot be unset.
+ *
+ * @param TestCase $test The finished test.
+ * @return array<class-string, string[]>
+ */
+ private function get_releasable_properties( TestCase $test ): array {
+ $test_class = get_class( $test );
+ if ( isset( $this->properties_by_class[ $test_class ] ) ) {
+ return $this->properties_by_class[ $test_class ];
+ }
+
+ $properties = array();
+ $class = new ReflectionClass( $test );
+ while ( $class && ! in_array( $class->getName(), self::BOUNDARY_CLASSES, true ) ) {
+ foreach ( $class->getProperties() as $property ) {
+ if ( $property->isStatic() || $property->getDeclaringClass()->getName() !== $class->getName() ) {
+ continue;
+ }
+ if ( method_exists( $property, 'isReadOnly' ) && $property->isReadOnly() ) {
+ continue;
+ }
+ $properties[ $class->getName() ][] = $property->getName();
+ }
+ $class = $class->getParentClass();
+ }
+
+ $this->properties_by_class[ $test_class ] = $properties;
+
+ return $properties;
+ }
+}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/settings.php b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/settings.php
index bae3c8ca8be..d4ec377cbdb 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/settings.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/settings.php
@@ -15,6 +15,13 @@ use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
class Settings_V2 extends WC_REST_Unit_Test_Case {
use ArraySubsetAsserts;
+ /**
+ * Each test registers the settings filters that route registration reads.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Setup our test server, endpoints, and user info.
*/
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/settings.php b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/settings.php
index 3a41b13e3b3..ee180673db0 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/settings.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/settings.php
@@ -15,6 +15,13 @@ use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
class Settings extends WC_REST_Unit_Test_Case {
use ArraySubsetAsserts;
+ /**
+ * Each test registers the settings filters that route registration reads.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Setup our test server, endpoints, and user info.
*/
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-performance-indicators.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-performance-indicators.php
index 9934bd87450..f00fb83201c 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-performance-indicators.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-performance-indicators.php
@@ -12,6 +12,13 @@ use Automattic\WooCommerce\Enums\OrderStatus;
*/
class WC_Admin_Tests_API_Reports_Performance_Indicators extends WC_REST_Unit_Test_Case {
+ /**
+ * Each test mocks Jetpack modules before the reports routes are registered.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Endpoints.
*
diff --git a/plugins/woocommerce/tests/php/framework/class-wc-lazy-rest-server-test.php b/plugins/woocommerce/tests/php/framework/class-wc-lazy-rest-server-test.php
index ac50dab6bc2..80642fd598d 100644
--- a/plugins/woocommerce/tests/php/framework/class-wc-lazy-rest-server-test.php
+++ b/plugins/woocommerce/tests/php/framework/class-wc-lazy-rest-server-test.php
@@ -12,6 +12,13 @@ declare( strict_types = 1 );
*/
class WC_Lazy_REST_Server_Test extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests exercise route initialization on a pristine server.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* @testdox get_routes() returns the complete route table after a scoped namespace dispatch.
*/
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
index d6064cba5a7..69c12b65494 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
@@ -6,6 +6,13 @@
*/
class WC_REST_Taxes_Controller_Tests extends WC_REST_Unit_Test_Case {
+ /**
+ * Route args list the tax classes that exist at registration, and tests create new ones.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Runs before any test.
*/
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OfflinePaymentMethods/class-wc-rest-offline-payment-methods-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OfflinePaymentMethods/class-wc-rest-offline-payment-methods-v4-controller-tests.php
index 43c5105dfc4..a21dbca6230 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OfflinePaymentMethods/class-wc-rest-offline-payment-methods-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OfflinePaymentMethods/class-wc-rest-offline-payment-methods-v4-controller-tests.php
@@ -16,6 +16,13 @@ use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders;
*/
class WC_REST_Offline_Payment_Methods_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Test endpoint.
*
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/ShippingZoneMethod/class-wc-rest-shipping-zone-method-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/ShippingZoneMethod/class-wc-rest-shipping-zone-method-v4-controller-tests.php
index 17912657578..765fa948cca 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/ShippingZoneMethod/class-wc-rest-shipping-zone-method-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/ShippingZoneMethod/class-wc-rest-shipping-zone-method-v4-controller-tests.php
@@ -19,6 +19,13 @@ use WP_REST_Request;
*/
class WC_REST_Shipping_Zone_Method_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* @var Controller
*/
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-image-upload-cleanup-test.php b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-image-upload-cleanup-test.php
index 33f9eee224a..03d6af9d4a2 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-image-upload-cleanup-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-image-upload-cleanup-test.php
@@ -7,6 +7,10 @@ declare( strict_types = 1 );
* @package WooCommerce\Tests\RestApi
*/
class WC_REST_Image_Upload_Cleanup_Test extends WC_REST_Unit_Test_Case {
+
+ /** @var bool Tests register their own controller's routes, which a reused server would keep serving from the first test. */
+ protected static $reuse_rest_server = false;
+
/** @var array Downloaded files, including files with no attachment. */
private $uploads = array();
diff --git a/plugins/woocommerce/tests/php/src/Admin/API/ActivityPanelCountsTest.php b/plugins/woocommerce/tests/php/src/Admin/API/ActivityPanelCountsTest.php
index 5660968faa5..101456c7d8d 100644
--- a/plugins/woocommerce/tests/php/src/Admin/API/ActivityPanelCountsTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/API/ActivityPanelCountsTest.php
@@ -25,6 +25,13 @@ use WP_REST_Server;
*/
class ActivityPanelCountsTest extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests re-register routes after changing the options they read.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Endpoint.
*
diff --git a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
index 2cc8d59f8ee..63986e770c0 100644
--- a/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/Features/Fulfillments/OrderFulfillmentsRestControllerTest.php
@@ -19,6 +19,14 @@ use WP_REST_Request;
* @package Automattic\WooCommerce\Tests\Internal\Orders
*/
class OrderFulfillmentsRestControllerTest extends WC_REST_Unit_Test_Case {
+
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* @var OrderFulfillmentsRestController
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewRestControllerTest.php
index dbeb573643c..40bb6c33ffc 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/EmailPreview/EmailPreviewRestControllerTest.php
@@ -15,6 +15,14 @@ use WP_REST_Request;
* @class PaymentsRestController
*/
class EmailPreviewRestControllerTest extends WC_REST_Unit_Test_Case {
+
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Endpoint.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessRestControllerTest.php
index a82cc1867a6..7a5c0e3cf1a 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessRestControllerTest.php
@@ -14,6 +14,13 @@ use WP_REST_Request;
*/
class ReadinessRestControllerTest extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests fire rest_api_init with the feature both on and off.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Enable the feature after parent::setUp(), so the filter stays out of the hook
* snapshot WordPress takes in the first test. Routes register on first dispatch.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Orders/OrderStatusRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Orders/OrderStatusRestControllerTest.php
index 595c6d46704..b90669bb63b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Orders/OrderStatusRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Orders/OrderStatusRestControllerTest.php
@@ -13,6 +13,14 @@ use WP_REST_Request;
* @package Automattic\WooCommerce\Tests\Internal\Orders
*/
class OrderStatusRestControllerTest extends WC_REST_Unit_Test_Case {
+
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* @var OrderStatusRestController
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
index 49b47bda2fc..1accc9ecc2f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Fulfillments/ProvidersTest.php
@@ -15,6 +15,13 @@ use WP_REST_Request;
*/
class ProvidersTest extends WC_REST_Unit_Test_Case {
+ /**
+ * Tests register their own controller's routes, which a reused server would keep serving from the first test.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Controller instance
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/OfflinePaymentMethods/OfflinePaymentMethodsControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/OfflinePaymentMethods/OfflinePaymentMethodsControllerTest.php
index c7496782a20..693f8afd5e2 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/OfflinePaymentMethods/OfflinePaymentMethodsControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/OfflinePaymentMethods/OfflinePaymentMethodsControllerTest.php
@@ -16,6 +16,14 @@ use WP_REST_Request;
* @class OfflinePaymentMethodsControllerTest
*/
class OfflinePaymentMethodsControllerTest extends WC_REST_Unit_Test_Case {
+
+ /**
+ * Each test registers a controller wired to its own payments service mock.
+ *
+ * @var bool
+ */
+ protected static $reuse_rest_server = false;
+
/**
* Endpoint.
*