Use new code style
This commit is contained in:
@ -12,11 +12,11 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_active_discount_code() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_active(true);
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertTrue( $result );
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,13 +24,13 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_inactive_discount_code() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( false );
|
||||
$discount_code->set_active(false);
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
$this->assertEquals( 'discount_code', $result->get_error_code() );
|
||||
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
|
||||
$this->assertInstanceOf(WP_Error::class, $result);
|
||||
$this->assertEquals('discount_code', $result->get_error_code());
|
||||
$this->assertEquals('This coupon code is not valid.', $result->get_error_message());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,16 +38,18 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_max_uses_exceeded() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_max_uses( 5 );
|
||||
$discount_code->set_uses( 5 );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_max_uses(5);
|
||||
$discount_code->set_uses(5);
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
$this->assertEquals( 'discount_code', $result->get_error_code() );
|
||||
$this->assertEquals( 'This discount code was already redeemed the maximum amount of times allowed.',
|
||||
$result->get_error_message() );
|
||||
$this->assertInstanceOf(WP_Error::class, $result);
|
||||
$this->assertEquals('discount_code', $result->get_error_code());
|
||||
$this->assertEquals(
|
||||
'This discount code was already redeemed the maximum amount of times allowed.',
|
||||
$result->get_error_message()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,14 +57,14 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_before_start_date() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_date_start( date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ) );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_date_start(date('Y-m-d H:i:s', strtotime('+1 day')));
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
$this->assertEquals( 'discount_code', $result->get_error_code() );
|
||||
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
|
||||
$this->assertInstanceOf(WP_Error::class, $result);
|
||||
$this->assertEquals('discount_code', $result->get_error_code());
|
||||
$this->assertEquals('This coupon code is not valid.', $result->get_error_message());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,14 +72,14 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_after_expiration_date() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_date_expiration( date( 'Y-m-d H:i:s', strtotime( '-1 day' ) ) );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_date_expiration(date('Y-m-d H:i:s', strtotime('-1 day')));
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
$this->assertEquals( 'discount_code', $result->get_error_code() );
|
||||
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
|
||||
$this->assertInstanceOf(WP_Error::class, $result);
|
||||
$this->assertEquals('discount_code', $result->get_error_code());
|
||||
$this->assertEquals('This coupon code is not valid.', $result->get_error_message());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,13 +88,13 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
public function test_is_valid_for_allowed_product() {
|
||||
$product_id = 123;
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_limit_products( true );
|
||||
$discount_code->set_allowed_products( [ $product_id ] );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_limit_products(true);
|
||||
$discount_code->set_allowed_products(array($product_id));
|
||||
|
||||
$result = $discount_code->is_valid( $product_id );
|
||||
$result = $discount_code->is_valid($product_id);
|
||||
|
||||
$this->assertTrue( $result );
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,15 +104,15 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
$allowed_product_id = 123;
|
||||
$disallowed_product_id = 456;
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_limit_products( true );
|
||||
$discount_code->set_allowed_products( [ $allowed_product_id ] );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_limit_products(true);
|
||||
$discount_code->set_allowed_products(array($allowed_product_id));
|
||||
|
||||
$result = $discount_code->is_valid( $disallowed_product_id );
|
||||
$result = $discount_code->is_valid($disallowed_product_id);
|
||||
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
$this->assertEquals( 'discount_code', $result->get_error_code() );
|
||||
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
|
||||
$this->assertInstanceOf(WP_Error::class, $result);
|
||||
$this->assertEquals('discount_code', $result->get_error_code());
|
||||
$this->assertEquals('This coupon code is not valid.', $result->get_error_message());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,11 +120,11 @@ class Discount_Code_Test extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_valid_no_product_limits() {
|
||||
$discount_code = new Discount_Code();
|
||||
$discount_code->set_active( true );
|
||||
$discount_code->set_limit_products( false );
|
||||
$discount_code->set_active(true);
|
||||
$discount_code->set_limit_products(false);
|
||||
|
||||
$result = $discount_code->is_valid();
|
||||
|
||||
$this->assertTrue( $result );
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,10 +12,10 @@ class Domain_Test extends WP_UnitTestCase {
|
||||
public function test_has_valid_ssl_certificate_with_valid_certificate() {
|
||||
// Mocking a domain with a valid SSL certificate.
|
||||
$domain = new Domain();
|
||||
$domain->set_domain( 'dogs.4thelols.uk' );
|
||||
$domain->set_domain('dogs.4thelols.uk');
|
||||
|
||||
// Assert that it returns true for a valid SSL certificate.
|
||||
$this->assertTrue( $domain->has_valid_ssl_certificate() );
|
||||
$this->assertTrue($domain->has_valid_ssl_certificate());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,10 +24,10 @@ class Domain_Test extends WP_UnitTestCase {
|
||||
public function test_has_valid_ssl_certificate_with_invalid_certificate() {
|
||||
// Mocking a domain with an invalid SSL certificate.
|
||||
$domain = new Domain();
|
||||
$domain->set_domain( 'eeeeeeeeeeeeeeeeauauexample.com' );
|
||||
$domain->set_domain('eeeeeeeeeeeeeeeeauauexample.com');
|
||||
|
||||
// Assert that it returns false for an invalid SSL certificate.
|
||||
$this->assertFalse( $domain->has_valid_ssl_certificate() );
|
||||
$this->assertFalse($domain->has_valid_ssl_certificate());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,9 +36,9 @@ class Domain_Test extends WP_UnitTestCase {
|
||||
public function test_has_valid_ssl_certificate_with_empty_domain() {
|
||||
// Mocking a domain with an empty value.
|
||||
$domain = new Domain();
|
||||
$domain->set_domain( '' );
|
||||
$domain->set_domain('');
|
||||
|
||||
// Assert that it returns false for an empty domain.
|
||||
$this->assertFalse( $domain->has_valid_ssl_certificate() );
|
||||
$this->assertFalse($domain->has_valid_ssl_certificate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
$this->membership = new Membership();
|
||||
|
||||
// Set a default customer ID.
|
||||
$this->membership->set_customer_id( 123 );
|
||||
$this->membership->set_customer_id(123);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,23 +34,23 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
*/
|
||||
public function test_is_customer_allowed() {
|
||||
// Admins with 'manage_network' capability should always return true.
|
||||
$admin_user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
|
||||
grant_super_admin( $admin_user_id );
|
||||
wp_set_current_user( $admin_user_id );
|
||||
$this->assertTrue( $this->membership->is_customer_allowed(), 'Failed asserting that admin is allowed.' );
|
||||
$admin_user_id = $this->factory()->user->create(array('role' => 'administrator'));
|
||||
grant_super_admin($admin_user_id);
|
||||
wp_set_current_user($admin_user_id);
|
||||
$this->assertTrue($this->membership->is_customer_allowed(), 'Failed asserting that admin is allowed.');
|
||||
|
||||
// Regular customers are allowed if IDs match.
|
||||
$customer_id = 123;
|
||||
$this->assertTrue(
|
||||
$this->membership->is_customer_allowed( $customer_id ),
|
||||
$this->membership->is_customer_allowed($customer_id),
|
||||
'Failed asserting that customer with matching ID is allowed.'
|
||||
);
|
||||
|
||||
// Regular customers are denied if IDs do not match.
|
||||
$wrong_customer_id = 456;
|
||||
wp_set_current_user( $wrong_customer_id );
|
||||
wp_set_current_user($wrong_customer_id);
|
||||
$this->assertFalse(
|
||||
$this->membership->is_customer_allowed( $wrong_customer_id ),
|
||||
$this->membership->is_customer_allowed($wrong_customer_id),
|
||||
'Failed asserting that customer with non-matching ID is denied.'
|
||||
);
|
||||
}
|
||||
@ -64,14 +64,14 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
$faker = new Faker();
|
||||
$faker->generate_fake_products();
|
||||
/** @var Product $product */
|
||||
$product = $faker->get_fake_data_generated( 'products' )[0];
|
||||
$product = $faker->get_fake_data_generated('products')[0];
|
||||
$product_id = $product->get_id();
|
||||
|
||||
$this->membership->add_product( $product_id, $quantity );
|
||||
$this->membership->add_product($product_id, $quantity);
|
||||
|
||||
// Verify that the product is added with the correct quantity.
|
||||
$addon_products = $this->membership->get_addon_ids();
|
||||
$this->assertContains( $product_id, $addon_products, 'Failed asserting that product ID was added.' );
|
||||
$this->assertContains($product_id, $addon_products, 'Failed asserting that product ID was added.');
|
||||
$this->assertEquals(
|
||||
$quantity,
|
||||
$this->membership->get_addon_products()[0]['quantity'],
|
||||
@ -80,7 +80,7 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
|
||||
// Add more of the same product and check the updated quantity.
|
||||
$additional_quantity = 3;
|
||||
$this->membership->add_product( $product_id, $additional_quantity );
|
||||
$this->membership->add_product($product_id, $additional_quantity);
|
||||
|
||||
$this->assertEquals(
|
||||
$quantity + $additional_quantity,
|
||||
@ -98,14 +98,14 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
$faker = new Faker();
|
||||
$faker->generate_fake_products();
|
||||
/** @var Product $product */
|
||||
$product = $faker->get_fake_data_generated( 'products' )[0];
|
||||
$product = $faker->get_fake_data_generated('products')[0];
|
||||
$product_id = $product->get_id();
|
||||
|
||||
$this->membership->add_product( $product_id, $quantity );
|
||||
$this->membership->add_product($product_id, $quantity);
|
||||
|
||||
// Remove some of the product's quantity.
|
||||
$remove_quantity = 3;
|
||||
$this->membership->remove_product( $product_id, $remove_quantity );
|
||||
$this->membership->remove_product($product_id, $remove_quantity);
|
||||
|
||||
// Verify the updated quantity.
|
||||
$this->assertEquals(
|
||||
@ -115,18 +115,18 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Remove the remaining quantity and verify it is removed.
|
||||
$this->membership->remove_product( $product_id, $quantity );
|
||||
$this->membership->remove_product($product_id, $quantity);
|
||||
$addon_products = $this->membership->get_addon_ids();
|
||||
$this->assertNotContains( $product_id, $addon_products, 'Failed asserting that the product was removed.' );
|
||||
$this->assertNotContains($product_id, $addon_products, 'Failed asserting that the product was removed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_remaining_days_in_cycle() method.
|
||||
*/
|
||||
public function test_get_remaining_days_in_cycle() {
|
||||
$this->membership->set_amount( 12.99 );
|
||||
$this->membership->set_amount(12.99);
|
||||
// Case 1: Non-recurring membership should return 10000.
|
||||
$this->membership->set_recurring( false );
|
||||
$this->membership->set_recurring(false);
|
||||
$this->assertEquals(
|
||||
10000,
|
||||
$this->membership->get_remaining_days_in_cycle(),
|
||||
@ -134,8 +134,8 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Case 2: Invalid expiration date should return 0.
|
||||
$this->membership->set_recurring( true );
|
||||
$this->membership->set_date_expiration( 'invalid-date' ); // Setting an invalid date.
|
||||
$this->membership->set_recurring(true);
|
||||
$this->membership->set_date_expiration('invalid-date'); // Setting an invalid date.
|
||||
$this->assertEquals(
|
||||
0,
|
||||
$this->membership->get_remaining_days_in_cycle(),
|
||||
@ -143,7 +143,7 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Case 3: No expiration date should return 0.
|
||||
$this->membership->set_date_expiration( '' );
|
||||
$this->membership->set_date_expiration('');
|
||||
$this->assertEquals(
|
||||
0,
|
||||
$this->membership->get_remaining_days_in_cycle(),
|
||||
@ -151,9 +151,9 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Case 4: Expiration date is in the future and remaining days are calculated properly.
|
||||
$today = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
|
||||
$today->add( new \DateInterval( 'P10D' ) );
|
||||
$this->membership->set_date_expiration( $today->format( 'Y-m-d H:i:s' ) );
|
||||
$today = new \DateTime('now', new \DateTimeZone('UTC'));
|
||||
$today->add(new \DateInterval('P10D'));
|
||||
$this->membership->set_date_expiration($today->format('Y-m-d H:i:s'));
|
||||
$remaining_days = $this->membership->get_remaining_days_in_cycle();
|
||||
$this->assertEquals(
|
||||
10,
|
||||
@ -162,7 +162,7 @@ class Membership_Test extends \WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Case 5: Expiration date is in the past, should return 0.
|
||||
$this->membership->set_date_expiration( date( 'Y-m-d H:i:s', strtotime( '-5 days' ) ) );
|
||||
$this->membership->set_date_expiration(date('Y-m-d H:i:s', strtotime('-5 days')));
|
||||
$remaining_days = $this->membership->get_remaining_days_in_cycle();
|
||||
$this->assertEquals(
|
||||
0,
|
||||
|
Reference in New Issue
Block a user