Fix (and improve) failing unit test.

This commit is contained in:
Nestor Soriano 2021-10-22 12:24:02 +02:00
parent d98112e014
commit 2d71ec403c
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
1 changed files with 20 additions and 6 deletions

View File

@ -16,18 +16,26 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
/**
* @testdox If 'wc_get_price_excluding_tax' gets an order as argument, it passes the order customer to 'WC_Tax::get_rates'.
*
* @testWith [true, 1]
* [true, 0]
* [false, null]
* @testWith [true, 1, true]
* [true, 1, false]
* [true, 0, true]
* [true, 0, false]
* [false, null, true]
* [false, null, false]
*
* @param bool $pass_order Whether an order is passed to 'wc_get_price_excluding_tax' or not.
* @param int|null $customer_id Id of the customer associated to the order.
* @param bool $set_filter Whether the 'woocommerce_adjust_non_base_location_prices' filter should be set to return false.
*/
public function test_wc_get_price_excluding_tax_passes_order_customer_to_get_rates_if_order_is_available( $pass_order, $customer_id ) {
public function test_wc_get_price_excluding_tax_passes_order_customer_to_get_rates_if_order_is_available( $pass_order, $customer_id, $set_filter ) {
$customer_passed_to_get_rates = false;
$get_base_rates_invoked = false;
$customer_id_passed_to_wc_customer_constructor = false;
if ( $set_filter ) {
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
}
FunctionsMockerHack::add_function_mocks(
array(
'wc_prices_include_tax' => '__return_true',
@ -93,21 +101,27 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
wc_get_price_excluding_tax( $product, array( 'order' => $order ) );
$this->assertEquals( $order->get_customer_id(), $customer_id_passed_to_wc_customer_constructor );
if ( $customer_id ) {
if ( $customer_id && $set_filter ) {
$this->assertEquals( $order->get_customer_id(), $customer_id_passed_to_wc_customer_constructor );
$this->assertFalse( $get_base_rates_invoked );
$this->assertSame( $customer, $customer_passed_to_get_rates );
} else {
$this->assertFalse( $customer_id_passed_to_wc_customer_constructor );
$this->assertFalse( $customer_passed_to_get_rates );
$this->assertTrue( $get_base_rates_invoked );
}
} else {
wc_get_price_excluding_tax( $product );
$this->assertFalse( $customer_id_passed_to_wc_customer_constructor );
$this->assertFalse( $customer_passed_to_get_rates );
$this->assertTrue( $get_base_rates_invoked );
}
// phpcs:enable Squiz.Commenting
if ( $set_filter ) {
remove_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
}
}
}