Update discount tests

This commit is contained in:
Mike Jolley 2017-07-25 16:27:57 +01:00
parent 474799889f
commit 4960bf0ca4
1 changed files with 35 additions and 12 deletions

View File

@ -3,12 +3,35 @@
/** /**
* Test for the discounts class. * Test for the discounts class.
* @package WooCommerce\Tests\Discounts * @package WooCommerce\Tests\Discounts
* @todo update tests for new 'items' stucture, or handle other data.
*/ */
return;
class WC_Tests_Discounts extends WC_Unit_Test_Case { class WC_Tests_Discounts extends WC_Unit_Test_Case {
protected function get_items_for_discounts_class( $items ) {
$items = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = (object) array(
'key' => '',
'quantity' => 0,
'price' => 0,
'product' => false,
'price_includes_tax' => wc_prices_include_tax(),
'subtotal' => 0,
'subtotal_tax' => 0,
'subtotal_taxes' => array(),
'total' => 0,
'total_tax' => 0,
'taxes' => array(),
'discounted_price' => 0,
);
$item->key = $cart_item_key;
$item->quantity = $cart_item['quantity'];
$item->price = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
$item->product = $cart_item['data'];
$items[ $cart_item_key ] = $item;
}
return $items;
}
/** /**
* Test get and set items. * Test get and set items.
*/ */
@ -27,12 +50,12 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
// Test setting items to the cart. // Test setting items to the cart.
$discounts = new WC_Discounts(); $discounts = new WC_Discounts();
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$this->assertEquals( 1, count( $discounts->get_items() ) ); $this->assertEquals( 1, count( $discounts->get_items() ) );
// Test setting items to an order. // Test setting items to an order.
$discounts = new WC_Discounts(); $discounts = new WC_Discounts();
$discounts->set_items( $order->get_items() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$this->assertEquals( 1, count( $discounts->get_items() ) ); $this->assertEquals( 1, count( $discounts->get_items() ) );
// Empty array of items. // Empty array of items.
@ -58,7 +81,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
$discounts = new WC_Discounts(); $discounts = new WC_Discounts();
$product = WC_Helper_Product::create_simple_product(); $product = WC_Helper_Product::create_simple_product();
WC()->cart->add_to_cart( $product->get_id(), 1 ); WC()->cart->add_to_cart( $product->get_id(), 1 );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
// Test applying multiple coupons and getting totals. // Test applying multiple coupons and getting totals.
$coupon = new WC_Coupon; $coupon = new WC_Coupon;
@ -85,12 +108,12 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
WC()->cart->add_to_cart( $product->get_id(), 2 ); WC()->cart->add_to_cart( $product->get_id(), 2 );
$coupon->set_discount_type( 'fixed_product' ); $coupon->set_discount_type( 'fixed_product' );
$coupon->set_amount( 2 ); $coupon->set_amount( 2 );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$discounts->apply_coupon( $coupon ); $discounts->apply_coupon( $coupon );
$this->assertEquals( array( 'test' => 4 ), $discounts->get_applied_coupons() ); $this->assertEquals( array( 'test' => 4 ), $discounts->get_applied_coupons() );
$coupon->set_discount_type( 'fixed_cart' ); $coupon->set_discount_type( 'fixed_cart' );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$discounts->apply_coupon( $coupon ); $discounts->apply_coupon( $coupon );
$this->assertEquals( array( 'test' => 2 ), $discounts->get_applied_coupons() ); $this->assertEquals( array( 'test' => 2 ), $discounts->get_applied_coupons() );
@ -117,19 +140,19 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
// Apply a percent discount. // Apply a percent discount.
$coupon->set_discount_type( 'percent' ); $coupon->set_discount_type( 'percent' );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$discounts->apply_coupon( $coupon ); $discounts->apply_coupon( $coupon );
$this->assertEquals( 9, $discounts->get_discounted_price( current( $discounts->get_items() ) ) ); $this->assertEquals( 9, $discounts->get_discounted_price( current( $discounts->get_items() ) ) );
// Apply a fixed cart coupon. // Apply a fixed cart coupon.
$coupon->set_discount_type( 'fixed_cart' ); $coupon->set_discount_type( 'fixed_cart' );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$discounts->apply_coupon( $coupon ); $discounts->apply_coupon( $coupon );
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ) ); $this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ) );
// Apply a fixed product coupon. // Apply a fixed product coupon.
$coupon->set_discount_type( 'fixed_product' ); $coupon->set_discount_type( 'fixed_product' );
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
$discounts->apply_coupon( $coupon ); $discounts->apply_coupon( $coupon );
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ) ); $this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ) );
@ -399,7 +422,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
$products[] = $product; $products[] = $product;
} }
$discounts->set_items( WC()->cart->get_cart() ); $discounts->set_items( $this->get_items_for_discounts_class() );
foreach ( $test['coupons'] as $coupon_props ) { foreach ( $test['coupons'] as $coupon_props ) {
$coupon = new WC_Coupon; $coupon = new WC_Coupon;