Skip to content

Commit 63abe7b

Browse files
committed
Added new tests for 2 new added Cart API
1 parent 4ccb1c2 commit 63abe7b

File tree

3 files changed

+106
-9
lines changed

3 files changed

+106
-9
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,25 +305,35 @@ public function getCondition($conditionName)
305305
return $this->getConditions()->get($conditionName);
306306
}
307307

308-
/**
309-
* Get all the condition filtered by Type
310-
* @param $type
311-
* @return CartConditionCollection
312-
*/
313-
public function getConditionsByType($type){
314-
return $this->getConditions()->filter(function(CartCondition $condition) use ($type){
308+
/**
309+
* Get all the condition filtered by Type
310+
* Please Note that this will only return condition added on cart bases, not those conditions added
311+
* specifically on an per item bases
312+
*
313+
* @param $type
314+
* @return CartConditionCollection
315+
*/
316+
public function getConditionsByType($type)
317+
{
318+
return $this->getConditions()->filter(function(CartCondition $condition) use ($type)
319+
{
315320
return $condition->getType() == $type;
316321
});
317322
}
318323

319324

320325
/**
321326
* Remove all the condition with the $type specified
327+
* Please Note that this will only remove condition added on cart bases, not those conditions added
328+
* specifically on an per item bases
329+
*
322330
* @param $type
323331
* @return $this
324332
*/
325-
public function removeConditionsByType($type){
326-
return $this->getConditionsByType($type)->each(function($condition){
333+
public function removeConditionsByType($type)
334+
{
335+
$this->getConditionsByType($type)->each(function($condition)
336+
{
327337
$this->removeCartCondition($condition->getName());
328338
});
329339
}

tests/CartConditionsTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,92 @@ public function test_get_calculated_value_of_a_condition()
521521
$this->assertEquals(25, $conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal),'First condition calculated value must be 5');
522522
}
523523

524+
public function test_get_conditions_by_type()
525+
{
526+
$cartCondition1 = new CartCondition(array(
527+
'name' => 'SALE 5%',
528+
'type' => 'sale',
529+
'target' => 'subtotal',
530+
'value' => '-5%',
531+
));
532+
$cartCondition2 = new CartCondition(array(
533+
'name' => 'Item Gift Pack 25.00',
534+
'type' => 'promo',
535+
'target' => 'subtotal',
536+
'value' => '-25',
537+
));
538+
$cartCondition3 = new CartCondition(array(
539+
'name' => 'Item Less 8%',
540+
'type' => 'promo',
541+
'target' => 'subtotal',
542+
'value' => '-8%',
543+
));
544+
545+
$item = array(
546+
'id' => 456,
547+
'name' => 'Sample Item 1',
548+
'price' => 100,
549+
'quantity' => 1,
550+
'attributes' => array(),
551+
);
552+
553+
$this->cart->add($item);
554+
555+
$this->cart->condition([$cartCondition1, $cartCondition2, $cartCondition3]);
556+
557+
// now lets get all conditions added in the cart with the type "promo"
558+
$promoConditions = $this->cart->getConditionsByType('promo');
559+
560+
$this->assertEquals(2, $promoConditions->count(), "We should have 2 items as promo condition type.");
561+
}
562+
563+
public function test_remove_conditions_by_type()
564+
{
565+
// NOTE:
566+
// when add a new condition, the condition's name will be the key to be use
567+
// to access the condition. For some reasons, if the condition name contains
568+
// a "dot" on it ("."), for example adding a condition with name "SALE 35.00"
569+
// this will cause issues when removing this condition by name, this will not be removed
570+
// so when adding a condition, the condition name should not contain any "period" (.)
571+
// to avoid any issues removing it using remove method: removeCartCondition($conditionName);
572+
573+
$cartCondition1 = new CartCondition(array(
574+
'name' => 'SALE 5%',
575+
'type' => 'sale',
576+
'target' => 'subtotal',
577+
'value' => '-5%',
578+
));
579+
$cartCondition2 = new CartCondition(array(
580+
'name' => 'Item Gift Pack 20',
581+
'type' => 'promo',
582+
'target' => 'subtotal',
583+
'value' => '-25',
584+
));
585+
$cartCondition3 = new CartCondition(array(
586+
'name' => 'Item Less 8%',
587+
'type' => 'promo',
588+
'target' => 'subtotal',
589+
'value' => '-8%',
590+
));
591+
592+
$item = array(
593+
'id' => 456,
594+
'name' => 'Sample Item 1',
595+
'price' => 100,
596+
'quantity' => 1,
597+
'attributes' => array(),
598+
);
599+
600+
$this->cart->add($item);
601+
602+
$this->cart->condition([$cartCondition1, $cartCondition2, $cartCondition3]);
603+
604+
// now lets remove all conditions added in the cart with the type "promo"
605+
$this->cart->removeConditionsByType('promo');
606+
607+
$this->assertEquals(1, $this->cart->getConditions()->count(), "We should have 1 condition remaining as promo conditions type has been removed.");
608+
}
609+
524610
protected function fillCart()
525611
{
526612
$items = array(

tests/CartTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,5 @@ public function test_clearing_cart()
399399

400400
$this->assertTrue($this->cart->isEmpty(),'cart should now be empty');
401401
}
402+
402403
}

0 commit comments

Comments
 (0)