Merge pull request #10 from tkaw220/master

Convert indentation to tabs and change line ending to Unix.
This commit is contained in:
Rob Gloudemans
2013-07-04 13:27:25 -07:00
6 changed files with 534 additions and 534 deletions

View File

@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" <phpunit backupGlobals="false"
backupStaticAttributes="false" backupStaticAttributes="false"
bootstrap="vendor/autoload.php" bootstrap="vendor/autoload.php"
colors="true" colors="true"
convertErrorsToExceptions="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" convertWarningsToExceptions="true"
processIsolation="false" processIsolation="false"
stopOnFailure="false" stopOnFailure="false"
syntaxCheck="false" syntaxCheck="false"
> >
<testsuites> <testsuites>
<testsuite name="Package Test Suite"> <testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory> <directory suffix=".php">./tests/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>

View File

@@ -4,365 +4,365 @@ use Illuminate\Support\Collection;
class Cart { class Cart {
/** /**
* Session class instance * Session class instance
* *
* @var Session * @var Session
*/ */
protected $session; protected $session;
/** /**
* Current cart instance * Current cart instance
* *
* @var string * @var string
*/ */
protected $instance; protected $instance;
/** /**
* Constructor * Constructor
* *
* @param Session $session Session class instance * @param Session $session Session class instance
*/ */
public function __construct($session) public function __construct($session)
{ {
$this->session = $session; $this->session = $session;
$this->instance = 'main'; $this->instance = 'main';
} }
/** /**
* Set the current cart instance * Set the current cart instance
* *
* @param string $instance Cart instance name * @param string $instance Cart instance name
* @return Cart * @return Cart
*/ */
public function instance($instance) public function instance($instance)
{ {
$this->instance = $instance; $this->instance = $instance;
// Return self so the method is chainable // Return self so the method is chainable
return $this; return $this;
} }
/** /**
* Add a row to the cart * Add a row to the cart
* *
* @param string $id Unique ID of the item * @param string $id Unique ID of the item
* @param string $name Name of the item * @param string $name Name of the item
* @param int $qty Item qty to add to the cart * @param int $qty Item qty to add to the cart
* @param float $price Price of one item * @param float $price Price of one item
* @param Array $options Array of additional options, such as 'size' or 'color' * @param Array $options Array of additional options, such as 'size' or 'color'
*/ */
public function add($id, $name, $qty, $price, Array $options = array()) public function add($id, $name, $qty, $price, Array $options = array())
{ {
$cart = $this->getContent(); $cart = $this->getContent();
$rowId = $this->generateRowId($id, $options); $rowId = $this->generateRowId($id, $options);
if($cart->has($rowId)) if($cart->has($rowId))
{ {
$row = $cart->get($rowId); $row = $cart->get($rowId);
$cart = $this->updateRow($rowId, array('qty' => $row->qty + $qty)); $cart = $this->updateRow($rowId, array('qty' => $row->qty + $qty));
} }
else else
{ {
$cart = $this->createRow($rowId, $id, $name, $qty, $price, $options); $cart = $this->createRow($rowId, $id, $name, $qty, $price, $options);
} }
return $this->updateCart($cart); return $this->updateCart($cart);
} }
/** /**
* Add multiple rows to the cart * Add multiple rows to the cart
* *
* @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters * @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters
*/ */
public function addBatch(Array $items) public function addBatch(Array $items)
{ {
foreach($items as $item) foreach($items as $item)
{ {
$options = (isset($item['options'])) ? $item['options'] : array(); $options = (isset($item['options'])) ? $item['options'] : array();
$this->add($item['id'], $item['name'], $item['qty'], $item['price'], $options); $this->add($item['id'], $item['name'], $item['qty'], $item['price'], $options);
} }
return; return;
} }
/** /**
* Update the quantity of one row of the cart * Update the quantity of one row of the cart
* *
* @param string $rowId The rowid of the item you want 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 * @param integer|Array $attribute New quantity of the item|Array of attributes to update
* @return boolean * @return boolean
*/ */
public function update($rowId, $attribute) public function update($rowId, $attribute)
{ {
if(is_array($attribute)) if(is_array($attribute))
{ {
return $this->updateAttribute($rowId, $attribute); return $this->updateAttribute($rowId, $attribute);
} }
return $this->updateQty($rowId, $attribute); return $this->updateQty($rowId, $attribute);
} }
/** /**
* Remove a row from the cart * Remove a row from the cart
* *
* @param string $rowId The rowid of the item * @param string $rowId The rowid of the item
* @return boolean * @return boolean
*/ */
public function remove($rowId) public function remove($rowId)
{ {
$cart = $this->getContent(); $cart = $this->getContent();
$cart->forget($rowId); $cart->forget($rowId);
return $this->updateCart($cart); return $this->updateCart($cart);
} }
/** /**
* Get a row of the cart by its ID * Get a row of the cart by its ID
* *
* @param string $rowId The ID of the row to fetch * @param string $rowId The ID of the row to fetch
* @return CartCollection * @return CartCollection
*/ */
public function get($rowId) public function get($rowId)
{ {
$cart = $this->getContent(); $cart = $this->getContent();
return ($cart->has($rowId)) ? $cart->get($rowId) : NULL; return ($cart->has($rowId)) ? $cart->get($rowId) : NULL;
} }
/** /**
* Get the cart content * Get the cart content
* *
* @return CartRowCollection * @return CartRowCollection
*/ */
public function content() public function content()
{ {
$cart = $this->getContent(); $cart = $this->getContent();
return (empty($cart)) ? NULL : $cart; return (empty($cart)) ? NULL : $cart;
} }
/**
* Empty the cart
*
* @return boolean
*/
public function destroy()
{
return $this->updateCart(NULL);
}
/** /**
* Get the price total * Empty the cart
* *
* @return float * @return boolean
*/ */
public function total() public function destroy()
{ {
$total = 0; return $this->updateCart(NULL);
$cart = $this->getContent(); }
if(empty($cart)) /**
{ * Get the price total
return $total; *
} * @return float
*/
public function total()
{
$total = 0;
$cart = $this->getContent();
foreach($cart AS $row) if(empty($cart))
{ {
$total += $row->subtotal; return $total;
} }
return $total; foreach($cart AS $row)
} {
$total += $row->subtotal;
}
/** return $total;
* Get the number of items in the cart }
*
* @param boolean $totalItems Get all the items (when false, will return the number of rows)
* @return int
*/
public function count($totalItems = true)
{
$cart = $this->getContent();
if( ! $totalItems) /**
{ * Get the number of items in the cart
return $cart->count(); *
} * @param boolean $totalItems Get all the items (when false, will return the number of rows)
* @return int
*/
public function count($totalItems = true)
{
$cart = $this->getContent();
$count = 0; if( ! $totalItems)
{
return $cart->count();
}
foreach($cart AS $row) $count = 0;
{
$count += $row->qty;
}
return $count; foreach($cart AS $row)
} {
$count += $row->qty;
}
/** return $count;
* Search if the cart has a item }
*
* @param Array $search An array with the item ID and optional options
* @return Array|boolean
*/
public function search(Array $search)
{
foreach($this->getContent() as $item)
{
$found = $item->search($search);
if($found) /**
{ * Search if the cart has a item
$rows[] = $item->rowid; *
} * @param Array $search An array with the item ID and optional options
} * @return Array|boolean
*/
public function search(Array $search)
{
foreach($this->getContent() as $item)
{
$found = $item->search($search);
return (empty($rows)) ? false : $rows; if($found)
} {
$rows[] = $item->rowid;
}
}
/** return (empty($rows)) ? false : $rows;
* 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'
* @return boolean
*/
protected function generateRowId($id, $options)
{
return md5($id . serialize(ksort($options)));
}
/** /**
* Update the cart * Generate a unique id for the new row
* *
* @param CartCollection $cart The new cart content * @param string $id Unique ID of the item
* @return void * @param Array $options Array of additional options, such as 'size' or 'color'
*/ * @return boolean
protected function updateCart($cart) */
{ protected function generateRowId($id, $options)
return $this->session->put($this->getInstance(), $cart); {
} return md5($id . serialize(ksort($options)));
}
/** /**
* Get the carts content, if there is no cart content set yet, return a new empty Collection * Update the cart
* *
* @return Illuminate\Support\Collection * @param CartCollection $cart The new cart content
*/ * @return void
protected function getContent() */
{ protected function updateCart($cart)
$content = ($this->session->has($this->getInstance())) ? $this->session->get($this->getInstance()) : new CartCollection; {
return $this->session->put($this->getInstance(), $cart);
}
return $content; /**
} * Get the carts content, if there is no cart content set yet, return a new empty Collection
*
* @return Illuminate\Support\Collection
*/
protected function getContent()
{
$content = ($this->session->has($this->getInstance())) ? $this->session->get($this->getInstance()) : new CartCollection;
/** return $content;
* Get the current cart instance }
*
* @return string
*/
protected function getInstance()
{
return 'cart.' . $this->instance;
}
/** /**
* Update a row if the rowId already exists * Get the current cart instance
* *
* @param string $rowId The ID of the row to update * @return string
* @param integer $qty The quantity to add to the row */
* @return Collection protected function getInstance()
*/ {
protected function updateRow($rowId, $attributes) return 'cart.' . $this->instance;
{ }
$cart = $this->getContent();
$row = $cart->get($rowId); /**
* Update a row if the rowId already exists
foreach($attributes as $key => $value) *
{ * @param string $rowId The ID of the row to update
if($key == 'options') * @param integer $qty The quantity to add to the row
{ * @return Collection
$options = $row->options->merge($value); */
$row->put($key, $options); protected function updateRow($rowId, $attributes)
} {
else $cart = $this->getContent();
{
$row->put($key, $value);
}
}
if( ! is_null(array_keys($attributes, array('qty', 'price')))) $row = $cart->get($rowId);
{
$row->put('subtotal', $row->qty * $row->price);
}
$cart->put($rowId, $row); foreach($attributes as $key => $value)
{
if($key == 'options')
{
$options = $row->options->merge($value);
$row->put($key, $options);
}
else
{
$row->put($key, $value);
}
}
return $cart; if( ! is_null(array_keys($attributes, array('qty', 'price'))))
} {
$row->put('subtotal', $row->qty * $row->price);
}
/** $cart->put($rowId, $row);
* 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
*/
protected function createRow($rowId, $id, $name, $qty, $price, $options)
{
$cart = $this->getContent();
$newRow = new CartRowCollection(array( return $cart;
'rowid' => $rowId, }
'id' => $id,
'name' => $name,
'qty' => $qty,
'price' => $price,
'options' => new CartRowOptionsCollection($options),
'subtotal' => $qty * $price
));
$cart->put($rowId, $newRow); /**
* 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
*/
protected function createRow($rowId, $id, $name, $qty, $price, $options)
{
$cart = $this->getContent();
return $cart; $newRow = new CartRowCollection(array(
} 'rowid' => $rowId,
'id' => $id,
'name' => $name,
'qty' => $qty,
'price' => $price,
'options' => new CartRowOptionsCollection($options),
'subtotal' => $qty * $price
));
/** $cart->put($rowId, $newRow);
* Update the quantity of a row
*
* @param string $rowId The ID of the row
* @param int $qty The qty to add
* @return CartCollection
*/
protected function updateQty($rowId, $qty)
{
if($qty == 0)
{
return $this->remove($rowId);
}
return $this->updateRow($rowId, array('qty' => $qty)); return $cart;
} }
/** /**
* Update an attribute of the row * Update the quantity of a row
* *
* @param string $rowId The ID of the row * @param string $rowId The ID of the row
* @param Array $attributes An array of attributes to update * @param int $qty The qty to add
* @return CartCollection * @return CartCollection
*/ */
protected function updateAttribute($rowId, $attributes) protected function updateQty($rowId, $qty)
{ {
return $this->updateRow($rowId, $attributes); if($qty == 0)
} {
return $this->remove($rowId);
}
return $this->updateRow($rowId, array('qty' => $qty));
}
/**
* 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
*/
protected function updateAttribute($rowId, $attributes)
{
return $this->updateRow($rowId, $attributes);
}
} }

View File

@@ -4,38 +4,38 @@ use Illuminate\Support\Collection;
class CartRowCollection extends Collection { class CartRowCollection extends Collection {
public function __construct($items) public function __construct($items)
{ {
parent::__construct($items); parent::__construct($items);
} }
public function __get($arg) public function __get($arg)
{ {
if($this->has($arg)) if($this->has($arg))
{ {
return $this->get($arg); return $this->get($arg);
} }
return NULL; return NULL;
} }
public function search(Array $search) public function search(Array $search)
{ {
foreach($search as $key => $value) foreach($search as $key => $value)
{ {
if($key === 'options') if($key === 'options')
{ {
$found = $this->{$key}->search($value); $found = $this->{$key}->search($value);
} }
else else
{ {
$found = ($this->{$key} === $value) ? true : false; $found = ($this->{$key} === $value) ? true : false;
} }
if( ! $found) return false; if( ! $found) return false;
} }
return $found; return $found;
} }
} }

View File

@@ -4,11 +4,11 @@ use Illuminate\Support\Facades\Facade;
class Cart extends Facade { class Cart extends Facade {
/** /**
* Get the registered name of the component. * Get the registered name of the component.
* *
* @return string * @return string
*/ */
protected static function getFacadeAccessor() { return 'cart'; } protected static function getFacadeAccessor() { return 'cart'; }
} }

View File

@@ -1,21 +1,21 @@
<?php namespace Gloudemans\Shoppingcart; <?php namespace Gloudemans\Shoppingcart;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class ShoppingcartServiceProvider extends ServiceProvider { class ShoppingcartServiceProvider extends ServiceProvider {
/** /**
* Register the service provider. * Register the service provider.
* *
* @return void * @return void
*/ */
public function register() public function register()
{ {
$this->app['cart'] = $this->app->share(function($app) $this->app['cart'] = $this->app->share(function($app)
{ {
$session = $app['session']; $session = $app['session'];
return new Cart($session); return new Cart($session);
}); });
} }
} }

View File

@@ -2,224 +2,224 @@
class CartTest extends TestCase { class CartTest extends TestCase {
public function testCartCanAdd() public function testCartCanAdd()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$this->assertEquals(Cart::count(), 1); $this->assertEquals(Cart::count(), 1);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanAddBatch() public function testCartCanAddBatch()
{ {
Cart::addBatch([ Cart::addBatch([
['id' => 1, 'name' => 'test_1', 'qty' => 1, 'price' => 10.00], ['id' => 1, 'name' => 'test_1', 'qty' => 1, 'price' => 10.00],
['id' => 2, 'name' => 'test_2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']] ['id' => 2, 'name' => 'test_2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
]); ]);
$this->assertEquals(Cart::count(), 2); $this->assertEquals(Cart::count(), 2);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanAddToExisting() public function testCartCanAddToExisting()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
$this->assertEquals(Cart::get($rowId)->qty, 2); $this->assertEquals(Cart::get($rowId)->qty, 2);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanUpdate() public function testCartCanUpdate()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
Cart::update($rowId, 2); Cart::update($rowId, 2);
$this->assertEquals(Cart::get($rowId)->qty, 2); $this->assertEquals(Cart::get($rowId)->qty, 2);
$this->assertEquals(Cart::get($rowId)->subtotal, 20.00); $this->assertEquals(Cart::get($rowId)->subtotal, 20.00);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanUpdateAttribute() public function testCartCanUpdateAttribute()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
Cart::update($rowId, ['name' => 'test_2']); Cart::update($rowId, ['name' => 'test_2']);
$this->assertEquals(Cart::get($rowId)->name, 'test_2'); $this->assertEquals(Cart::get($rowId)->name, 'test_2');
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanUpdateOptionsAttribute() public function testCartCanUpdateOptionsAttribute()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
Cart::update($rowId, ['options' => ['color' => 'yellow']]); Cart::update($rowId, ['options' => ['color' => 'yellow']]);
$this->assertEquals(Cart::get($rowId)->options, new Gloudemans\Shoppingcart\CartRowOptionsCollection(['size' => 'L', 'color' => 'yellow'])); $this->assertEquals(Cart::get($rowId)->options, new Gloudemans\Shoppingcart\CartRowOptionsCollection(['size' => 'L', 'color' => 'yellow']));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', Cart::get($rowId)->options); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', Cart::get($rowId)->options);
} }
public function testCartCanRemove() public function testCartCanRemove()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
Cart::add(2, 'test', 1, 10.00, ['size' => 'L']); Cart::add(2, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
Cart::remove($rowId); Cart::remove($rowId);
$this->assertEquals(Cart::count(), 1); $this->assertEquals(Cart::count(), 1);
$this->assertNull(Cart::get($rowId)); $this->assertNull(Cart::get($rowId));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanRemoveOnUpdate() public function testCartCanRemoveOnUpdate()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
Cart::update($rowId, 0); Cart::update($rowId, 0);
$this->assertEquals(Cart::count(), 0); $this->assertEquals(Cart::count(), 0);
$this->assertNull(Cart::get($rowId)); $this->assertNull(Cart::get($rowId));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanGet() public function testCartCanGet()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$rowId = Cart::content()->first()->rowid; $rowId = Cart::content()->first()->rowid;
$row = Cart::get($rowId); $row = Cart::get($rowId);
$this->assertEquals($row->id, 1); $this->assertEquals($row->id, 1);
$this->assertEquals($row->name, 'test'); $this->assertEquals($row->name, 'test');
$this->assertEquals($row->qty, 1); $this->assertEquals($row->qty, 1);
$this->assertEquals($row->price, 10.00); $this->assertEquals($row->price, 10.00);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowCollection', $row); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowCollection', $row);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', $row->options); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', $row->options);
$this->assertEquals($row, Cart::content()->first()); $this->assertEquals($row, Cart::content()->first());
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanGetContent() public function testCartCanGetContent()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
$this->assertEquals(Cart::content()->count(), 1); $this->assertEquals(Cart::content()->count(), 1);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanDestroy() public function testCartCanDestroy()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
Cart::destroy(); Cart::destroy();
$this->assertEquals(Cart::count(), 0); $this->assertEquals(Cart::count(), 0);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanGetTotal() public function testCartCanGetTotal()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
Cart::add(2, 'test', 1, 10.00, ['size' => 'L']); Cart::add(2, 'test', 1, 10.00, ['size' => 'L']);
$total = Cart::total(); $total = Cart::total();
$this->assertTrue(is_float($total)); $this->assertTrue(is_float($total));
$this->assertEquals($total, 20.00); $this->assertEquals($total, 20.00);
} }
public function testCartCanGetCount() public function testCartCanGetCount()
{ {
Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); Cart::add(1, 'test', 1, 10.00, ['size' => 'L']);
Cart::add(2, 'test', 2, 10.00, ['size' => 'L']); Cart::add(2, 'test', 2, 10.00, ['size' => 'L']);
$count = Cart::count(false); $count = Cart::count(false);
$this->assertTrue(is_integer($count)); $this->assertTrue(is_integer($count));
$this->assertEquals($count, 2); $this->assertEquals($count, 2);
$count = Cart::count(); $count = Cart::count();
$this->assertTrue(is_integer($count)); $this->assertTrue(is_integer($count));
$this->assertEquals($count, 3); $this->assertEquals($count, 3);
} }
public function testCartCanHaveMultipleInstances() public function testCartCanHaveMultipleInstances()
{ {
Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, ['size' => 'L']); Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, ['size' => 'L']);
Cart::instance('test_2')->add(2, 'test_2', 2, 10.00, ['size' => 'L']); Cart::instance('test_2')->add(2, 'test_2', 2, 10.00, ['size' => 'L']);
$name = Cart::instance('test_1')->content()->first()->name; $name = Cart::instance('test_1')->content()->first()->name;
$this->assertEquals($name, 'test_1'); $this->assertEquals($name, 'test_1');
$name = Cart::instance('test_2')->content()->first()->name; $name = Cart::instance('test_2')->content()->first()->name;
$this->assertEquals($name, 'test_2'); $this->assertEquals($name, 'test_2');
$count = Cart::count(); $count = Cart::count();
$this->assertEquals($count, 2); $this->assertEquals($count, 2);
Cart::add(3, 'test_3', 3, 10.00); Cart::add(3, 'test_3', 3, 10.00);
$count = Cart::count(); $count = Cart::count();
$this->assertEquals($count, 5); $this->assertEquals($count, 5);
Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, ['size' => 'L']); Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, ['size' => 'L']);
$count = Cart::count(); $count = Cart::count();
$this->assertEquals($count, 2); $this->assertEquals($count, 2);
} }
public function testCartCanSearch() public function testCartCanSearch()
{ {
Cart::add(1, 'Product 1', 1, 10.00, ['size' => 'large', 'color' => 'red']); Cart::add(1, 'Product 1', 1, 10.00, ['size' => 'large', 'color' => 'red']);
Cart::add(2, 'Product 2', 1, 10.00, ['size' => 'large']); Cart::add(2, 'Product 2', 1, 10.00, ['size' => 'large']);
$search = Cart::search(['id' => 1, 'name' => 'Product 1']); $search = Cart::search(['id' => 1, 'name' => 'Product 1']);
$this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']);
$search = Cart::search(['id' => 2, 'name' => 'Product 1']); $search = Cart::search(['id' => 2, 'name' => 'Product 1']);
$this->assertFalse($search); $this->assertFalse($search);
$search = Cart::search(['id' => 2, 'name' => 'Product 2']); $search = Cart::search(['id' => 2, 'name' => 'Product 2']);
$this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']); $this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']);
$search = Cart::search(['id' => 1, 'price' => 10.00]); $search = Cart::search(['id' => 1, 'price' => 10.00]);
$this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']);
$search = Cart::search(['qty' => 1, 'price' => 10.00]); $search = Cart::search(['qty' => 1, 'price' => 10.00]);
$this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']); $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']);
$search = Cart::search(['id' => 2, 'kaas' => 'Product 2']); $search = Cart::search(['id' => 2, 'kaas' => 'Product 2']);
$this->assertFalse($search); $this->assertFalse($search);
$search = Cart::search(['id' => 2, 'options' => ['size' => 'large']]); $search = Cart::search(['id' => 2, 'options' => ['size' => 'large']]);
$this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']); $this->assertEquals($search, ['bb0042610e1c6e8bfd7293bfa1807f82']);
$search = Cart::search(['options' => ['size' => 'large']]); $search = Cart::search(['options' => ['size' => 'large']]);
$this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']); $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928', 'bb0042610e1c6e8bfd7293bfa1807f82']);
$search = Cart::search(['id' => 1, 'options' => ['color' => 'red']]); $search = Cart::search(['id' => 1, 'options' => ['color' => 'red']]);
$this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']); $this->assertEquals($search, ['a308327de59a3e249baabc24a6c29928']);
} }
} }