Fix the usage of FakeQueue in DataRegeneratorTest.

Also update the doc comment of FakeQueue itself on how to use it.
This commit is contained in:
Nestor Soriano 2021-05-05 11:11:36 +02:00
parent c4e7074c70
commit f53f959d03
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
4 changed files with 28 additions and 30 deletions

View File

@ -48,14 +48,6 @@ class WC_Queue {
return self::$instance; 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 * Get class to instantiate
* *

View File

@ -197,7 +197,8 @@ CREATE TABLE ' . $this->lookup_table_name . '(
* Enqueue one regeneration step in action scheduler. * Enqueue one regeneration step in action scheduler.
*/ */
private function enqueue_regeneration_step_run() { 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, WC()->call_function( 'time' ) + 1,
'woocommerce_run_product_attribute_lookup_update_callback', 'woocommerce_run_product_attribute_lookup_update_callback',
array(), array(),

View File

@ -11,11 +11,18 @@ namespace Automattic\WooCommerce\Testing\Tools;
* Fake scheduled actions queue for unit tests, it just records all the method calls * Fake scheduled actions queue for unit tests, it just records all the method calls
* in a publicly accessible $methods_called property. * 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 { class FakeQueue implements \WC_Queue_Interface {

View File

@ -33,11 +33,9 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case {
private $lookup_table_name; private $lookup_table_name;
/** /**
* Runs before all the tests in the class. * @var FakeQueue
*/ */
public static function setUpBeforeClass() { private $queue;
\WC_Queue::reset_instance();
}
/** /**
* Runs before each test. * Runs before each test.
@ -45,14 +43,9 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case {
public function setUp() { public function setUp() {
global $wpdb; global $wpdb;
$this->lookup_table_name = $wpdb->prefix . 'wc_product_attributes_lookup'; parent::setUp();
add_filter( $this->lookup_table_name = $wpdb->prefix . 'wc_product_attributes_lookup';
'woocommerce_queue_class',
function() {
return FakeQueue::class;
}
);
// phpcs:disable Squiz.Commenting // phpcs:disable Squiz.Commenting
$this->lookup_data_store = new class() extends LookupDataStore { $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 ); $container->replace( LookupDataStore::class, $this->lookup_data_store );
$this->sut = $container->get( DataRegenerator::class ); $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', 'hook' => 'woocommerce_run_product_attribute_lookup_update_callback',
'group' => 'woocommerce-db-updates', '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 ) ); $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_product_id_to_process' ) );
$this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) ); $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) );
$this->assertEquals( 'no', get_option( 'woocommerce_attribute_lookup__enabled' ) ); $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(); $this->sut->initiate_regeneration();
WC()->queue()->methods_called = array(); $this->queue->methods_called = array();
update_option( 'woocommerce_attribute_lookup__last_products_page_processed', 7 ); 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', 'hook' => 'woocommerce_run_product_attribute_lookup_update_callback',
'group' => 'woocommerce-db-updates', '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 ) ); $this->assertEquals( sort( $expected_enqueued ), sort( $actual_enqueued ) );
} }
@ -231,7 +229,7 @@ class DataRegeneratorTest extends \WC_Unit_Test_Case {
); );
$this->sut->initiate_regeneration(); $this->sut->initiate_regeneration();
WC()->queue()->methods_called = array(); $this->queue->methods_called = array();
do_action( 'woocommerce_run_product_attribute_lookup_update_callback' ); 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_product_id_to_process' ) );
$this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) ); $this->assertFalse( get_option( 'woocommerce_attribute_lookup__last_products_page_processed' ) );
$this->assertEquals( 'no', get_option( 'woocommerce_attribute_lookup__enabled' ) ); $this->assertEquals( 'no', get_option( 'woocommerce_attribute_lookup__enabled' ) );
$this->assertEmpty( WC()->queue()->methods_called ); $this->assertEmpty( $this->queue->methods_called );
} }
} }