From ea2eec4f6890faba8b42dce965cdfa76bb712e54 Mon Sep 17 00:00:00 2001 From: Rob Gloudemans Date: Fri, 22 Aug 2014 10:55:56 +0200 Subject: [PATCH] Added events, updated tests, some docblock updates and fixed bug on empty search param --- src/Gloudemans/Shoppingcart/Cart.php | 151 ++++++++++++++++----------- tests/CartTest.php | 45 ++++++++ 2 files changed, 136 insertions(+), 60 deletions(-) diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index 9ce1c2d..37da071 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -7,14 +7,14 @@ class Cart { /** * Session class instance * - * @var Session + * @var Illuminate\Session\SessionManager */ protected $session; /** * Event class instance * - * @var Event + * @var Illuminate\Events\Dispatcher */ protected $event; @@ -42,8 +42,8 @@ class Cart { /** * Constructor * - * @param Session $session Session class instance - * @param Event $event Event class instance + * @param Illuminate\Session\SessionManager $session Session class instance + * @param Illuminate\Events\Dispatcher $event Event class instance */ public function __construct($session, $event) { @@ -56,8 +56,8 @@ class Cart { /** * Set the current cart instance * - * @param string $instance Cart instance name - * @return Cart + * @param string $instance Cart instance name + * @return Gloudemans\Shoppingcart\Cart */ public function instance($instance = null) { @@ -90,13 +90,13 @@ class Cart { /** * Add a row to the cart * - * @param string|Array $id Unique ID of the item|Item formated as array|Array of items - * @param string $name Name of the item - * @param int $qty Item qty to add to the cart - * @param float $price Price of one item - * @param Array $options Array of additional options, such as 'size' or 'color' + * @param string|array $id Unique ID of the item|Item formated as array|Array of items + * @param string $name Name of the item + * @param int $qty Item qty to add to the cart + * @param float $price Price of one item + * @param array $options Array of additional options, such as 'size' or 'color' */ - public function add($id, $name = null, $qty = null, $price = null, Array $options = array()) + public function add($id, $name = null, $qty = null, $price = null, array $options = array()) { // If the first parameter is an array we need to call the add() function again if(is_array($id)) @@ -110,32 +110,45 @@ class Cart { foreach($id as $item) { - $options = isset($item['options']) ? $item['options'] : array(); + $options = array_get($item, 'options', array()); $this->addRow($item['id'], $item['name'], $item['qty'], $item['price'], $options); } + // Fire the cart.batched event + $this->event->fire('cart.batched', $id); + return; } - $options = isset($id['options']) ? $id['options'] : array(); + $options = array_get($id, 'options', array()); // Fire the cart.add event $this->event->fire('cart.add', array_merge($id, array('options' => $options))); - return $this->addRow($id['id'], $id['name'], $id['qty'], $id['price'], $options); + $result = $this->addRow($id['id'], $id['name'], $id['qty'], $id['price'], $options); + + // Fire the cart.added event + $this->event->fire('cart.added', array_merge($id, array('options' => $options))); + + return $result; } // Fire the cart.add event $this->event->fire('cart.add', compact('id', 'name', 'qty', 'price', 'options')); - return $this->addRow($id, $name, $qty, $price, $options); + $result = $this->addRow($id, $name, $qty, $price, $options); + + // Fire the cart.added event + $this->event->fire('cart.added', compact('id', 'name', 'qty', 'price', 'options')); + + return $result; } /** * Update the quantity of one row of the cart * - * @param string $rowId The rowid of the item you want to update - * @param integer|Array $attribute New quantity of the item|Array of attributes to update + * @param string $rowId The rowid of the item you want to update + * @param integer|array $attribute New quantity of the item|Array of attributes to update * @return boolean */ public function update($rowId, $attribute) @@ -147,19 +160,29 @@ class Cart { // Fire the cart.update event $this->event->fire('cart.update', $rowId); - return $this->updateAttribute($rowId, $attribute); + $result = $this->updateAttribute($rowId, $attribute); + + // Fire the cart.updated event + $this->event->fire('cart.updated', $rowId); + + return $result; } // Fire the cart.update event $this->event->fire('cart.update', $rowId); - return $this->updateQty($rowId, $attribute); + $result = $this->updateQty($rowId, $attribute); + + // Fire the cart.updated event + $this->event->fire('cart.updated', $rowId); + + return $result; } /** * Remove a row from the cart * - * @param string $rowId The rowid of the item + * @param string $rowId The rowid of the item * @return boolean */ public function remove($rowId) @@ -173,14 +196,17 @@ class Cart { $cart->forget($rowId); + // Fire the cart.removed event + $this->event->fire('cart.removed', $rowId); + return $this->updateCart($cart); } /** * Get a row of the cart by its ID * - * @param string $rowId The ID of the row to fetch - * @return CartCollection + * @param string $rowId The ID of the row to fetch + * @return Gloudemans\Shoppingcart\CartCollection */ public function get($rowId) { @@ -192,7 +218,7 @@ class Cart { /** * Get the cart content * - * @return CartRowCollection + * @return Gloudemans\Shoppingcart\CartRowCollection */ public function content() { @@ -211,7 +237,12 @@ class Cart { // Fire the cart.destroy event $this->event->fire('cart.destroy'); - return $this->updateCart(NULL); + $result = $this->updateCart(NULL); + + // Fire the cart.destroyed event + $this->event->fire('cart.destroyed'); + + return $result; } /** @@ -240,7 +271,7 @@ class Cart { /** * Get the number of items in the cart * - * @param boolean $totalItems Get all the items (when false, will return the number of rows) + * @param boolean $totalItems Get all the items (when false, will return the number of rows) * @return int */ public function count($totalItems = true) @@ -265,11 +296,13 @@ class Cart { /** * Search if the cart has a item * - * @param Array $search An array with the item ID and optional options - * @return Array|boolean + * @param array $search An array with the item ID and optional options + * @return array|boolean */ - public function search(Array $search) + public function search(array $search) { + if(empty($search)) return false; + foreach($this->getContent() as $item) { $found = $item->search($search); @@ -286,13 +319,13 @@ class Cart { /** * Add row to the cart * - * @param string $id Unique ID of the item - * @param string $name Name of the item - * @param int $qty Item qty to add to the cart - * @param float $price Price of one item - * @param Array $options Array of additional options, such as 'size' or 'color' + * @param string $id Unique ID of the item + * @param string $name Name of the item + * @param int $qty Item qty to add to the cart + * @param float $price Price of one item + * @param array $options Array of additional options, such as 'size' or 'color' */ - protected function addRow($id, $name, $qty, $price, Array $options = array()) + protected function addRow($id, $name, $qty, $price, array $options = array()) { if(empty($id) || empty($name) || empty($qty) || ! isset($price)) { @@ -329,8 +362,8 @@ class Cart { /** * Generate a unique id for the new row * - * @param string $id Unique ID of the item - * @param Array $options Array of additional options, such as 'size' or 'color' + * @param string $id Unique ID of the item + * @param array $options Array of additional options, such as 'size' or 'color' * @return boolean */ protected function generateRowId($id, $options) @@ -354,7 +387,7 @@ class Cart { /** * Update the cart * - * @param CartCollection $cart The new cart content + * @param Gloudemans\Shoppingcart\CartCollection $cart The new cart content * @return void */ protected function updateCart($cart) @@ -365,7 +398,7 @@ class Cart { /** * Get the carts content, if there is no cart content set yet, return a new empty Collection * - * @return Illuminate\Support\Collection + * @return Gloudemans\Shoppingcart\CartCollection */ protected function getContent() { @@ -387,9 +420,9 @@ class Cart { /** * Update a row if the rowId already exists * - * @param string $rowId The ID of the row to update - * @param integer $qty The quantity to add to the row - * @return Collection + * @param string $rowId The ID of the row to update + * @param integer $qty The quantity to add to the row + * @return Gloudemans\Shoppingcart\CartCollection */ protected function updateRow($rowId, $attributes) { @@ -423,13 +456,13 @@ class Cart { /** * Create a new row Object * - * @param string $rowId The ID of the new row - * @param string $id Unique ID of the item - * @param string $name Name of the item - * @param int $qty Item qty to add to the cart - * @param float $price Price of one item - * @param Array $options Array of additional options, such as 'size' or 'color' - * @return Collection + * @param string $rowId The ID of the new row + * @param string $id Unique ID of the item + * @param string $name Name of the item + * @param int $qty Item qty to add to the cart + * @param float $price Price of one item + * @param array $options Array of additional options, such as 'size' or 'color' + * @return Gloudemans\Shoppingcart\CartCollection */ protected function createRow($rowId, $id, $name, $qty, $price, $options) { @@ -453,9 +486,9 @@ class Cart { /** * Update the quantity of a row * - * @param string $rowId The ID of the row - * @param int $qty The qty to add - * @return CartCollection + * @param string $rowId The ID of the row + * @param int $qty The qty to add + * @return Gloudemans\Shoppingcart\CartCollection */ protected function updateQty($rowId, $qty) { @@ -470,9 +503,9 @@ class Cart { /** * Update an attribute of the row * - * @param string $rowId The ID of the row - * @param Array $attributes An array of attributes to update - * @return CartCollection + * @param string $rowId The ID of the row + * @param array $attributes An array of attributes to update + * @return Gloudemans\Shoppingcart\CartCollection */ protected function updateAttribute($rowId, $attributes) { @@ -482,14 +515,12 @@ class Cart { /** * Check if the array is a multidimensional array * - * @param Array $array The array to check + * @param array $array The array to check * @return boolean */ - protected function is_multi(Array $array) + protected function is_multi(array $array) { - $first = array_shift($array); - - return is_array($first); + return is_array(head($array)); } } diff --git a/tests/CartTest.php b/tests/CartTest.php index 870bd93..8d7079b 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -31,6 +31,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanAdd() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large')); } @@ -38,6 +39,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanAddArray() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large'))); } @@ -45,6 +47,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanAddBatch() { $this->events->shouldReceive('fire')->once()->with('cart.batch', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.batched', m::type('array')); $this->cart->add(array( array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00), @@ -55,6 +58,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanAddMultipleOptions() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large', 'color' => 'red')); @@ -98,6 +102,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanUpdateExistingItem() { $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -108,7 +113,9 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanUpdateQty() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.updated', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->update('8cbf215baa3b757e910e5305ab981172', 2); @@ -119,7 +126,9 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanUpdateItem() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.updated', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->update('8cbf215baa3b757e910e5305ab981172', array('name' => 'Product 2')); @@ -130,7 +139,9 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanUpdateOptions() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.updated', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'S')); $this->cart->update('9be7e69d236ca2d09d2e0838d2c59aeb', array('options' => array('size' => 'L'))); @@ -149,7 +160,9 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanRemove() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.removed', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->remove('8cbf215baa3b757e910e5305ab981172'); @@ -160,8 +173,11 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanRemoveOnUpdate() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.updated', m::type('string')); $this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.removed', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->update('8cbf215baa3b757e910e5305ab981172', 0); @@ -172,8 +188,11 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanRemoveOnNegativeUpdate() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.updated', m::type('string')); $this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string')); + $this->events->shouldReceive('fire')->once()->with('cart.removed', m::type('string')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->update('8cbf215baa3b757e910e5305ab981172', -1); @@ -184,6 +203,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanGet() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $item = $this->cart->get('8cbf215baa3b757e910e5305ab981172'); @@ -194,6 +214,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanGetContent() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -204,7 +225,9 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanDestroy() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->events->shouldReceive('fire')->once()->with('cart.destroy'); + $this->events->shouldReceive('fire')->once()->with('cart.destroyed'); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->destroy(); @@ -216,6 +239,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanGetTotal() { $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->add('986se', 'Product 2', 1, 19.99); @@ -226,6 +250,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanGetItemCount() { $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->add('986se', 'Product 2', 2, 19.99); @@ -236,6 +261,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanGetRowCount() { $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); $this->cart->add('986se', 'Product 2', 2, 19.99); @@ -246,6 +272,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanSearch() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -256,6 +283,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanHaveMultipleInstances() { $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); $this->cart->instance('firstInstance')->add('293ad', 'Product 1', 1, 9.99); $this->cart->instance('secondInstance')->add('986se', 'Product 2', 1, 19.99); @@ -266,6 +294,18 @@ class CartTest extends PHPUnit_Framework_TestCase { $this->assertFalse($this->cart->instance('secondInstance')->content()->has('8cbf215baa3b757e910e5305ab981172')); } + public function testCartCanSearchInMultipleInstances() + { + $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->twice()->with('cart.added', m::type('array')); + + $this->cart->instance('firstInstance')->add('293ad', 'Product 1', 1, 9.99); + $this->cart->instance('secondInstance')->add('986se', 'Product 2', 1, 19.99); + + $this->assertEquals($this->cart->instance('firstInstance')->search(array('id' => '293ad')), ['8cbf215baa3b757e910e5305ab981172']); + $this->assertEquals($this->cart->instance('secondInstance')->search(array('id' => '986se')), ['22eae2b9c10083d6631aaa023106871a']); + } + /** * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException */ @@ -277,6 +317,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartReturnsCartCollection() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -286,6 +327,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCollectionHasCartRowCollection() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -295,6 +337,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartRowCollectionHasCartRowOptionsCollection() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->add('293ad', 'Product 1', 1, 9.99); @@ -319,6 +362,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanReturnModelProperties() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->associate('TestProduct')->add('293ad', 'Product 1', 1, 9.99); @@ -328,6 +372,7 @@ class CartTest extends PHPUnit_Framework_TestCase { public function testCartCanReturnNamespadedModelProperties() { $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array')); + $this->events->shouldReceive('fire')->once()->with('cart.added', m::type('array')); $this->cart->associate('TestProduct', 'Acme\Test\Models')->add('293ad', 'Product 1', 1, 9.99);