Merge pull request #6927 from purcebr/master
Unit Tests for WC_Customer class
This commit is contained in:
commit
b3cf6435b9
|
@ -100,6 +100,7 @@ class WC_Unit_Tests_Bootstrap {
|
|||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php' );
|
||||
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class WC_Helper_Customer
|
||||
*
|
||||
* This helper class should ONLY be used for unit tests!
|
||||
*/
|
||||
class WC_Helper_Customer {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
public static function get_customer_details() {
|
||||
return WC()->session->get( 'customer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's chosen shipping method.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public static function get_chosen_shipping_methods() {
|
||||
return WC()->session->get( 'chosen_shipping_methods' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "Tax Based On" WooCommerce option.
|
||||
*
|
||||
* @return string base or billing
|
||||
*/
|
||||
|
||||
public static function get_tax_based_on() {
|
||||
return get_option( 'woocommerce_tax_based_on' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the the current customer's billing details in the session.
|
||||
*
|
||||
* @param string $default_shipping_method Shipping Method slug
|
||||
*/
|
||||
|
||||
public static function set_customer_details( $customer_details ) {
|
||||
WC()->session->set( 'customer', $customer_details );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user's chosen shipping method.
|
||||
*
|
||||
* @param string $chosen_shipping_method Shipping Method slug
|
||||
*/
|
||||
|
||||
public static function set_chosen_shipping_methods( $chosen_shipping_methods ) {
|
||||
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "Tax Based On" WooCommerce option.
|
||||
*
|
||||
* @param string $default_shipping_method Shipping Method slug
|
||||
*/
|
||||
|
||||
public static function set_tax_based_on( $default_shipping_method ) {
|
||||
update_option( 'woocommerce_tax_based_on', $default_shipping_method );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
class WC_Tests_Customer extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Test the get_taxable_address method
|
||||
*/
|
||||
public function test_get_taxable_address() {
|
||||
|
||||
$base_store_address = WC_Helper_Customer::get_expected_store_location();
|
||||
$customer_address = WC_Helper_Customer::get_expected_customer_location();
|
||||
|
||||
// Get the original settings for the session and the WooCommerce options
|
||||
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
||||
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
||||
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
||||
|
||||
$customer = WC_Helper_Customer::create_mock_customer();
|
||||
|
||||
// Create dummy product, and add the product to the cart.
|
||||
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
WC()->cart->add_to_cart( $product->id, 1 );
|
||||
|
||||
// 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( 'base' );
|
||||
$this->assertEquals( $customer->get_taxable_address(), $base_store_address );
|
||||
|
||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
||||
|
||||
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 );
|
||||
|
||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
||||
|
||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||
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 );
|
||||
|
||||
//Now reset the settings back to the way they were before this test
|
||||
|
||||
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_customer_details( $original_customer_details );
|
||||
|
||||
// Clean up the cart
|
||||
WC()->cart->empty_cart();
|
||||
|
||||
// Clean up product
|
||||
WC_Helper_Product::delete_product( $product->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the is_customer_outside_base method
|
||||
*/
|
||||
public function test_is_customer_outside_base() {
|
||||
|
||||
// Get the original settings for the session and the WooCommerce options
|
||||
$original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods();
|
||||
$original_tax_based_on = WC_Helper_Customer::get_tax_based_on();
|
||||
$original_customer_details = WC_Helper_Customer::get_customer_details();
|
||||
|
||||
$customer = WC_Helper_Customer::create_mock_customer();
|
||||
|
||||
// Create dummy product, and add the product to the cart.
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
WC()->cart->add_to_cart( $product->id, 1 );
|
||||
|
||||
// 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( 'base' );
|
||||
$this->assertEquals( $customer->is_customer_outside_base(), false );
|
||||
|
||||
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
|
||||
|
||||
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 );
|
||||
|
||||
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
|
||||
|
||||
WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) );
|
||||
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 );
|
||||
|
||||
//Now reset the settings back to the way they were before this test
|
||||
|
||||
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_customer_details( $original_customer_details );
|
||||
|
||||
// Clean up the cart
|
||||
WC()->cart->empty_cart();
|
||||
|
||||
// Clean up product
|
||||
WC_Helper_Product::delete_product( $product->id );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue