Commit 4b32b77e8c1 for woocommerce
commit 4b32b77e8c15e15b37c71abf8455884c914c2e91
Author: Hannah Tinkler <hannah.tinkler@gmail.com>
Date: Thu Mar 26 17:15:13 2026 +0000
Replaces stubs with mocks. (#63875)
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/InternalNotificationDispatcherTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/InternalNotificationDispatcherTest.php
index 71d989a37e6..5dccfce2da7 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/InternalNotificationDispatcherTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/InternalNotificationDispatcherTest.php
@@ -5,9 +5,9 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\PushNotifications\Dispatchers;
use Automattic\WooCommerce\Internal\PushNotifications\Dispatchers\InternalNotificationDispatcher;
+use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewOrderNotification;
+use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewReviewNotification;
use Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubOrderNotification;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubReviewNotification;
use WC_Unit_Test_Case;
/**
@@ -83,7 +83,7 @@ class InternalNotificationDispatcherTest extends WC_Unit_Test_Case {
* @testdox Should fire a non-blocking POST to the send endpoint URL.
*/
public function test_dispatch_fires_non_blocking_post_to_send_endpoint(): void {
- $notifications = array( new StubOrderNotification( 1 ) );
+ $notifications = array( $this->create_order_mock( 1 ) );
$this->sut->dispatch( $notifications );
@@ -112,7 +112,7 @@ class InternalNotificationDispatcherTest extends WC_Unit_Test_Case {
* @testdox Should include a valid JWT with correct claims and body hash.
*/
public function test_dispatch_includes_valid_jwt_with_correct_claims(): void {
- $notifications = array( new StubOrderNotification( 1 ) );
+ $notifications = array( $this->create_order_mock( 1 ) );
$this->sut->dispatch( $notifications );
@@ -141,8 +141,8 @@ class InternalNotificationDispatcherTest extends WC_Unit_Test_Case {
*/
public function test_dispatch_body_contains_encoded_notifications(): void {
$notifications = array(
- new StubOrderNotification( 10 ),
- new StubReviewNotification( 20 ),
+ $this->create_order_mock( 10 ),
+ $this->create_review_mock( 20 ),
);
$this->sut->dispatch( $notifications );
@@ -165,4 +165,30 @@ class InternalNotificationDispatcherTest extends WC_Unit_Test_Case {
$this->assertNull( $this->captured_url, 'No HTTP request should be made for empty notifications' );
}
+
+ /**
+ * Creates a mock NewOrderNotification that avoids database calls.
+ *
+ * @param int $resource_id The resource ID.
+ * @return NewOrderNotification
+ */
+ private function create_order_mock( int $resource_id ): NewOrderNotification {
+ return $this->getMockBuilder( NewOrderNotification::class )
+ ->setConstructorArgs( array( $resource_id ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
+ }
+
+ /**
+ * Creates a mock NewReviewNotification that avoids database calls.
+ *
+ * @param int $resource_id The resource ID.
+ * @return NewReviewNotification
+ */
+ private function create_review_mock( int $resource_id ): NewReviewNotification {
+ return $this->getMockBuilder( NewReviewNotification::class )
+ ->setConstructorArgs( array( $resource_id ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Notifications/NotificationTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Notifications/NotificationTest.php
index d5601a83e97..7f416a7f171 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Notifications/NotificationTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Notifications/NotificationTest.php
@@ -7,8 +7,6 @@ namespace Automattic\WooCommerce\Tests\Internal\PushNotifications\Notifications;
use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewOrderNotification;
use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewReviewNotification;
use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubOrderNotification;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubReviewNotification;
use InvalidArgumentException;
use WC_Unit_Test_Case;
@@ -20,7 +18,10 @@ class NotificationTest extends WC_Unit_Test_Case {
* @testdox Should return an identifier combining blog ID, type, and resource ID.
*/
public function test_get_identifier(): void {
- $notification = new StubOrderNotification( 42 );
+ $notification = $this->getMockBuilder( NewOrderNotification::class )
+ ->setConstructorArgs( array( 42 ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
$this->assertSame( get_current_blog_id() . '_store_order_42', $notification->get_identifier() );
}
@@ -29,7 +30,10 @@ class NotificationTest extends WC_Unit_Test_Case {
* @testdox Should return notification data as an array.
*/
public function test_to_array(): void {
- $notification = new StubReviewNotification( 99 );
+ $notification = $this->getMockBuilder( NewReviewNotification::class )
+ ->setConstructorArgs( array( 99 ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
$result = $notification->to_array();
@@ -49,7 +53,7 @@ class NotificationTest extends WC_Unit_Test_Case {
public function test_throws_for_non_positive_resource_id( int $resource_id ): void {
$this->expectException( InvalidArgumentException::class );
- new StubOrderNotification( $resource_id );
+ new NewOrderNotification( $resource_id );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Services/PendingNotificationStoreTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Services/PendingNotificationStoreTest.php
index 9e3893071f5..a337009855d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Services/PendingNotificationStoreTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Services/PendingNotificationStoreTest.php
@@ -5,9 +5,9 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\PushNotifications\Services;
use Automattic\WooCommerce\Internal\PushNotifications\Dispatchers\InternalNotificationDispatcher;
+use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewOrderNotification;
+use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewReviewNotification;
use Automattic\WooCommerce\Internal\PushNotifications\Services\PendingNotificationStore;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubOrderNotification;
-use Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs\StubReviewNotification;
use WC_Unit_Test_Case;
/**
@@ -47,7 +47,7 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should add a notification to the store.
*/
public function test_add_stores_notification(): void {
- $this->store->add( new StubOrderNotification( 42 ) );
+ $this->store->add( $this->create_order_mock( 42 ) );
$this->assertSame( 1, $this->store->count() );
}
@@ -56,8 +56,8 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should deduplicate notifications with the same type and resource ID.
*/
public function test_add_deduplicates_same_type_and_resource(): void {
- $this->store->add( new StubOrderNotification( 42 ) );
- $this->store->add( new StubOrderNotification( 42 ) );
+ $this->store->add( $this->create_order_mock( 42 ) );
+ $this->store->add( $this->create_order_mock( 42 ) );
$this->assertSame( 1, $this->store->count() );
}
@@ -66,8 +66,8 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should store notifications with different types separately.
*/
public function test_add_allows_different_types_for_same_resource(): void {
- $this->store->add( new StubOrderNotification( 42 ) );
- $this->store->add( new StubReviewNotification( 42 ) );
+ $this->store->add( $this->create_order_mock( 42 ) );
+ $this->store->add( $this->create_review_mock( 42 ) );
$this->assertSame( 2, $this->store->count() );
}
@@ -76,8 +76,8 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should store notifications with different resource IDs separately.
*/
public function test_add_allows_same_type_for_different_resources(): void {
- $this->store->add( new StubOrderNotification( 42 ) );
- $this->store->add( new StubOrderNotification( 43 ) );
+ $this->store->add( $this->create_order_mock( 42 ) );
+ $this->store->add( $this->create_order_mock( 43 ) );
$this->assertSame( 2, $this->store->count() );
}
@@ -90,7 +90,7 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
$store = new PendingNotificationStore();
$store->init( $dispatcher );
- $store->add( new StubOrderNotification( 42 ) );
+ $store->add( $this->create_order_mock( 42 ) );
$this->assertSame( 0, $store->count() );
}
@@ -99,9 +99,9 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should register shutdown hook only once regardless of how many notifications are added.
*/
public function test_add_registers_shutdown_hook_once(): void {
- $this->store->add( new StubOrderNotification( 1 ) );
- $this->store->add( new StubOrderNotification( 2 ) );
- $this->store->add( new StubOrderNotification( 3 ) );
+ $this->store->add( $this->create_order_mock( 1 ) );
+ $this->store->add( $this->create_order_mock( 2 ) );
+ $this->store->add( $this->create_order_mock( 3 ) );
$hook_count = 0;
@@ -124,7 +124,7 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should clear pending notifications after dispatch.
*/
public function test_dispatch_all_clears_store(): void {
- $this->store->add( new StubOrderNotification( 1 ) );
+ $this->store->add( $this->create_order_mock( 1 ) );
$this->store->dispatch_all();
@@ -135,8 +135,8 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
* @testdox Should return all pending notifications via get_all.
*/
public function test_get_all_returns_pending_notifications(): void {
- $this->store->add( new StubOrderNotification( 1 ) );
- $this->store->add( new StubReviewNotification( 2 ) );
+ $this->store->add( $this->create_order_mock( 1 ) );
+ $this->store->add( $this->create_review_mock( 2 ) );
$all = $this->store->get_all();
@@ -144,4 +144,30 @@ class PendingNotificationStoreTest extends WC_Unit_Test_Case {
$this->assertSame( 1, $all[0]->get_resource_id() );
$this->assertSame( 2, $all[1]->get_resource_id() );
}
+
+ /**
+ * Creates a mock NewOrderNotification that avoids database calls.
+ *
+ * @param int $resource_id The resource ID.
+ * @return NewOrderNotification
+ */
+ private function create_order_mock( int $resource_id ): NewOrderNotification {
+ return $this->getMockBuilder( NewOrderNotification::class )
+ ->setConstructorArgs( array( $resource_id ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
+ }
+
+ /**
+ * Creates a mock NewReviewNotification that avoids database calls.
+ *
+ * @param int $resource_id The resource ID.
+ * @return NewReviewNotification
+ */
+ private function create_review_mock( int $resource_id ): NewReviewNotification {
+ return $this->getMockBuilder( NewReviewNotification::class )
+ ->setConstructorArgs( array( $resource_id ) )
+ ->onlyMethods( array( 'to_payload', 'has_meta', 'write_meta' ) )
+ ->getMock();
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubOrderNotification.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubOrderNotification.php
deleted file mode 100644
index 8f3a9a94b92..00000000000
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubOrderNotification.php
+++ /dev/null
@@ -1,40 +0,0 @@
-<?php
-
-declare( strict_types = 1 );
-
-namespace Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs;
-
-use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewOrderNotification;
-
-/**
- * Stub notification with type 'store_order' for testing.
- */
-class StubOrderNotification extends NewOrderNotification {
- /** @var array<string, bool> */
- private array $meta = array();
-
- /**
- * {@inheritDoc}
- */
- public function to_payload(): ?array {
- return array( 'test' => true );
- }
-
- /**
- * {@inheritDoc}
- *
- * @param string $key The meta key.
- */
- public function has_meta( string $key ): bool {
- return isset( $this->meta[ $key ] );
- }
-
- /**
- * {@inheritDoc}
- *
- * @param string $key The meta key.
- */
- public function write_meta( string $key ): void {
- $this->meta[ $key ] = true;
- }
-}
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubReviewNotification.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubReviewNotification.php
deleted file mode 100644
index 26c2460980d..00000000000
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Stubs/StubReviewNotification.php
+++ /dev/null
@@ -1,40 +0,0 @@
-<?php
-
-declare( strict_types = 1 );
-
-namespace Automattic\WooCommerce\Tests\Internal\PushNotifications\Stubs;
-
-use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewReviewNotification;
-
-/**
- * Stub notification with type 'store_review' for testing.
- */
-class StubReviewNotification extends NewReviewNotification {
- /** @var array<string, bool> */
- private array $meta = array();
-
- /**
- * {@inheritDoc}
- */
- public function to_payload(): ?array {
- return array( 'test' => true );
- }
-
- /**
- * {@inheritDoc}
- *
- * @param string $key The meta key.
- */
- public function has_meta( string $key ): bool {
- return isset( $this->meta[ $key ] );
- }
-
- /**
- * {@inheritDoc}
- *
- * @param string $key The meta key.
- */
- public function write_meta( string $key ): void {
- $this->meta[ $key ] = true;
- }
-}