From 4369ebe4eb1302e584480ea1742f6c381e094bc5 Mon Sep 17 00:00:00 2001 From: darrylcoder Date: Wed, 18 Mar 2015 18:23:37 -0700 Subject: [PATCH] added two new methods --- src/Darryldecode/Cart/Cart.php | 4 +- src/Darryldecode/Cart/CartCondition.php | 46 ++++++++++++++----- src/Darryldecode/Cart/ItemCollection.php | 10 +++++ tests/CartConditionsTest.php | 43 ++++++++++++++++++ tests/ItemTest.php | 57 ++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 tests/ItemTest.php diff --git a/src/Darryldecode/Cart/Cart.php b/src/Darryldecode/Cart/Cart.php index 94ff782..f54e44f 100644 --- a/src/Darryldecode/Cart/Cart.php +++ b/src/Darryldecode/Cart/Cart.php @@ -82,9 +82,7 @@ public function getInstanceName() */ public function get($itemId) { - $contents = $this->getContent(); - - return $contents->get($itemId); + return $this->getContent()->get($itemId); } /** diff --git a/src/Darryldecode/Cart/CartCondition.php b/src/Darryldecode/Cart/CartCondition.php index 9442ffb..529f098 100644 --- a/src/Darryldecode/Cart/CartCondition.php +++ b/src/Darryldecode/Cart/CartCondition.php @@ -17,6 +17,13 @@ class CartCondition { */ private $args; + /** + * the parsed raw value of the condition + * + * @var + */ + private $parsedRawValue; + /** * @param array $args (name, type, target, value) * @throws InvalidConditionException @@ -86,6 +93,19 @@ public function applyCondition($totalOrSubTotalOrPrice) return $this->apply($totalOrSubTotalOrPrice, $this->getValue()); } + /** + * get the calculated value of this condition supplied by the subtotal|price + * + * @param $totalOrSubTotalOrPrice + * @return mixed + */ + public function getCalculatedValue($totalOrSubTotalOrPrice) + { + $this->apply($totalOrSubTotalOrPrice, $this->getValue()); + + return $this->parsedRawValue; + } + /** * apply condition * @@ -106,25 +126,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); - $valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100); + $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); - return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted); + $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else if ( $this->valueIsToBeAdded($conditionValue) ) { $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); - $valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100); + $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); - return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted); + $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $value = Helpers::normalizePrice($conditionValue); - $valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100); + $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); - return floatval($totalOrSubTotalOrPrice + $valueToBeSubtracted); + $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } @@ -134,23 +154,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { if( $this->valueIsToBeSubtracted($conditionValue) ) { - $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); + $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); - return floatval($totalOrSubTotalOrPrice - $value); + $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else if ( $this->valueIsToBeAdded($conditionValue) ) { - $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); + $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); - return floatval($totalOrSubTotalOrPrice + $value); + $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { - $value = Helpers::normalizePrice($conditionValue); + $this->parsedRawValue = Helpers::normalizePrice($conditionValue); - return floatval($totalOrSubTotalOrPrice + $value); + $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } + + return $result; } /** diff --git a/src/Darryldecode/Cart/ItemCollection.php b/src/Darryldecode/Cart/ItemCollection.php index 09c2309..83216a6 100644 --- a/src/Darryldecode/Cart/ItemCollection.php +++ b/src/Darryldecode/Cart/ItemCollection.php @@ -11,6 +11,16 @@ class ItemCollection extends Collection { + /** + * get the sum of price + * + * @return mixed|null + */ + public function getPriceSum() + { + return $this->price * $this->quantity; + } + public function __get($name) { if( $this->has($name) ) return $this->get($name); diff --git a/tests/CartConditionsTest.php b/tests/CartConditionsTest.php index 5ad8b26..68b8ddf 100644 --- a/tests/CartConditionsTest.php +++ b/tests/CartConditionsTest.php @@ -478,6 +478,49 @@ public function test_clear_cart_conditions() $this->assertEquals(0, $this->cart->getConditions()->count(), 'Cart should have no conditions now'); } + public function test_get_calculated_value_of_a_condition() + { + $cartCondition1 = new CartCondition(array( + 'name' => 'SALE 5%', + 'type' => 'sale', + 'target' => 'subtotal', + 'value' => '-5%', + )); + $cartCondition2 = new CartCondition(array( + 'name' => 'Item Gift Pack 25.00', + 'type' => 'promo', + 'target' => 'subtotal', + 'value' => '-25', + )); + + $item = array( + 'id' => 456, + 'name' => 'Sample Item 1', + 'price' => 100, + 'quantity' => 1, + 'attributes' => array(), + ); + + $this->cart->add($item); + + $this->cart->condition([$cartCondition1, $cartCondition2]); + + $subTotal = $this->cart->getSubTotal(); + + $this->assertEquals(100, $subTotal, 'Subtotal should be 100'); + + // way 1 + // now we will get the calculated value of the condition 1 + $cond1 = $this->cart->getCondition('SALE 5%'); + $this->assertEquals(5,$cond1->getCalculatedValue($subTotal), 'The calculated value must be 5'); + + // way 2 + // get all cart conditions and get their calculated values + $conditions = $this->cart->getConditions(); + $this->assertEquals(5, $conditions['SALE 5%']->getCalculatedValue($subTotal),'First condition calculated value must be 5'); + $this->assertEquals(25, $conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal),'First condition calculated value must be 5'); + } + protected function fillCart() { $items = array( diff --git a/tests/ItemTest.php b/tests/ItemTest.php new file mode 100644 index 0000000..51d9e66 --- /dev/null +++ b/tests/ItemTest.php @@ -0,0 +1,57 @@ +shouldReceive('fire'); + + $this->cart = new Cart( + new SessionMock(), + $events, + 'shopping', + 'SAMPLESESSIONKEY' + ); + } + + public function tearDown() + { + m::close(); + } + + public function test_item_get_sum_price_using_property() + { + $this->cart->add(455, 'Sample Item', 100.99, 2, array()); + + $item = $this->cart->get(455); + + $this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98'); + } + + public function test_item_get_sum_price_using_array_style() + { + $this->cart->add(455, 'Sample Item', 100.99, 2, array()); + + $item = $this->cart->get(455); + + $this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98'); + } +} \ No newline at end of file