Unit Tests: Created WC_Helper_Fee class and WC_Tests_Cart::test_cart_fee method

This commit is contained in:
claudiosmweb 2014-10-30 16:08:33 -02:00
parent 9dd82ad5a6
commit 720ef008d7
3 changed files with 79 additions and 0 deletions

View File

@ -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' );
}
/**

View File

@ -0,0 +1,40 @@
<?php
/**
* Class WC_Helper_Fee
*
* This helper class should ONLY be used for unit tests!
*/
class WC_Helper_Fee {
/**
* Create a cart simple fee without taxes
*
* @since 2.3
*/
public static function create_simple_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
WC()->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' ) );
}
}

View File

@ -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 );
}
}