From 628244bdcf6ac784267708482dfa6c9587be98fa Mon Sep 17 00:00:00 2001 From: Rob Gloudemans Date: Sat, 27 Jul 2013 17:46:57 +0200 Subject: [PATCH] Added a few exceptions that will be thrown at the time of errors. Makes for easier debugging. Also updated README accordingly --- README.md | 14 ++++++- src/Gloudemans/Shoppingcart/Cart.php | 34 ++++++++++++++- .../ShoppingcartInstanceException.php | 3 ++ .../ShoppingcartInvalidItemException.php | 3 ++ .../ShoppingcartInvalidPriceException.php | 3 ++ .../ShoppingcartInvalidQtyException.php | 3 ++ .../ShoppingcartInvalidRowIDException.php | 3 ++ tests/CartTest.php | 42 +++++++++++++++++++ 8 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInstanceException.php create mode 100644 src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInvalidItemException.php create mode 100644 src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInvalidPriceException.php create mode 100644 src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInvalidQtyException.php create mode 100644 src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInvalidRowIDException.php diff --git a/README.md b/README.md index 2b86aee..3d71ff1 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Now all you have to do is add the service provider of the package and alias the Add a new line to the `service providers` array: - '\Gloudemans\Shoppingcart\ShoppingcartServiceProvider' + 'Gloudemans\Shoppingcart\ShoppingcartServiceProvider' And finally add a new line to the `aliases` array: @@ -221,6 +221,18 @@ N.B. Keep in mind that the cart stays in the last set instance for as long as yo N.B.2 The default cart instance is called `main`, so when you're not using instances,`Cart::content();` is the same as `Cart::instance('main')->content()`. +## Exceptions +The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions: + +| Exception | Reason | +| ------------------------------------- | ------------------------------------------------------------------------ | +| *ShoppingcartInstanceException* | When no instance is passed to the instance() method | +| *ShoppingcartInvalidItemException* | When a new product misses one of it's arguments (id, name, qty, price) | +| *ShoppingcartInvalidPriceException* | When a not numeric price is passed | +| *ShoppingcartInvalidQtyException* | When a not numeric quantity is passed | +| *ShoppingcartInvalidRowIDException* | When the rowId that got passed doesn't exists in the current cart | + + ## Example Below is a little example of how to list the cart content in a table: diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index 82c564d..ae798bc 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -36,8 +36,10 @@ class Cart { * @param string $instance Cart instance name * @return Cart */ - public function instance($instance) + public function instance($instance = null) { + if(empty($instance)) throw new Exceptions\ShoppingcartInstanceException; + $this->instance = $instance; // Return self so the method is chainable @@ -99,6 +101,8 @@ class Cart { */ public function update($rowId, $attribute) { + if( ! $this->hasRowId($rowId)) throw new Exceptions\ShoppingcartInvalidRowIDException; + if(is_array($attribute)) { return $this->updateAttribute($rowId, $attribute); @@ -115,6 +119,8 @@ class Cart { */ public function remove($rowId) { + if( ! $this->hasRowId($rowId)) throw new Exceptions\ShoppingcartInvalidRowIDException; + $cart = $this->getContent(); $cart->forget($rowId); @@ -237,6 +243,21 @@ class Cart { */ protected function addRow($id, $name, $qty, $price, Array $options = array()) { + if(empty($id) || empty($name) || empty($qty) || empty($price)) + { + throw new Exceptions\ShoppingcartInvalidItemException; + } + + if( ! is_numeric($qty)) + { + throw new Exceptions\ShoppingcartInvalidQtyException; + } + + if( ! is_numeric($price)) + { + throw new Exceptions\ShoppingcartInvalidPriceException; + } + $cart = $this->getContent(); $rowId = $this->generateRowId($id, $options); @@ -268,6 +289,17 @@ class Cart { return md5($id . serialize($options)); } + /** + * Check if a rowid exists in the current cart instance + * + * @param string $id Unique ID of the item + * @return boolean + */ + protected function hasRowId($rowId) + { + return $this->getContent()->has($rowId); + } + /** * Update the cart * diff --git a/src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInstanceException.php b/src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInstanceException.php new file mode 100644 index 0000000..17e2bf0 --- /dev/null +++ b/src/Gloudemans/Shoppingcart/Exceptions/ShoppingcartInstanceException.php @@ -0,0 +1,3 @@ +assertEquals($search, array($row1)); } + /** + * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException + */ + public function testShoppingcartInstanceException() + { + Cart::instance(); + } + + /** + * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException + */ + public function testShoppingcartInvalidItemException() + { + Cart::add(1); + } + + /** + * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidQtyException + */ + public function testShoppingcartInvalidQtyException() + { + Cart::add(1, 'Product 1', 'nonnumeric', 10.00); + } + + /** + * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidPriceException + */ + public function testShoppingcartInvalidPriceException() + { + Cart::add(1, 'Product 1', 1, 'nonnumeric'); + } + + /** + * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException + */ + public function testShoppingcartInvalidRowIDException() + { + Cart::add(1, 'Product 1', 1, 10.00); + + Cart::update('nonexistingrowid', 2); + } + } \ No newline at end of file