From 720ef008d78723d93b6c7cb275cfb3f68605628d Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Thu, 30 Oct 2014 16:08:33 -0200 Subject: [PATCH] Unit Tests: Created WC_Helper_Fee class and WC_Tests_Cart::test_cart_fee method --- tests/bootstrap.php | 1 + .../framework/helpers/class-wc-helper-fee.php | 40 +++++++++++++++++++ tests/unit-tests/cart.php | 38 ++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/framework/helpers/class-wc-helper-fee.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 44b9d1e9723..083af117946 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -99,6 +99,7 @@ class WC_Unit_Tests_Bootstrap { // Helpers require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-product.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php' ); + require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php' ); } /** diff --git a/tests/framework/helpers/class-wc-helper-fee.php b/tests/framework/helpers/class-wc-helper-fee.php new file mode 100644 index 00000000000..71ee69a90f9 --- /dev/null +++ b/tests/framework/helpers/class-wc-helper-fee.php @@ -0,0 +1,40 @@ +cart->add_fee( 'Dummy Fee', 10 ); + } + + /** + * Add a cart simple fee without taxes + * + * @since 2.3 + */ + public static function add_cart_fee() { + add_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_simple_fee' ) ); + } + + /** + * Remove a cart simple fee without taxes + * + * @since 2.3 + */ + public static function remove_cart_fee() { + remove_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_simple_fee' ) ); + } +} diff --git a/tests/unit-tests/cart.php b/tests/unit-tests/cart.php index 28b824e53e2..2bcd852b97b 100644 --- a/tests/unit-tests/cart.php +++ b/tests/unit-tests/cart.php @@ -429,4 +429,42 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { $this->assertEquals( apply_filters( 'woocommerce_cart_needs_shipping_address', $needs_shipping_address ), WC()->cart->needs_shipping_address() ); } + /** + * Test cart fee + * + * @since 2.3 + */ + public function test_cart_fee() { + // Create product + $product = WC_Helper_Product::create_simple_product(); + update_post_meta( $product->id, '_price', '10' ); + update_post_meta( $product->id, '_regular_price', '10' ); + + // 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 ); + + // Test if the cart total amount is equal 20 + $this->assertEquals( 20, 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 product + WC_Helper_Product::delete_product( $product->id ); + } + }