phpcs for discounts class
This commit is contained in:
parent
33b1c4205b
commit
630cae2483
|
@ -1,9 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for the discounts class.
|
* Test for the discounts class.
|
||||||
* @package WooCommerce\Tests\Discounts
|
* @package WooCommerce\Tests\Discounts
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WC_Tests_Discounts.
|
||||||
|
*/
|
||||||
class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,7 +33,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
* Helper function to hold a reference to created coupon objects so they
|
* Helper function to hold a reference to created coupon objects so they
|
||||||
* can be cleaned up properly at the end of each test.
|
* can be cleaned up properly at the end of each test.
|
||||||
*
|
*
|
||||||
* @param $coupon WC_Coupon The coupon object to store.
|
* @param WC_Coupon $coupon The coupon object to store.
|
||||||
*/
|
*/
|
||||||
protected function store_coupon( $coupon ) {
|
protected function store_coupon( $coupon ) {
|
||||||
$this->coupons[ $coupon->get_code() ] = $coupon;
|
$this->coupons[ $coupon->get_code() ] = $coupon;
|
||||||
|
@ -40,7 +43,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
* Helper function to hold a reference to created product objects so they
|
* Helper function to hold a reference to created product objects so they
|
||||||
* can be cleaned up properly at the end of each test.
|
* can be cleaned up properly at the end of each test.
|
||||||
*
|
*
|
||||||
* @param $product WC_Product The product object to store.
|
* @param WC_Product $product The product object to store.
|
||||||
*/
|
*/
|
||||||
protected function store_product( $product ) {
|
protected function store_product( $product ) {
|
||||||
$this->products[] = $product;
|
$this->products[] = $product;
|
||||||
|
@ -50,18 +53,21 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
* Helper function to hold a reference to created order objects so they
|
* Helper function to hold a reference to created order objects so they
|
||||||
* can be cleaned up properly at the end of each test.
|
* can be cleaned up properly at the end of each test.
|
||||||
*
|
*
|
||||||
* @param $order WC_Order The order object to store.
|
* @param WC_Order $order The order object to store.
|
||||||
*/
|
*/
|
||||||
protected function store_order( $order ) {
|
protected function store_order( $order ) {
|
||||||
$this->orders[] = $order;
|
$this->orders[] = $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup tests.
|
||||||
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->products = array();
|
$this->products = array();
|
||||||
$this->coupons = array();
|
$this->coupons = array();
|
||||||
$this->orders = array();
|
$this->orders = array();
|
||||||
$this->last_test_data = null;
|
$this->last_test_data = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +85,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
* Test get and set items.
|
* Test get and set items.
|
||||||
*/
|
*/
|
||||||
public function test_get_set_items_from_cart() {
|
public function test_get_set_items_from_cart() {
|
||||||
// Create dummy product - price will be 10
|
// Create dummy product - price will be 10.
|
||||||
$product = WC_Helper_Product::create_simple_product();
|
$product = WC_Helper_Product::create_simple_product();
|
||||||
$this->store_product( $product );
|
$this->store_product( $product );
|
||||||
|
|
||||||
|
@ -1336,14 +1342,19 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_free_shipping_coupon_no_products.
|
||||||
|
*/
|
||||||
public function test_free_shipping_coupon_no_products() {
|
public function test_free_shipping_coupon_no_products() {
|
||||||
$discounts = new WC_Discounts();
|
$discounts = new WC_Discounts();
|
||||||
$coupon = WC_Helper_Coupon::create_coupon( 'freeshipping' );
|
$coupon = WC_Helper_Coupon::create_coupon( 'freeshipping' );
|
||||||
$coupon->set_props( array(
|
$coupon->set_props(
|
||||||
'discount_type' => 'percent',
|
array(
|
||||||
'amount' => '',
|
'discount_type' => 'percent',
|
||||||
'free_shipping' => 'yes',
|
'amount' => '',
|
||||||
));
|
'free_shipping' => 'yes',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$discounts->apply_coupon( $coupon );
|
$discounts->apply_coupon( $coupon );
|
||||||
|
|
||||||
|
@ -1351,10 +1362,18 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
$this->assertEquals( 0, count( $all_discounts['freeshipping'] ), 'Free shipping coupon should not have any discounts.' );
|
$this->assertEquals( 0, count( $all_discounts['freeshipping'] ), 'Free shipping coupon should not have any discounts.' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* filter_woocommerce_coupon_get_discount_amount.
|
||||||
|
*
|
||||||
|
* @param float $discount Discount amount.
|
||||||
|
*/
|
||||||
public function filter_woocommerce_coupon_get_discount_amount( $discount ) {
|
public function filter_woocommerce_coupon_get_discount_amount( $discount ) {
|
||||||
return $discount / 2;
|
return $discount / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_coupon_discount_amount_filter.
|
||||||
|
*/
|
||||||
public function test_coupon_discount_amount_filter() {
|
public function test_coupon_discount_amount_filter() {
|
||||||
$discounts = new WC_Discounts();
|
$discounts = new WC_Discounts();
|
||||||
|
|
||||||
|
@ -1373,11 +1392,13 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
WC()->cart->add_to_cart( $product2->get_id(), 1 );
|
WC()->cart->add_to_cart( $product2->get_id(), 1 );
|
||||||
|
|
||||||
$coupon = WC_Helper_Coupon::create_coupon( 'test' );
|
$coupon = WC_Helper_Coupon::create_coupon( 'test' );
|
||||||
$coupon->set_props( array(
|
$coupon->set_props(
|
||||||
'code' => 'test',
|
array(
|
||||||
'discount_type' => 'percent',
|
'code' => 'test',
|
||||||
'amount' => '20',
|
'discount_type' => 'percent',
|
||||||
) );
|
'amount' => '20',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$discounts->set_items_from_cart( WC()->cart );
|
$discounts->set_items_from_cart( WC()->cart );
|
||||||
$discounts->apply_coupon( $coupon );
|
$discounts->apply_coupon( $coupon );
|
||||||
|
@ -1410,19 +1431,23 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
$product_sale->save();
|
$product_sale->save();
|
||||||
|
|
||||||
$coupon_percent = new WC_Coupon();
|
$coupon_percent = new WC_Coupon();
|
||||||
$coupon_percent->set_props( array(
|
$coupon_percent->set_props(
|
||||||
'amount' => 10,
|
array(
|
||||||
'discount_type' => 'percent',
|
'amount' => 10,
|
||||||
'exclude_sale_items' => false,
|
'discount_type' => 'percent',
|
||||||
) );
|
'exclude_sale_items' => false,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_percent->save();
|
$coupon_percent->save();
|
||||||
|
|
||||||
$coupon_percent_no_sale = new WC_Coupon();
|
$coupon_percent_no_sale = new WC_Coupon();
|
||||||
$coupon_percent_no_sale->set_props( array(
|
$coupon_percent_no_sale->set_props(
|
||||||
'amount' => 10,
|
array(
|
||||||
'discount_type' => 'percent',
|
'amount' => 10,
|
||||||
'exclude_sale_items' => true,
|
'discount_type' => 'percent',
|
||||||
) );
|
'exclude_sale_items' => true,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_percent_no_sale->save();
|
$coupon_percent_no_sale->save();
|
||||||
|
|
||||||
WC()->cart->empty_cart();
|
WC()->cart->empty_cart();
|
||||||
|
@ -1467,19 +1492,23 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
$product_sale->save();
|
$product_sale->save();
|
||||||
|
|
||||||
$coupon_cart = new WC_Coupon();
|
$coupon_cart = new WC_Coupon();
|
||||||
$coupon_cart->set_props( array(
|
$coupon_cart->set_props(
|
||||||
'amount' => 5,
|
array(
|
||||||
'discount_type' => 'fixed_cart',
|
'amount' => 5,
|
||||||
'exclude_sale_items' => false,
|
'discount_type' => 'fixed_cart',
|
||||||
) );
|
'exclude_sale_items' => false,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_cart->save();
|
$coupon_cart->save();
|
||||||
|
|
||||||
$coupon_cart_no_sale = new WC_Coupon();
|
$coupon_cart_no_sale = new WC_Coupon();
|
||||||
$coupon_cart_no_sale->set_props( array(
|
$coupon_cart_no_sale->set_props(
|
||||||
'amount' => 5,
|
array(
|
||||||
'discount_type' => 'fixed_cart',
|
'amount' => 5,
|
||||||
'exclude_sale_items' => true,
|
'discount_type' => 'fixed_cart',
|
||||||
) );
|
'exclude_sale_items' => true,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_cart_no_sale->save();
|
$coupon_cart_no_sale->save();
|
||||||
|
|
||||||
WC()->cart->empty_cart();
|
WC()->cart->empty_cart();
|
||||||
|
@ -1504,8 +1533,6 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the per product coupon logic with and without sale items.
|
* Test the per product coupon logic with and without sale items.
|
||||||
*
|
|
||||||
* @since 3.4.6
|
|
||||||
*/
|
*/
|
||||||
public function test_is_coupon_valid_fixed_product_sale_items() {
|
public function test_is_coupon_valid_fixed_product_sale_items() {
|
||||||
$product_no_sale = new WC_Product_Simple();
|
$product_no_sale = new WC_Product_Simple();
|
||||||
|
@ -1518,19 +1545,23 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
|
||||||
$product_sale->save();
|
$product_sale->save();
|
||||||
|
|
||||||
$coupon_product = new WC_Coupon();
|
$coupon_product = new WC_Coupon();
|
||||||
$coupon_product->set_props( array(
|
$coupon_product->set_props(
|
||||||
'amount' => 5,
|
array(
|
||||||
'discount_type' => 'fixed_product',
|
'amount' => 5,
|
||||||
'exclude_sale_items' => false,
|
'discount_type' => 'fixed_product',
|
||||||
) );
|
'exclude_sale_items' => false,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_product->save();
|
$coupon_product->save();
|
||||||
|
|
||||||
$coupon_product_no_sale = new WC_Coupon();
|
$coupon_product_no_sale = new WC_Coupon();
|
||||||
$coupon_product_no_sale->set_props( array(
|
$coupon_product_no_sale->set_props(
|
||||||
'amount' => 5,
|
array(
|
||||||
'discount_type' => 'fixed_product',
|
'amount' => 5,
|
||||||
'exclude_sale_items' => true,
|
'discount_type' => 'fixed_product',
|
||||||
) );
|
'exclude_sale_items' => true,
|
||||||
|
)
|
||||||
|
);
|
||||||
$coupon_product_no_sale->save();
|
$coupon_product_no_sale->save();
|
||||||
|
|
||||||
WC()->cart->empty_cart();
|
WC()->cart->empty_cart();
|
||||||
|
|
Loading…
Reference in New Issue