Skip to content

Commit 7d25d94

Browse files
committed
added attributes to a condition
1 parent 5113955 commit 7d25d94

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/Darryldecode/Cart/CartCondition.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public function getType()
7272
return $this->args['type'];
7373
}
7474

75+
/**
76+
* get the additional attributes of a condition
77+
*
78+
* @return array
79+
*/
80+
public function getAttributes()
81+
{
82+
return (isset($this->args['attributes'])) ? $this->args['attributes'] : array();
83+
}
84+
7585
/**
7686
* the value of this the condition
7787
*

tests/CartConditionsTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,79 @@ public function test_remove_conditions_by_type()
674674
$this->assertEquals(1, $this->cart->getConditions()->count(), "We should have 1 condition remaining as promo conditions type has been removed.");
675675
}
676676

677+
public function test_add_cart_condition_without_condition_attributes()
678+
{
679+
$cartCondition1 = new CartCondition(array(
680+
'name' => 'SALE 5%',
681+
'type' => 'sale',
682+
'target' => 'subtotal',
683+
'value' => '-5%'
684+
));
685+
686+
$item = array(
687+
'id' => 456,
688+
'name' => 'Sample Item 1',
689+
'price' => 100,
690+
'quantity' => 1,
691+
'attributes' => array(),
692+
);
693+
694+
$this->cart->add($item);
695+
696+
$this->cart->condition([$cartCondition1]);
697+
698+
// prove first we have now the condition on the cart
699+
$contition = $this->cart->getCondition("SALE 5%");
700+
$this->assertEquals('SALE 5%',$contition->getName());
701+
702+
// when get attribute is called and there is no attributes added,
703+
// it should return an empty array
704+
$conditionAttribute = $contition->getAttributes();
705+
$this->assertInternalType('array', $conditionAttribute);
706+
}
707+
708+
public function test_add_cart_condition_with_condition_attributes()
709+
{
710+
$cartCondition1 = new CartCondition(array(
711+
'name' => 'SALE 5%',
712+
'type' => 'sale',
713+
'target' => 'subtotal',
714+
'value' => '-5%',
715+
'attributes' => array(
716+
'description' => 'october fest promo sale',
717+
'sale_start_date' => '2015-01-20',
718+
'sale_end_date' => '2015-01-30',
719+
)
720+
));
721+
722+
$item = array(
723+
'id' => 456,
724+
'name' => 'Sample Item 1',
725+
'price' => 100,
726+
'quantity' => 1,
727+
'attributes' => array(),
728+
);
729+
730+
$this->cart->add($item);
731+
732+
$this->cart->condition([$cartCondition1]);
733+
734+
// prove first we have now the condition on the cart
735+
$contition = $this->cart->getCondition("SALE 5%");
736+
$this->assertEquals('SALE 5%',$contition->getName());
737+
738+
// when get attribute is called and there is no attributes added,
739+
// it should return an empty array
740+
$conditionAttributes = $contition->getAttributes();
741+
$this->assertInternalType('array', $conditionAttributes);
742+
$this->assertArrayHasKey('description',$conditionAttributes);
743+
$this->assertArrayHasKey('sale_start_date',$conditionAttributes);
744+
$this->assertArrayHasKey('sale_end_date',$conditionAttributes);
745+
$this->assertEquals('october fest promo sale',$conditionAttributes['description']);
746+
$this->assertEquals('2015-01-20',$conditionAttributes['sale_start_date']);
747+
$this->assertEquals('2015-01-30',$conditionAttributes['sale_end_date']);
748+
}
749+
677750
protected function fillCart()
678751
{
679752
$items = array(

0 commit comments

Comments
 (0)