Unit Tests: Created test_fixed_product_discount and test_percent_product_discount methods
This commit is contained in:
parent
720ef008d7
commit
8faf3ec85a
|
@ -22,6 +22,7 @@ class WC_Helper_Fee {
|
|||
|
||||
/**
|
||||
* Add a cart simple fee without taxes
|
||||
* Note: need to be added before add any product in the cart
|
||||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
|
|
|
@ -141,4 +141,104 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
|
|||
// Delete product
|
||||
WC_Helper_Product::delete_product( $product->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test fixed product discount method
|
||||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
public function test_fixed_product_discount() {
|
||||
|
||||
// Create product
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
update_post_meta( $product->id, '_price', '10' );
|
||||
update_post_meta( $product->id, '_regular_price', '10' );
|
||||
|
||||
// Create coupon
|
||||
$coupon = WC_Helper_Coupon::create_coupon();
|
||||
update_post_meta( $coupon->id, 'discount_type', 'fixed_product' );
|
||||
update_post_meta( $coupon->id, 'coupon_amount', '5' );
|
||||
|
||||
// We need this to have the calculate_totals() method calculate totals
|
||||
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
|
||||
define( 'WOOCOMMERCE_CHECKOUT', true );
|
||||
}
|
||||
|
||||
// Add fee
|
||||
WC_Helper_Fee::add_cart_fee();
|
||||
|
||||
// Add product to cart
|
||||
WC()->cart->add_to_cart( $product->id, 1 );
|
||||
|
||||
// Add coupon
|
||||
WC()->cart->add_discount( $coupon->code );
|
||||
|
||||
// Test if the cart total amount is equal 19.5
|
||||
$this->assertEquals( 15, WC()->cart->total );
|
||||
|
||||
// Clearing WC notices
|
||||
wc_clear_notices();
|
||||
|
||||
// Clean up the cart
|
||||
WC()->cart->empty_cart();
|
||||
|
||||
// Remove fee
|
||||
WC_Helper_Fee::remove_cart_fee();
|
||||
|
||||
// Delete coupon
|
||||
WC_Helper_Coupon::delete_coupon( $coupon->id );
|
||||
|
||||
// Delete product
|
||||
WC_Helper_Product::delete_product( $product->id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test percent product discount method
|
||||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
public function test_percent_product_discount() {
|
||||
|
||||
// Create product
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
update_post_meta( $product->id, '_price', '10' );
|
||||
update_post_meta( $product->id, '_regular_price', '10' );
|
||||
|
||||
// Create coupon
|
||||
$coupon = WC_Helper_Coupon::create_coupon();
|
||||
update_post_meta( $coupon->id, 'discount_type', 'percent_product' );
|
||||
update_post_meta( $coupon->id, 'coupon_amount', '5' );
|
||||
|
||||
// We need this to have the calculate_totals() method calculate totals
|
||||
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
|
||||
define( 'WOOCOMMERCE_CHECKOUT', true );
|
||||
}
|
||||
|
||||
// Add fee
|
||||
WC_Helper_Fee::add_cart_fee();
|
||||
|
||||
// Add product to cart
|
||||
WC()->cart->add_to_cart( $product->id, 1 );
|
||||
|
||||
// Add coupon
|
||||
WC()->cart->add_discount( $coupon->code );
|
||||
|
||||
// Test if the cart total amount is equal 19.5
|
||||
$this->assertEquals( 19.5, WC()->cart->total );
|
||||
|
||||
// Clearing WC notices
|
||||
wc_clear_notices();
|
||||
|
||||
// Clean up the cart
|
||||
WC()->cart->empty_cart();
|
||||
|
||||
// Remove fee
|
||||
WC_Helper_Fee::remove_cart_fee();
|
||||
|
||||
// Delete coupon
|
||||
WC_Helper_Coupon::delete_coupon( $coupon->id );
|
||||
|
||||
// Delete product
|
||||
WC_Helper_Product::delete_product( $product->id );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue