From 30d789f0c09ffb671cafe295eef5dc1935902c6f Mon Sep 17 00:00:00 2001 From: Rob Gloudemans Date: Sat, 1 Mar 2014 19:52:00 +0100 Subject: [PATCH] Remove item when qty is a negative number + test, also added test for multiple options --- src/Gloudemans/Shoppingcart/Cart.php | 1 + tests/CartTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index 53da40c..bcc6409 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -427,6 +427,7 @@ class Cart { */ protected function updateQty($rowId, $qty) { + if($qty <= 0) if($qty == 0) { return $this->remove($rowId); diff --git a/tests/CartTest.php b/tests/CartTest.php index 1235522..4c2c7ea 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -50,6 +50,19 @@ class CartTest extends PHPUnit_Framework_TestCase { )); } + public function testCartCanAddMultipleOptions() + { + $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + + $this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large', 'color' => 'red')); + + $cartRow = $this->cart->get('c5417b5761c7fb837e4227a38870dd4d'); + + $this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', $cartRow->options); + $this->assertEquals('large', $cartRow->options->size); + $this->assertEquals('red', $cartRow->options->color); + } + /** * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException */ @@ -154,6 +167,18 @@ class CartTest extends PHPUnit_Framework_TestCase { $this->assertTrue($this->cart->content()->isEmpty()); } + public function testCartCanRemoveOnNegativeUpdate() + { + $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string')); + + $this->cart->add('293ad', 'Product 1', 1, 9.99); + $this->cart->update('8cbf215baa3b757e910e5305ab981172', -1); + + $this->assertTrue($this->cart->content()->isEmpty()); + } + public function testCartCanGet() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));