Convert indentation to tabs and change line ending to Unix.

Signed-off-by: Edwin <tkaw220@gmail.com>
This commit is contained in:
Edwin
2013-07-02 11:44:52 +08:00
parent 7d148ad0ac
commit 043e7fb1ce
6 changed files with 534 additions and 534 deletions

View File

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

View File

@@ -4,365 +4,365 @@ use Illuminate\Support\Collection;
class Cart {
/**
* Session class instance
*
* @var Session
*/
protected $session;
/**
* Session class instance
*
* @var Session
*/
protected $session;
/**
* Current cart instance
*
* @var string
*/
protected $instance;
/**
* Current cart instance
*
* @var string
*/
protected $instance;
/**
* Constructor
*
* @param Session $session Session class instance
*/
public function __construct($session)
{
$this->session = $session;
/**
* Constructor
*
* @param Session $session Session class instance
*/
public function __construct($session)
{
$this->session = $session;
$this->instance = 'main';
}
$this->instance = 'main';
}
/**
* Set the current cart instance
*
* @param string $instance Cart instance name
* @return Cart
*/
public function instance($instance)
{
$this->instance = $instance;
/**
* Set the current cart instance
*
* @param string $instance Cart instance name
* @return Cart
*/
public function instance($instance)
{
$this->instance = $instance;
// Return self so the method is chainable
return $this;
}
// Return self so the method is chainable
return $this;
}
/**
* Add a 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'
*/
public function add($id, $name, $qty, $price, Array $options = array())
{
$cart = $this->getContent();
/**
* Add a 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'
*/
public function add($id, $name, $qty, $price, Array $options = array())
{
$cart = $this->getContent();
$rowId = $this->generateRowId($id, $options);
$rowId = $this->generateRowId($id, $options);
if($cart->has($rowId))
{
$row = $cart->get($rowId);
$cart = $this->updateRow($rowId, array('qty' => $row->qty + $qty));
}
else
{
$cart = $this->createRow($rowId, $id, $name, $qty, $price, $options);
}
if($cart->has($rowId))
{
$row = $cart->get($rowId);
$cart = $this->updateRow($rowId, array('qty' => $row->qty + $qty));
}
else
{
$cart = $this->createRow($rowId, $id, $name, $qty, $price, $options);
}
return $this->updateCart($cart);
}
return $this->updateCart($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
*/
public function addBatch(Array $items)
{
foreach($items as $item)
{
$options = (isset($item['options'])) ? $item['options'] : array();
/**
* 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
*/
public function addBatch(Array $items)
{
foreach($items as $item)
{
$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
*
* @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)
{
if(is_array($attribute))
{
return $this->updateAttribute($rowId, $attribute);
}
/**
* 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
* @return boolean
*/
public function update($rowId, $attribute)
{
if(is_array($attribute))
{
return $this->updateAttribute($rowId, $attribute);
}
return $this->updateQty($rowId, $attribute);
}
return $this->updateQty($rowId, $attribute);
}
/**
* Remove a row from the cart
*
* @param string $rowId The rowid of the item
* @return boolean
*/
public function remove($rowId)
{
$cart = $this->getContent();
/**
* Remove a row from the cart
*
* @param string $rowId The rowid of the item
* @return boolean
*/
public function remove($rowId)
{
$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
*
* @param string $rowId The ID of the row to fetch
* @return CartCollection
*/
public function get($rowId)
{
$cart = $this->getContent();
/**
* Get a row of the cart by its ID
*
* @param string $rowId The ID of the row to fetch
* @return CartCollection
*/
public function get($rowId)
{
$cart = $this->getContent();
return ($cart->has($rowId)) ? $cart->get($rowId) : NULL;
}
return ($cart->has($rowId)) ? $cart->get($rowId) : NULL;
}
/**
* Get the cart content
*
* @return CartRowCollection
*/
public function content()
{
$cart = $this->getContent();
/**
* Get the cart content
*
* @return CartRowCollection
*/
public function content()
{
$cart = $this->getContent();
return (empty($cart)) ? NULL : $cart;
}
/**
* Empty the cart
*
* @return boolean
*/
public function destroy()
{
return $this->updateCart(NULL);
}
return (empty($cart)) ? NULL : $cart;
}
/**
* Get the price total
*
* @return float
*/
public function total()
{
$total = 0;
$cart = $this->getContent();
/**
* Empty the cart
*
* @return boolean
*/
public function destroy()
{
return $this->updateCart(NULL);
}
if(empty($cart))
{
return $total;
}
/**
* Get the price total
*
* @return float
*/
public function total()
{
$total = 0;
$cart = $this->getContent();
foreach($cart AS $row)
{
$total += $row->subtotal;
}
if(empty($cart))
{
return $total;
}
return $total;
}
foreach($cart AS $row)
{
$total += $row->subtotal;
}
/**
* 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();
return $total;
}
if( ! $totalItems)
{
return $cart->count();
}
/**
* 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();
$count = 0;
if( ! $totalItems)
{
return $cart->count();
}
foreach($cart AS $row)
{
$count += $row->qty;
}
$count = 0;
return $count;
}
foreach($cart AS $row)
{
$count += $row->qty;
}
/**
* 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);
return $count;
}
if($found)
{
$rows[] = $item->rowid;
}
}
/**
* 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);
return (empty($rows)) ? false : $rows;
}
if($found)
{
$rows[] = $item->rowid;
}
}
/**
* 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)));
}
return (empty($rows)) ? false : $rows;
}
/**
* Update the cart
*
* @param CartCollection $cart The new cart content
* @return void
*/
protected function updateCart($cart)
{
return $this->session->put($this->getInstance(), $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'
* @return boolean
*/
protected function generateRowId($id, $options)
{
return md5($id . serialize(ksort($options)));
}
/**
* 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;
/**
* Update the cart
*
* @param CartCollection $cart The new cart content
* @return void
*/
protected function updateCart($cart)
{
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;
/**
* Get the current cart instance
*
* @return string
*/
protected function getInstance()
{
return 'cart.' . $this->instance;
}
return $content;
}
/**
* 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
*/
protected function updateRow($rowId, $attributes)
{
$cart = $this->getContent();
/**
* Get the current cart instance
*
* @return string
*/
protected function getInstance()
{
return 'cart.' . $this->instance;
}
$row = $cart->get($rowId);
foreach($attributes as $key => $value)
{
if($key == 'options')
{
$options = $row->options->merge($value);
$row->put($key, $options);
}
else
{
$row->put($key, $value);
}
}
/**
* 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
*/
protected function updateRow($rowId, $attributes)
{
$cart = $this->getContent();
if( ! is_null(array_keys($attributes, array('qty', 'price'))))
{
$row->put('subtotal', $row->qty * $row->price);
}
$row = $cart->get($rowId);
$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);
}
/**
* 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();
$cart->put($rowId, $row);
$newRow = new CartRowCollection(array(
'rowid' => $rowId,
'id' => $id,
'name' => $name,
'qty' => $qty,
'price' => $price,
'options' => new CartRowOptionsCollection($options),
'subtotal' => $qty * $price
));
return $cart;
}
$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
));
/**
* 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);
}
$cart->put($rowId, $newRow);
return $this->updateRow($rowId, array('qty' => $qty));
}
return $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
*/
protected function updateAttribute($rowId, $attributes)
{
return $this->updateRow($rowId, $attributes);
}
/**
* 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));
}
/**
* 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 {
public function __construct($items)
{
parent::__construct($items);
}
public function __construct($items)
{
parent::__construct($items);
}
public function __get($arg)
{
if($this->has($arg))
{
return $this->get($arg);
}
public function __get($arg)
{
if($this->has($arg))
{
return $this->get($arg);
}
return NULL;
}
return NULL;
}
public function search(Array $search)
{
foreach($search as $key => $value)
{
if($key === 'options')
{
$found = $this->{$key}->search($value);
}
else
{
$found = ($this->{$key} === $value) ? true : false;
}
public function search(Array $search)
{
foreach($search as $key => $value)
{
if($key === 'options')
{
$found = $this->{$key}->search($value);
}
else
{
$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 {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cart'; }
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'cart'; }
}

View File

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

View File

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