Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 14ac863

Browse files
Add protection around mock queries in unit tests. (#3602)
* Add protection around mock queries in unit tests. This adds additional checks before mocking wc_get_orders query results to prevent overriding refunds query. This is needed with WC core 4.9 since it does an addtional wc_get_orders query to preload and hydrate cache for refunds in order to improve performance. * update jetpack autoloader to 2.7.1 Co-authored-by: Seghir Nadir <[email protected]>
1 parent 370659f commit 14ac863

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tests/php/Domain/Services/DeleteDraftOrders.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ public function test_delete_expired_draft_orders() {
123123
}
124124

125125
public function test_greater_than_batch_results_error() {
126-
$sample_results = function() {
127-
return array_fill( 0, 21, ( new WC_Order ) );
128-
126+
$sample_results = function( $results, $args ) {
127+
if ( isset( $args[ 'status' ] ) && DraftOrders::DB_STATUS === $args[ 'status' ] ) {
128+
return array_fill( 0, 21, ( new WC_Order ) );
129+
}
130+
return $results;
129131
};
130132
$this->mock_results_for_wc_query($sample_results);
131133
$this->draft_orders_instance->delete_expired_draft_orders();
@@ -134,8 +136,11 @@ public function test_greater_than_batch_results_error() {
134136
}
135137

136138
public function test_order_not_instance_of_wc_order_error() {
137-
$sample_results = function() {
138-
return [ 10 ];
139+
$sample_results = function( $results, $args ) {
140+
if ( isset( $args[ 'status' ] ) && DraftOrders::DB_STATUS === $args[ 'status' ] ) {
141+
return [ 10 ];
142+
}
143+
return $results;
139144
};
140145
$this->mock_results_for_wc_query( $sample_results );
141146
$this->draft_orders_instance->delete_expired_draft_orders();
@@ -144,10 +149,13 @@ public function test_order_not_instance_of_wc_order_error() {
144149
}
145150

146151
public function test_order_incorrect_status_error() {
147-
$sample_results = function() {
148-
$test_order = new WC_Order();
149-
$test_order->set_status( 'on-hold' );
150-
return [ $test_order ];
152+
$sample_results = function( $results, $args ) {
153+
if ( isset( $args[ 'status' ] ) && DraftOrders::DB_STATUS === $args[ 'status' ] ) {
154+
$test_order = new WC_Order();
155+
$test_order->set_status( 'on-hold' );
156+
return [ $test_order ];
157+
}
158+
return $results;
151159
};
152160
$this->mock_results_for_wc_query( $sample_results );
153161
$this->draft_orders_instance->delete_expired_draft_orders();
@@ -182,10 +190,11 @@ public function test_order_status_verification() {
182190
}
183191

184192
private function mock_results_for_wc_query( $mock_callback ) {
185-
add_filter( 'woocommerce_order_query', $mock_callback );
193+
add_filter( 'woocommerce_order_query', $mock_callback, 10, 2 );
186194
}
187195

188196
private function unset_mock_results_for_wc_query( $mock_callback ) {
189-
remove_filter( 'woocommerce_order_query', $mock_callback );
197+
$removed = remove_filter( 'woocommerce_order_query', $mock_callback );
198+
$this->assertTrue( $removed );
190199
}
191200
}

0 commit comments

Comments
 (0)