Files
LaravelShoppingcart/tests/CartTest.php
Rob Gloudemans c8e37b805e Fixed tests
2014-02-12 20:02:36 +01:00

277 lines
8.8 KiB
PHP

<?php
use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartCollection;
use Gloudemans\Shoppingcart\CartRowCollection;
use Gloudemans\Shoppingcart\CartRowOptionsCollection;
use Mockery as m;
require_once 'SessionMock.php';
class CartTest extends PHPUnit_Framework_TestCase {
protected $session;
protected $events;
protected $cart;
public function setUp()
{
$this->session = new SessionMock;
$this->events = m::mock('Illuminate\Events\Dispatcher');
$this->cart = new Cart($this->session, $this->events);
}
public function tearDown()
{
m::close();
}
public function testCartCanAdd()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
}
public function testCartCanAddArray()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large')));
}
public function testCartCanAddBatch()
{
$this->events->shouldReceive('fire')->once()->with('cart.batch', m::type('array'));
$this->cart->add(array(
array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00),
array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large'))
));
}
/**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException
*/
public function testCartThrowsExceptionOnEmptyItem()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
$this->cart->add('', '', '', '');
}
/**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidQtyException
*/
public function testCartThrowsExceptionOnNoneNumericQty()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
$this->cart->add('293ad', 'Product 1', 'none-numeric', 9.99);
}
/**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidPriceException
*/
public function testCartThrowsExceptionOnNoneNumericPrice()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
$this->cart->add('293ad', 'Product 1', 1, 'none-numeric');
}
public function testCartCanUpdateExistingItem()
{
$this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertEquals(2, $this->cart->content()->first()->qty);
}
public function testCartCanUpdateQty()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->update('8cbf215baa3b757e910e5305ab981172', 2);
$this->assertEquals(2, $this->cart->content()->first()->qty);
}
public function testCartCanUpdateItem()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->update('8cbf215baa3b757e910e5305ab981172', ['name' => 'Product 2']);
$this->assertEquals('Product 2', $this->cart->content()->first()->name);
}
public function testCartCanUpdateOptions()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.update', m::type('string'));
$this->cart->add('293ad', 'Product 1', 1, 9.99, ['size' => 'S']);
$this->cart->update('9be7e69d236ca2d09d2e0838d2c59aeb', ['options' => ['size' => 'L']]);
$this->assertEquals('L', $this->cart->content()->first()->options->size);
}
/**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException
*/
public function testCartThrowsExceptionOnInvalidRowId()
{
$this->cart->update('invalidRowId', 1);
}
public function testCartCanRemove()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->remove('8cbf215baa3b757e910e5305ab981172');
$this->assertTrue($this->cart->content()->isEmpty());
}
public function testCartCanRemoveOnUpdate()
{
$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', 0);
$this->assertTrue($this->cart->content()->isEmpty());
}
public function testCartCanGet()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$item = $this->cart->get('8cbf215baa3b757e910e5305ab981172');
$this->assertEquals('293ad', $item->id);
}
public function testCartCanGetContent()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', $this->cart->content());
$this->assertFalse($this->cart->content()->isEmpty());
}
public function testCartCanDestroy()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.destroy');
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->destroy();
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', $this->cart->content());
$this->assertTrue($this->cart->content()->isEmpty());
}
public function testCartCanGetTotal()
{
$this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 1, 19.99);
$this->assertEquals(29.98, $this->cart->total());
}
public function testCartCanGetItemCount()
{
$this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 2, 19.99);
$this->assertEquals(3, $this->cart->count());
}
public function testCartCanGetRowCount()
{
$this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 2, 19.99);
$this->assertEquals(2, $this->cart->count(false));
}
public function testCartCanSearch()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertEquals('8cbf215baa3b757e910e5305ab981172', $this->cart->search(array('id' => '293ad'))[0]);
}
public function testCartCanHaveMultipleInstances()
{
$this->events->shouldReceive('fire')->twice()->with('cart.add', 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->assertTrue($this->cart->instance('firstInstance')->content()->has('8cbf215baa3b757e910e5305ab981172'));
$this->assertFalse($this->cart->instance('firstInstance')->content()->has('22eae2b9c10083d6631aaa023106871a'));
$this->assertTrue($this->cart->instance('secondInstance')->content()->has('22eae2b9c10083d6631aaa023106871a'));
$this->assertFalse($this->cart->instance('secondInstance')->content()->has('8cbf215baa3b757e910e5305ab981172'));
}
/**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException
*/
public function testCartThrowsExceptionOnEmptyInstance()
{
$this->cart->instance();
}
public function testCartReturnsCartCollection()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', $this->cart->content());
}
public function testCartCollectionHasCartRowCollection()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowCollection', $this->cart->content()->first());
}
public function testCartRowCollectionHasCartRowOptionsCollection()
{
$this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', $this->cart->content()->first()->options);
}
}