fix cart operator to sum up items that are equal.

This commit is contained in:
Paweł Jędrzejewski 2012-03-29 00:00:07 +02:00
parent 5d898acc05
commit b5222a514d
2 changed files with 16 additions and 1 deletions

View file

@ -46,7 +46,7 @@ abstract class CartOperator implements CartOperatorInterface
*/ */
public function addItem(CartInterface $cart, ItemInterface $item) public function addItem(CartInterface $cart, ItemInterface $item)
{ {
if (0 > $cart->countItems()) { if (false === $cart->isEmpty()) {
foreach ($cart->getItems() as $existingItem) { foreach ($cart->getItems() as $existingItem) {
if ($existingItem->equals($item)) { if ($existingItem->equals($item)) {
$existingItem->setQuantity($existingItem->getQuantity() + $item->getQuantity()); $existingItem->setQuantity($existingItem->getQuantity() + $item->getQuantity());

View file

@ -32,6 +32,21 @@ class CartOperatorTest extends \PHPUnit_Framework_TestCase
$cartOperator->addItem($cart, $item); $cartOperator->addItem($cart, $item);
} }
public function testAddItemIncreasesQuantityIfThereAreItemsThatAreEqual()
{
$item = $this->getItem();
$item->setQuantity(3);
$cart = $this->getCart();
$cartOperator = $this->getCartOperator();
$cartOperator->addItem($cart, $item);
$cartOperator->addItem($cart, $item);
$this->assertEquals(1, $cart->countItems());
$this->assertEquals(6, $item->getQuantity());
}
public function testRefreshSetsTotalItems() public function testRefreshSetsTotalItems()
{ {
$cart = $this->getMockCart(); $cart = $this->getMockCart();