Refactoring and fixing bugs in WC_Customer Unit Tests
This commit is contained in:
parent
0213fcce85
commit
bc9821253f
|
@ -8,26 +8,65 @@
|
||||||
class WC_Helper_Customer {
|
class WC_Helper_Customer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the the current customer's billing details from the session
|
* Create a mock customer for testing purposes.
|
||||||
|
*
|
||||||
|
* @return WC_Customer
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static function create_mock_customer() {
|
||||||
|
|
||||||
|
$customer_data = array(
|
||||||
|
'country' => 'US',
|
||||||
|
'state' => 'PA',
|
||||||
|
'postcode' => '19123',
|
||||||
|
'city' => 'Philadelphia',
|
||||||
|
'address' => '123 South Street',
|
||||||
|
'address_2' => 'Apt 1',
|
||||||
|
'shipping_country' => 'US',
|
||||||
|
'shipping_state' => 'PA',
|
||||||
|
'shipping_postcode' => '19123',
|
||||||
|
'shipping_city' => 'Philadelphia',
|
||||||
|
'shipping_address' => '123 South Street',
|
||||||
|
'shipping_address_2' => 'Apt 1',
|
||||||
|
'is_vat_exempt' => false,
|
||||||
|
'calculated_shipping' => false
|
||||||
|
);
|
||||||
|
|
||||||
|
WC_Helper_Customer::set_customer_details( $customer_data );
|
||||||
|
|
||||||
|
return new WC_Customer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the expected output for the mock customer's shipping destination.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static function get_expected_customer_location() {
|
||||||
|
return array( "US", "PA", "19123", "Philadelphia" );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the expected output for the store's base location settings.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static function get_expected_store_location() {
|
||||||
|
return array( "GB", "", "", "" );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the customer's shipping and billing info from the session.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static function get_customer_details() {
|
public static function get_customer_details() {
|
||||||
WC()->session->get( 'customer' );
|
return WC()->session->get( 'customer' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the store's default shipping method.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static function get_default_shipping_method() {
|
|
||||||
get_option( 'woocommerce_default_shipping_method' );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the user's chosen shipping method.
|
* Get the user's chosen shipping method.
|
||||||
*
|
*
|
||||||
|
@ -49,7 +88,7 @@ class WC_Helper_Customer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the the current customer's billing details in the session
|
* Set the the current customer's billing details in the session.
|
||||||
*
|
*
|
||||||
* @param string $default_shipping_method Shipping Method slug
|
* @param string $default_shipping_method Shipping Method slug
|
||||||
*/
|
*/
|
||||||
|
@ -58,16 +97,6 @@ class WC_Helper_Customer {
|
||||||
WC()->session->set( 'customer', $customer_details );
|
WC()->session->set( 'customer', $customer_details );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the store's default shipping method.
|
|
||||||
*
|
|
||||||
* @param string $default_shipping_method Shipping Method slug
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static function set_default_shipping_method( $default_shipping_method ) {
|
|
||||||
update_option( 'woocommerce_default_shipping_method', $default_shipping_method );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the user's chosen shipping method.
|
* Set the user's chosen shipping method.
|
||||||
*
|
*
|
||||||
|
|
|
@ -6,166 +6,95 @@ class WC_Tests_Customer extends WC_Unit_Test_Case {
|
||||||
*/
|
*/
|
||||||
public function test_get_taxable_address() {
|
public function test_get_taxable_address() {
|
||||||
|
|
||||||
$customer_data = array(
|
$base_store_address = WC_Helper_Customer::get_expected_store_location();
|
||||||
'country' => 'US',
|
$customer_address = WC_Helper_Customer::get_expected_customer_location();
|
||||||
'state' => 'PA',
|
|
||||||
'postcode' => '19123',
|
|
||||||
'city' => 'Philadelphia',
|
|
||||||
'address' => '123 South Street',
|
|
||||||
'address_2' => 'Apt 1',
|
|
||||||
'shipping_country' => 'US',
|
|
||||||
'shipping_state' => 'PA',
|
|
||||||
'shipping_postcode' => '19123',
|
|
||||||
'shipping_city' => 'Philadelphia',
|
|
||||||
'shipping_address' => '123 South Street',
|
|
||||||
'shipping_address_2' => 'Apt 1',
|
|
||||||
'is_vat_exempt' => false,
|
|
||||||
'calculated_shipping' => false
|
|
||||||
);
|
|
||||||
|
|
||||||
$base_store_address = array( "GB", "", "", "" );
|
|
||||||
$customer_address = array( "US", "PA", "19123", "Philadelphia" );
|
|
||||||
|
|
||||||
//Initialize the session variables for the dummy customer.
|
|
||||||
|
|
||||||
// Get the original settings for the session and the options
|
// Get the original settings for the session and the WooCommerce options
|
||||||
$original_default_shipping_method = WC_Helper_Customer::get_default_shipping_method();
|
|
||||||
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
||||||
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
||||||
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
||||||
|
|
||||||
WC_Helper_Customer::set_customer_details( $customer_data );
|
$customer = WC_Helper_Customer::create_mock_customer();
|
||||||
|
|
||||||
//Create a dummy customer to use for testing!
|
|
||||||
$customer = new WC_Customer();
|
|
||||||
|
|
||||||
// Create dummy product, and add the product to the cart.
|
// Create dummy product, and add the product to the cart.
|
||||||
|
|
||||||
$product = WC_Helper_Product::create_simple_product();
|
$product = WC_Helper_Product::create_simple_product();
|
||||||
|
|
||||||
WC()->cart->add_to_cart( $product->id, 1 );
|
WC()->cart->add_to_cart( $product->id, 1 );
|
||||||
|
|
||||||
//Run through these tests twice - with two different selections for the store's default shipping options
|
// Customer is going with the Local Pickup option, and the store calculates tax based on the store location.
|
||||||
|
|
||||||
foreach( array('local_pickup') as $default_shipping_option ) {
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'base' );
|
||||||
|
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
||||||
|
|
||||||
WC_Helper_Customer::get_default_shipping_method( $default_shipping_option );
|
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
||||||
|
|
||||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the store location.
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
||||||
|
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
||||||
WC_Helper_Customer::set_tax_based_on( 'base' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
||||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
$this->assertEquals( $customer->get_taxable_address(), $customer_address );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
// Customer is going with the Free Shipping option, and the store calculates tax based on the store base location.
|
||||||
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'base' );
|
||||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
//Now reset the settings back to the way they were before this test
|
||||||
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->get_taxable_address(), $customer_address );
|
|
||||||
|
|
||||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the store base location.
|
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
|
||||||
WC_Helper_Customer::set_tax_based_on( 'base' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Reset the session and the options.
|
|
||||||
|
|
||||||
WC_Helper_Customer::set_default_shipping_method( $original_default_shipping_method );
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods );
|
WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods );
|
||||||
WC_Helper_Customer::set_tax_based_on( $original_tax_based_on );
|
WC_Helper_Customer::set_tax_based_on( $original_tax_based_on );
|
||||||
WC_Helper_Customer::set_customer_details( $original_customer_details );
|
WC_Helper_Customer::set_customer_details( $original_customer_details );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the is_customer_outside_base method
|
* Test the is_customer_outside_base method
|
||||||
*/
|
*/
|
||||||
public function test_is_customer_outside_base() {
|
public function test_is_customer_outside_base() {
|
||||||
|
|
||||||
$customer_data = array(
|
// Get the original settings for the session and the WooCommerce options
|
||||||
'country' => 'US',
|
|
||||||
'state' => 'PA',
|
|
||||||
'postcode' => '19123',
|
|
||||||
'city' => 'Philadelphia',
|
|
||||||
'address' => '123 South Street',
|
|
||||||
'address_2' => 'Apt 1',
|
|
||||||
'shipping_country' => 'US',
|
|
||||||
'shipping_state' => 'PA',
|
|
||||||
'shipping_postcode' => '19123',
|
|
||||||
'shipping_city' => 'Philadelphia',
|
|
||||||
'shipping_address' => '123 South Street',
|
|
||||||
'shipping_address_2' => 'Apt 1',
|
|
||||||
'is_vat_exempt' => false,
|
|
||||||
'calculated_shipping' => false
|
|
||||||
);
|
|
||||||
|
|
||||||
//Initialize the session variables for the dummy customer.
|
|
||||||
|
|
||||||
// Get the original settings for the session and the options
|
|
||||||
$original_default_shipping_method = WC_Helper_Customer::get_default_shipping_method();
|
|
||||||
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
||||||
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
||||||
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
||||||
|
|
||||||
WC_Helper_Customer::set_customer_details( $customer_data );
|
$customer = WC_Helper_Customer::create_mock_customer();
|
||||||
|
|
||||||
//Create a dummy customer to use for testing!
|
|
||||||
$customer = new WC_Customer();
|
|
||||||
|
|
||||||
// Create dummy product, and add the product to the cart.
|
// Create dummy product, and add the product to the cart.
|
||||||
$product = WC_Helper_Product::create_simple_product();
|
$product = WC_Helper_Product::create_simple_product();
|
||||||
|
|
||||||
WC()->cart->add_to_cart( $product->id, 1 );
|
WC()->cart->add_to_cart( $product->id, 1 );
|
||||||
|
|
||||||
//Run through these tests twice - with two different selections for the store's default shipping options
|
// Customer is going with the Local Pickup option, and the store calculates tax based on the store location.
|
||||||
|
|
||||||
foreach( array('local_pickup', 'free_shipping') as $default_shipping_option ) {
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'base' );
|
||||||
|
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
||||||
|
|
||||||
WC_Helper_Customer::get_default_shipping_method( $default_shipping_option );
|
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
||||||
|
|
||||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the store location.
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
||||||
|
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
||||||
WC_Helper_Customer::set_tax_based_on( 'base' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
||||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
$this->assertEquals( $customer->is_customer_outside_base(), true );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) );
|
// Customer is going with the Free Shipping option, and the store calculates tax based on the store base location.
|
||||||
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||||
|
WC_Helper_Customer::set_tax_based_on( 'base' );
|
||||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
//Now reset the settings back to the way they were before this test
|
||||||
WC_Helper_Customer::set_tax_based_on( 'billing' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->is_customer_outside_base(), true );
|
|
||||||
|
|
||||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the store base location.
|
|
||||||
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
|
||||||
WC_Helper_Customer::set_tax_based_on( 'base' );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Reset the session and the options.
|
|
||||||
|
|
||||||
WC_Helper_Customer::set_default_shipping_method( $original_default_shipping_method );
|
|
||||||
WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods );
|
WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods );
|
||||||
WC_Helper_Customer::set_tax_based_on( $original_tax_based_on );
|
WC_Helper_Customer::set_tax_based_on( $original_tax_based_on );
|
||||||
WC_Helper_Customer::set_customer_details( $original_customer_details );
|
WC_Helper_Customer::set_customer_details( $original_customer_details );
|
||||||
|
|
Loading…
Reference in New Issue