Add tests for the getter/setter methods

This commit is contained in:
Justin Shreve 2016-02-25 10:58:19 -08:00
parent 1e8c45fba5
commit 50e1bc7117
2 changed files with 368 additions and 0 deletions

View File

@ -810,6 +810,7 @@ class WC_Coupon extends WC_Legacy_Coupon implements WC_Data {
update_post_meta( $this->get_id(), 'usage_count', $this->get_usage_count() );
if ( $used_by ) {
add_post_meta( $this->get_id(), '_used_by', strtolower( $used_by ) );
$this->set_used_by( (array) get_post_meta( $this->get_id(), '_used_by' ) );
}
}
}
@ -832,6 +833,7 @@ class WC_Coupon extends WC_Legacy_Coupon implements WC_Data {
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_used_by' AND meta_value = %s AND post_id = %d LIMIT 1;", $used_by, $this->get_id() ) );
if ( $meta_id ) {
delete_metadata_by_mid( 'post', $meta_id );
$this->set_used_by( (array) get_post_meta( $this->get_id(), '_used_by' ) );
}
}
}

View File

@ -197,5 +197,371 @@ class CouponCRUD extends \WC_Unit_Test_Case {
$this->assertEquals( array(), $coupon->get_product_ids() );
}
/**
* Test getting a coupon's ID
* @since 2.6.0
*/
public function test_coupon_get_id() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertNotEquals( 0, $coupon->get_id() );
}
/**
* Test getting a coupon's code
* @since 2.6.0
*/
public function test_coupon_get_code() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( 'dummycoupon', $coupon->get_code() );
}
/**
* Test setting a coupon's code
* @since 2.6.0
*/
public function test_coupon_set_code() {
$coupon = \WC_Helper_Coupon::create_coupon();
$code = 'coupon-' . time();
$coupon->set_code( $code );
$this->assertEquals( $code, $coupon->get_code() );
}
/**
* Test getting a coupon's description
* @since 2.6.0
*/
public function test_coupon_get_description() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( 'This is a dummy coupon', $coupon->get_description() );
}
/**
* Test setting a coupon's description
* @since 2.6.0
*/
public function test_coupon_set_description() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_description( 'This is an updated coupon' );
$this->assertEquals( 'This is an updated coupon', $coupon->get_description() );
}
/**
* Test getting a coupon's discount type
* @since 2.6.0
*/
public function test_coupon_get_discount_type() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( 'fixed_cart', $coupon->get_discount_type() );
}
/**
* Test setting a coupon's discount type
* @since 2.6.0
*/
public function test_coupon_set_discount_type() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_discount_type( 'percent_product' );
$this->assertEquals( 'percent_product', $coupon->get_discount_type() );
}
/**
* Test getting a coupon's amount
* @since 2.6.0
*/
public function test_coupon_get_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( 1, $coupon->get_amount() );
}
/**
* Test setting a coupon's amount
* @since 2.6.0
*/
public function test_coupon_set_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_amount( 5 );
$this->assertEquals( 5, $coupon->get_amount() );
}
/**
* Test getting a coupon's usage count
* @since 2.6.0
*/
public function test_coupon_get_usage_count() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( 0, $coupon->get_usage_count() );
}
/**
* Test setting a coupon's usage count
* @since 2.6.0
*/
public function test_coupon_set_usage_count() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_usage_count( 6 );
$this->assertEquals( 6, $coupon->get_usage_count() );
}
/**
* Test getting indvidual use status
* @since 2.6.0
*/
public function test_coupon_get_individual_use() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertFalse( $coupon->get_individual_use() );
}
/**
* Test setting individual use status
* @since 2.6.0
*/
public function test_coupon_set_individual_use() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_individual_use( true );
$this->assertTrue( $coupon->get_individual_use() );
}
/**
* Test getting product ids
* @since 2.6.0
*/
public function test_coupon_get_product_ids() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_product_ids() );
}
/**
* Test setting product ids
* @since 2.6.0
*/
public function test_coupon_set_product_ids() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_product_ids( array( 322, 333 ) );
$this->assertEquals( array( 322, 333 ), $coupon->get_product_ids() );
}
/**
* Test getting excluded product ids
* @since 2.6.0
*/
public function test_coupon_get_excluded_product_ids() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_excluded_product_ids() );
}
/**
* Test setting excluded product ids
* @since 2.6.0
*/
public function test_coupon_set_excluded_product_ids() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_excluded_product_ids( array( 322, 333 ) );
$this->assertEquals( array( 322, 333 ), $coupon->get_excluded_product_ids() );
}
/**
* Test getting coupon usage limit
* @since 2.6.0
*/
public function test_coupon_get_usage_limit() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEmpty( $coupon->get_usage_limit() );
}
/**
* Test setting coupon usage limit
* @since 2.6.0
*/
public function test_coupon_set_usage_limit() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_usage_limit( 100 );
$this->assertEquals( 100, $coupon->get_usage_limit() );
}
/**
* Test getting coupon usage limit per user
* @since 2.6.0
*/
public function test_coupon_get_usage_limit_per_user() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEmpty( $coupon->get_usage_limit_per_user() );
}
/**
* Test setting coupon usage limit per user
* @since 2.6.0
*/
public function test_coupon_set_usage_limit_per_user() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_usage_limit_per_user( 10 );
$this->assertEquals( 10, $coupon->get_usage_limit_per_user() );
}
/**
* Test getting coupon usage limit to x items
* @since 2.6.0
*/
public function test_coupon_get_limit_usage_to_x_items() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEmpty( $coupon->get_limit_usage_to_x_items() );
}
/**
* Test setting coupon usage limit to x items
* @since 2.6.0
*/
public function test_coupon_set_limit_usage_to_x_items() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_limit_usage_to_x_items( 10 );
$this->assertEquals( 10, $coupon->get_limit_usage_to_x_items() );
}
/**
* Test getting free shipping status
* @since 2.6.0
*/
public function test_coupon_get_free_shipping() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertFalse( $coupon->get_free_shipping() );
}
/**
* Test setting free shipping status
* @since 2.6.0
*/
public function test_coupon_set_free_shipping() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_free_shipping( true );
$this->assertTrue( $coupon->get_free_shipping() );
}
/**
* Test getting product categories
* @since 2.6.0
*/
public function test_coupon_get_product_categories() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_product_categories() );
}
/**
* Test setting product categories
* @since 2.6.0
*/
public function test_coupon_set_product_categories() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_product_categories( array( 322, 333 ) );
$this->assertEquals( array( 322, 333 ), $coupon->get_product_categories() );
}
/**
* Test getting excluded product categories
* @since 2.6.0
*/
public function test_coupon_get_excluded_product_categories() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_excluded_product_categories() );
}
/**
* Test setting excluded product categories
* @since 2.6.0
*/
public function test_coupon_set_excluded_product_categories() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_excluded_product_categories( array( 322, 333 ) );
$this->assertEquals( array( 322, 333 ), $coupon->get_excluded_product_categories() );
}
/**
* Test getting exclude sale items status
* @since 2.6.0
*/
public function test_coupon_get_exclude_sale_items() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertFalse( $coupon->get_exclude_sale_items() );
}
/**
* Test setting exclude sale items status
* @since 2.6.0
*/
public function test_coupon_set_exclude_sale_items() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_exclude_sale_items( true );
$this->assertTrue( $coupon->get_exclude_sale_items() );
}
/**
* Test getting a coupon's minimum amount
* @since 2.6.0
*/
public function test_coupon_get_minimum_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEmpty( $coupon->get_minimum_amount() );
}
/**
* Test setting a coupon's minimum amount
* @since 2.6.0
*/
public function test_coupon_set_minimum_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_minimum_amount( 5 );
$this->assertEquals( 5, $coupon->get_minimum_amount() );
}
/**
* Test getting a coupon's maximum amount
* @since 2.6.0
*/
public function test_coupon_get_maximum_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEmpty( $coupon->get_maximum_amount() );
}
/**
* Test setting a coupon's maximum amount
* @since 2.6.0
*/
public function test_coupon_set_maximum_amount() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_maximum_amount( 5 );
$this->assertEquals( 5, $coupon->get_maximum_amount() );
}
/**
* Test getting coupon email restrictions
* @since 2.6.0
*/
public function test_coupon_get_email_restrictions() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_email_restrictions() );
}
/**
* Test setting coupon email restrictions
* @since 2.6.0
*/
public function test_coupon_set_email_restrictions() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon->set_email_restrictions( array( 'test@test.local', 'woo@test.local' ) );
$this->assertEquals( array( 'test@test.local', 'woo@test.local' ), $coupon->get_email_restrictions() );
}
/**
* Test getting and setting coupon used by list
* @since 2.6.0
*/
public function test_coupon_get_and_set_used_by() {
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( array(), $coupon->get_used_by() );
$coupon->inc_usage_count( 1 );
$this->assertEquals( array( 1 ), $coupon->get_used_by() );
$coupon->dcr_usage_count( 1 );
$this->assertEquals( array(), $coupon->get_used_by() );
}
}