Fixed tests

This commit is contained in:
Rob Gloudemans
2014-02-12 20:02:36 +01:00
parent 40d596c266
commit c8e37b805e
5 changed files with 195 additions and 178 deletions

View File

@@ -3,6 +3,7 @@ language: php
php: php:
- 5.3 - 5.3
- 5.4 - 5.4
- 5.5
before_script: before_script:
- curl -s http://getcomposer.org/installer | php - curl -s http://getcomposer.org/installer | php

View File

@@ -12,6 +12,9 @@
"php": ">=5.3.0", "php": ">=5.3.0",
"illuminate/support": "~4" "illuminate/support": "~4"
}, },
"require-dev": {
"mockery/mockery": "0.9.*"
},
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"Gloudemans\\Shoppingcart": "src/" "Gloudemans\\Shoppingcart": "src/"

View File

@@ -14,8 +14,8 @@ class ShoppingcartServiceProvider extends ServiceProvider {
$this->app['cart'] = $this->app->share(function($app) $this->app['cart'] = $this->app->share(function($app)
{ {
$session = $app['session']; $session = $app['session'];
$event = $app['events']; $events = $app['events'];
return new Cart($session, $event); return new Cart($session, $events);
}); });
} }
} }

View File

@@ -1,281 +1,277 @@
<?php <?php
class CartTest extends TestCase { 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() public function testCartCanAdd()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->assertEquals(Cart::count(), 1); $this->cart->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanAddArray() public function testCartCanAddArray()
{ {
Cart::add(array('id' => 1, 'name' => 'test', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'L'))); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->assertEquals(Cart::count(), 1); $this->cart->add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large')));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanAddBatch() public function testCartCanAddBatch()
{ {
Cart::add(array( $this->events->shouldReceive('fire')->once()->with('cart.batch', m::type('array'));
array('id' => 1, 'name' => 'test_1', 'qty' => 1, 'price' => 10.00),
array('id' => 2, 'name' => 'test_2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large')) $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'))
)); ));
$this->assertEquals(Cart::count(), 2);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanAddToExisting() /**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException
*/
public function testCartThrowsExceptionOnEmptyItem()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
Cart::add(1, 'test', 1, 10.00, array('size' => 'L'));
$rowId = Cart::content()->first()->rowid; $this->cart->add('', '', '', '');
$this->assertEquals(Cart::get($rowId)->qty, 2);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanUpdate() /**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidQtyException
*/
public function testCartThrowsExceptionOnNoneNumericQty()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 'none-numeric', 9.99);
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());
} }
public function testCartCanUpdateAttribute() /**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidPriceException
*/
public function testCartThrowsExceptionOnNoneNumericPrice()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::any());
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 1, 'none-numeric');
Cart::update($rowId, array('name' => 'test_2'));
$this->assertEquals(Cart::get($rowId)->name, 'test_2');
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanUpdateOptionsAttribute() public function testCartCanUpdateExistingItem()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('293ad', 'Product 1', 1, 9.99);
Cart::update($rowId, array('options' => array('color' => 'yellow'))); $this->assertEquals(2, $this->cart->content()->first()->qty);
}
$this->assertEquals(Cart::get($rowId)->options, new Gloudemans\Shoppingcart\CartRowOptionsCollection(array('size' => 'L', 'color' => 'yellow'))); public function testCartCanUpdateQty()
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', Cart::get($rowId)->options); {
$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() public function testCartCanRemove()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
Cart::add(2, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.remove', m::type('string'));
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->remove('8cbf215baa3b757e910e5305ab981172');
Cart::remove($rowId); $this->assertTrue($this->cart->content()->isEmpty());
$this->assertEquals(Cart::count(), 1);
$this->assertNull(Cart::get($rowId));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanRemoveOnUpdate() public function testCartCanRemoveOnUpdate()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $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'));
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->update('8cbf215baa3b757e910e5305ab981172', 0);
Cart::update($rowId, 0); $this->assertTrue($this->cart->content()->isEmpty());
$this->assertEquals(Cart::count(), 0);
$this->assertNull(Cart::get($rowId));
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
} }
public function testCartCanGet() public function testCartCanGet()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$rowId = Cart::content()->first()->rowid; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$item = $this->cart->get('8cbf215baa3b757e910e5305ab981172');
$row = Cart::get($rowId); $this->assertEquals('293ad', $item->id);
$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() public function testCartCanGetContent()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->assertEquals(Cart::content()->count(), 1); $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content());
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', $this->cart->content());
$this->assertFalse($this->cart->content()->isEmpty());
} }
public function testCartCanDestroy() public function testCartCanDestroy()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
$this->events->shouldReceive('fire')->once()->with('cart.destroy');
Cart::destroy(); $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->destroy();
$this->assertEquals(Cart::count(), 0); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', $this->cart->content());
$this->assertInstanceOf('Gloudemans\Shoppingcart\CartCollection', Cart::content()); $this->assertTrue($this->cart->content()->isEmpty());
} }
public function testCartCanGetTotal() public function testCartCanGetTotal()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
Cart::add(2, 'test', 1, 10.00, array('size' => 'L'));
$total = Cart::total(); $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 1, 19.99);
$this->assertTrue(is_float($total)); $this->assertEquals(29.98, $this->cart->total());
$this->assertEquals($total, 20.00);
} }
public function testCartCanGetCount() public function testCartCanGetItemCount()
{ {
Cart::add(1, 'test', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
Cart::add(2, 'test', 2, 10.00, array('size' => 'L'));
$count = Cart::count(false); $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 2, 19.99);
$this->assertTrue(is_integer($count)); $this->assertEquals(3, $this->cart->count());
$this->assertEquals($count, 2);
$count = Cart::count();
$this->assertTrue(is_integer($count));
$this->assertEquals($count, 3);
} }
public function testCartCanHaveMultipleInstances() public function testCartCanGetRowCount()
{ {
Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, array('size' => 'L')); $this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
Cart::instance('test_2')->add(2, 'test_2', 2, 10.00, array('size' => 'L'));
$name = Cart::instance('test_1')->content()->first()->name; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$this->cart->add('986se', 'Product 2', 2, 19.99);
$this->assertEquals($name, 'test_1'); $this->assertEquals(2, $this->cart->count(false));
$name = Cart::instance('test_2')->content()->first()->name;
$this->assertEquals($name, 'test_2');
$count = Cart::count();
$this->assertEquals($count, 2);
Cart::add(3, 'test_3', 3, 10.00);
$count = Cart::count();
$this->assertEquals($count, 5);
Cart::instance('test_1')->add(1, 'test_1', 1, 10.00, array('size' => 'L'));
$count = Cart::count();
$this->assertEquals($count, 2);
} }
public function testCartCanSearch() public function testCartCanSearch()
{ {
Cart::add(1, 'Product 1', 1, 10.00, array('size' => 'large', 'color' => 'red')); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
Cart::add(2, 'Product 2', 1, 10.00, array('size' => 'large'));
$row1 = '17351068ba2f30bac8d3ccb1ad94dfe6'; $this->cart->add('293ad', 'Product 1', 1, 9.99);
$row2 = '5b9865a287421917623bdcf99f7de904';
$search = Cart::search(array('id' => 1, 'name' => 'Product 1')); $this->assertEquals('8cbf215baa3b757e910e5305ab981172', $this->cart->search(array('id' => '293ad'))[0]);
$this->assertEquals($search, array($row1)); }
$search = Cart::search(array('id' => 2, 'name' => 'Product 1')); public function testCartCanHaveMultipleInstances()
$this->assertFalse($search); {
$this->events->shouldReceive('fire')->twice()->with('cart.add', m::type('array'));
$search = Cart::search(array('id' => 2, 'name' => 'Product 2')); $this->cart->instance('firstInstance')->add('293ad', 'Product 1', 1, 9.99);
$this->assertEquals($search, array($row2)); $this->cart->instance('secondInstance')->add('986se', 'Product 2', 1, 19.99);
$search = Cart::search(array('id' => 1, 'price' => 10.00)); $this->assertTrue($this->cart->instance('firstInstance')->content()->has('8cbf215baa3b757e910e5305ab981172'));
$this->assertEquals($search, array($row1)); $this->assertFalse($this->cart->instance('firstInstance')->content()->has('22eae2b9c10083d6631aaa023106871a'));
$this->assertTrue($this->cart->instance('secondInstance')->content()->has('22eae2b9c10083d6631aaa023106871a'));
$search = Cart::search(array('qty' => 1, 'price' => 10.00)); $this->assertFalse($this->cart->instance('secondInstance')->content()->has('8cbf215baa3b757e910e5305ab981172'));
$this->assertEquals($search, array($row1, $row2));
$search = Cart::search(array('id' => 2, 'kaas' => 'Product 2'));
$this->assertFalse($search);
$search = Cart::search(array('id' => 2, 'options' => array('size' => 'large')));
$this->assertEquals($search, array($row2));
$search = Cart::search(array('options' => array('size' => 'large')));
$this->assertEquals($search, array($row1, $row2));
$search = Cart::search(array('id' => 1, 'options' => array('color' => 'red')));
$this->assertEquals($search, array($row1));
$search = Cart::search(array('options' => array('color' => 'red', 'size' => 'large')));
$this->assertEquals($search, array($row1));
} }
/** /**
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException * @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException
*/ */
public function testShoppingcartInstanceException() public function testCartThrowsExceptionOnEmptyInstance()
{ {
Cart::instance(); $this->cart->instance();
} }
/** public function testCartReturnsCartCollection()
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException
*/
public function testShoppingcartInvalidItemException()
{ {
Cart::add(1); $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()
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidQtyException
*/
public function testShoppingcartInvalidQtyException()
{ {
Cart::add(1, 'Product 1', 'nonnumeric', 10.00); $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()
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidPriceException
*/
public function testShoppingcartInvalidPriceException()
{ {
Cart::add(1, 'Product 1', 1, 'nonnumeric'); $this->events->shouldReceive('fire')->once()->with('cart.add', m::type('array'));
}
/** $this->cart->add('293ad', 'Product 1', 1, 9.99);
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException
*/
public function testShoppingcartInvalidRowIDException()
{
Cart::add(1, 'Product 1', 1, 10.00);
Cart::update('nonexistingrowid', 2); $this->assertInstanceOf('Gloudemans\Shoppingcart\CartRowOptionsCollection', $this->cart->content()->first()->options);
} }
} }

17
tests/SessionMock.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
class SessionMock {
protected $session;
public function has($key)
{
return isset($this->session[$key]);
}
public function get($key)
{
return $this->session[$key];
}
public function put($key, $value)
{
$this->session[$key] = $value;
}
}