Add unit tests to compare cloned cart objects to original (#17560)

* Add unit tests to compare cloned cart objects to original

* Don't use reflection for fees test

* Skip test using ReflectionProperty::setAccessible() below PHP 5.3
This commit is contained in:
Jeremy Pry 2017-11-10 07:03:14 -05:00 committed by Mike Jolley
parent 78466a6665
commit a4d1c829eb
1 changed files with 44 additions and 0 deletions

View File

@ -1061,4 +1061,48 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
WC_Helper_Coupon::delete_coupon( $iu_coupon1->get_code() );
WC_Helper_Coupon::delete_coupon( $iu_coupon2->get_code() );
}
public function test_clone_cart() {
$cart = wc()->cart;
$new_cart = clone $cart;
$is_identical_cart = $cart === $new_cart;
// Cloned carts should not be identical.
$this->assertFalse( $is_identical_cart, 'Cloned cart not identical to original cart' );
}
public function test_cloned_cart_session() {
// PHP 5.2 does not include support for ReflectionProperty::setAccessible().
if ( version_compare( '5.3', PHP_VERSION, '>' ) ) {
$this->markTestSkipped( 'Test requires PHP 5.3 and above to use ReflectionProperty::setAccessible()' );
}
$cart = wc()->cart;
$new_cart = clone $cart;
// Allow accessing protected properties.
$reflected_cart = new ReflectionClass( $cart );
$cart_session = $reflected_cart->getProperty( 'session' );
$cart_session->setAccessible( true );
$reflected_new_cart = new ReflectionClass( $new_cart );
$new_cart_session = $reflected_new_cart->getProperty( 'session' );
$new_cart_session->setAccessible( true );
// Ensure that cloned properties are not identical.
$identical_sessions = $cart_session->getValue( $cart ) === $new_cart_session->getValue( $new_cart );
$this->assertFalse( $identical_sessions, 'Cloned cart sessions should not be identical to original cart' );
}
public function test_cloned_cart_fees() {
$cart = wc()->cart;
$new_cart = clone $cart;
// Get the properties from each object.
$cart_fees = $cart->fees_api();
$new_cart_fees = $new_cart->fees_api();
// Ensure that cloned properties are not identical.
$identical_fees = $cart_fees === $new_cart_fees;
$this->assertFalse( $identical_fees, 'Cloned cart fees should not be identical to original cart.' );
}
}