/** * @param CartContainer $cart * * @return $this */ public function apply(CartContainer $cart) { if ($this instanceof PercentPromotionalRuleInterface) { $total = $cart->getTotal(); $cart->setTotal($total - $total / 100 * $this->getValue()); } if ($this instanceof ProductPricePromotionalRuleInterface) { $item = $cart->get($this->getItem()->getCode()); $item->setPrice($this->getAmount()); $cart->calculateTotal(); } return $this; }
public function testAdd() { $expected = array(); $this->cartContainer->add($this->getItem001()); $expected[$this->getItem001()->getCode()] = $this->getItem001(); $this->assertSame($expected, $this->cartContainer->getAll()); $this->cartContainer->add($this->getItem002()); $expected[$this->getItem002()->getCode()] = $this->getItem002(); $this->assertSame($expected, $this->cartContainer->getAll()); $this->cartContainer->add($this->getItem002()); $expected[$this->getItem002()->getCode()]->increaseQuantity(); $this->assertSame($expected, $this->cartContainer->getAll()); $this->cartContainer->calculateTotal(); $total = 0; /** @var Item $item */ foreach ($expected as $item) { $total += $item->getPrice() * $item->getQuantity(); } $this->assertEquals($total, $this->cartContainer->getTotal()); $expectedTotal = 42.42; $this->cartContainer->setTotal($expectedTotal); $this->assertEquals($expectedTotal, $this->cartContainer->getTotal()); $this->cartContainer->applyPromotionalRules(); }