diff --git a/includes/queue/class-wc-queue.php b/includes/queue/class-wc-queue.php index 62e8974c3d4..80dd4b67a95 100644 --- a/includes/queue/class-wc-queue.php +++ b/includes/queue/class-wc-queue.php @@ -48,14 +48,6 @@ class WC_Queue { return self::$instance; } - /** - * Reset the singleton instance so that the next time instance() is invoked - * the woocommerce_queue_class hook will be fired again. - */ - final public static function reset_instance() { - self::$instance = null; - } - /** * Get class to instantiate * diff --git a/src/Internal/ProductAttributesLookup/DataRegenerator.php b/src/Internal/ProductAttributesLookup/DataRegenerator.php index e1026671836..ebc1758a0ff 100644 --- a/src/Internal/ProductAttributesLookup/DataRegenerator.php +++ b/src/Internal/ProductAttributesLookup/DataRegenerator.php @@ -197,7 +197,8 @@ CREATE TABLE ' . $this->lookup_table_name . '( * Enqueue one regeneration step in action scheduler. */ private function enqueue_regeneration_step_run() { - WC()->queue()->schedule_single( + $queue = WC()->get_instance_of( \WC_Queue::class ); + $queue->schedule_single( WC()->call_function( 'time' ) + 1, 'woocommerce_run_product_attribute_lookup_update_callback', array(), diff --git a/tests/Tools/FakeQueue.php b/tests/Tools/FakeQueue.php index cfb8b23607c..a8dd9151b55 100644 --- a/tests/Tools/FakeQueue.php +++ b/tests/Tools/FakeQueue.php @@ -11,11 +11,18 @@ namespace Automattic\WooCommerce\Testing\Tools; * Fake scheduled actions queue for unit tests, it just records all the method calls * in a publicly accessible $methods_called property. * - * To use, add this to the setUp method of the unit tests class: + * To use: * - * add_filter( 'woocommerce_queue_class', function() { return FakeQueue::class; } ); + * 1. The production class must get an instance of the queue in this way: * - * then WC->queue() will return an instance of this class. + * WC()->get_instance_of(\WC_Queue::class) + * + * 2. Add the following in the setUp() method of the unit tests class: + * + * $this->register_legacy_proxy_class_mocks([\WC_Queue::class => new FakeQueue()]); + * + * 3. Get the instance of the fake queue with $this->get_legacy_instance_of(\WC_Queue::class) + * and check its methods_called field as appropriate. */ class FakeQueue implements \WC_Queue_Interface { diff --git a/tests/php/src/Internal/ProductAttributesLookup/DataRegeneratorTest.php b/tests/php/src/Internal/ProductAttributesLookup/DataRegeneratorTest.php index 84ccb5d162b..bde4569878b 100644 --- a/tests/php/src/Internal/ProductAttributesLookup/DataRegeneratorTest.php +++ b/tests/php/src/Internal/ProductAttributesLookup/DataRegeneratorTest.php @@ -33,11 +33,9 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { private $lookup_table_name; /** - * Runs before all the tests in the class. + * @var FakeQueue */ - public static function setUpBeforeClass() { - \WC_Queue::reset_instance(); - } + private $queue; /** * Runs before each test. @@ -45,14 +43,9 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { public function setUp() { global $wpdb; - $this->lookup_table_name = $wpdb->prefix . 'wc_product_attributes_lookup'; + parent::setUp(); - add_filter( - 'woocommerce_queue_class', - function() { - return FakeQueue::class; - } - ); + $this->lookup_table_name = $wpdb->prefix . 'wc_product_attributes_lookup'; // phpcs:disable Squiz.Commenting $this->lookup_data_store = new class() extends LookupDataStore { @@ -72,7 +65,12 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { $container->replace( LookupDataStore::class, $this->lookup_data_store ); $this->sut = $container->get( DataRegenerator::class ); - WC()->queue()->methods_called = array(); + $this->register_legacy_proxy_class_mocks( + array( + \WC_Queue::class => new FakeQueue(), + ) + ); + $this->queue = $this->get_legacy_instance_of( \WC_Queue::class ); } /** @@ -131,7 +129,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { 'hook' => 'woocommerce_run_product_attribute_lookup_update_callback', 'group' => 'woocommerce-db-updates', ); - $actual_enqueued = current( WC()->queue()->methods_called ); + $actual_enqueued = current( $this->queue->methods_called ); $this->assertEquals( sort( $expected_enqueued ), sort( $actual_enqueued ) ); } @@ -158,7 +156,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_product_id_to_process' ) ); $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) ); $this->assertEquals( 'no', get_option( 'woocommerce_attribute_lookup__enabled' ) ); - $this->assertEmpty( WC()->queue()->methods_called ); + $this->assertEmpty( $this->queue->methods_called ); } /** @@ -184,7 +182,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { ); $this->sut->initiate_regeneration(); - WC()->queue()->methods_called = array(); + $this->queue->methods_called = array(); update_option( 'woocommerce_attribute_lookup__last_products_page_processed', 7 ); @@ -201,7 +199,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { 'hook' => 'woocommerce_run_product_attribute_lookup_update_callback', 'group' => 'woocommerce-db-updates', ); - $actual_enqueued = current( WC()->queue()->methods_called ); + $actual_enqueued = current( $this->queue->methods_called ); $this->assertEquals( sort( $expected_enqueued ), sort( $actual_enqueued ) ); } @@ -231,7 +229,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { ); $this->sut->initiate_regeneration(); - WC()->queue()->methods_called = array(); + $this->queue->methods_called = array(); do_action( 'woocommerce_run_product_attribute_lookup_update_callback' ); @@ -239,6 +237,6 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case { $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_product_id_to_process' ) ); $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) ); $this->assertEquals( 'no', get_option( 'woocommerce_attribute_lookup__enabled' ) ); - $this->assertEmpty( WC()->queue()->methods_called ); + $this->assertEmpty( $this->queue->methods_called ); } }