modify storage interface and test it, add tests for operator and provider.

This commit is contained in:
Paweł Jędrzejewski 2012-03-28 21:20:54 +02:00
parent 5002b77d6b
commit a754a5a722
7 changed files with 291 additions and 9 deletions

View file

@ -46,11 +46,13 @@ abstract class CartOperator implements CartOperatorInterface
*/
public function addItem(CartInterface $cart, ItemInterface $item)
{
foreach ($cart->getItems() as $existingItem) {
if ($item->equals($existingItem)) {
$existingItem->setQuantity($existingItem->getQuantity() + $item->getQuantity());
if (0 > $cart->countItems()) {
foreach ($cart->getItems() as $existingItem) {
if ($existingItem->equals($item)) {
$existingItem->setQuantity($existingItem->getQuantity() + $item->getQuantity());
return;
return;
}
}
}

View file

@ -76,7 +76,7 @@ class CartProvider implements CartProviderInterface
$cart = $this->cartManager->createCart();
$this->cartManager->persistCart($cart);
$this->storage->setCurrentCartIdentifier($cart->getId());
$this->storage->setCurrentCartIdentifier($cart);
$this->cart = $cart;
}
@ -92,5 +92,6 @@ class CartProvider implements CartProviderInterface
public function setCart(CartInterface $cart)
{
$this->cart = $cart;
$this->storage->setCurrentCartIdentifier($cart);
}
}

View file

@ -11,6 +11,8 @@
namespace Sylius\Bundle\CartBundle\Storage;
use Sylius\Bundle\CartBundle\Model\CartInterface;
/**
* Interface for service that stores current cart id.
*
@ -28,7 +30,7 @@ interface CartStorageInterface
/**
* Sets current cart id and persists it.
*
* @param mixed $identifier
* @param CartInterface $cart
*/
function setCurrentCartIdentifier($identifier);
function setCurrentCartIdentifier(CartInterface $cart);
}

View file

@ -11,6 +11,7 @@
namespace Sylius\Bundle\CartBundle\Storage;
use Sylius\Bundle\CartBundle\Model\CartInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
@ -49,8 +50,8 @@ class SessionCartStorage implements CartStorageInterface
/**
* {@inheritdoc}
*/
public function setCurrentCartIdentifier($identifier)
public function setCurrentCartIdentifier(CartInterface $cart)
{
$this->session->set('_sylius.cart-id', $identifier);
$this->session->set('_sylius.cart-id', $cart->getId());
}
}

View file

@ -0,0 +1,129 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\CartBundle\Tests\Operator;
/**
* Simple default operator test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class CartOperatorTest extends \PHPUnit_Framework_TestCase
{
public function testAddItemAddsItemToCart()
{
$item = $this->getMockItem();
$cart = $this->getMockCart();
$cart->expects($this->once())
->method('addItem')
->with($item)
;
$cartOperator = $this->getCartOperator();
$cartOperator->addItem($cart, $item);
}
public function testRefreshSetsTotalItems()
{
$cart = $this->getMockCart();
$cart->expects($this->once())
->method('countItems')
->will($this->returnValue(6))
;
$cart->expects($this->once())
->method('setTotalItems')
->with($this->equalTo(6))
;
$cartOperator = $this->getCartOperator();
$cartOperator->refresh($cart);
}
public function testClearRemovesCart()
{
$cart = $this->getMockCart();
$cartManager = $this->getMockCartManager();
$cartManager->expects($this->once())
->method('removeCart')
->with($this->equalTo($cart))
;
$cartOperator = $this->getCartOperator($cartManager);
$cartOperator->clear($cart);
}
public function testSavePersistsCart()
{
$cart = $this->getMockCart();
$cartManager = $this->getMockCartManager();
$cartManager->expects($this->once())
->method('persistCart')
->with($this->equalTo($cart))
;
$cartOperator = $this->getCartOperator($cartManager);
$cartOperator->save($cart);
}
public function testRemoveItemRemovesItemFromCart()
{
$item = $this->getMockItem();
$cart = $this->getMockCart();
$cart->expects($this->once())
->method('removeItem')
->with($item)
;
$cartOperator = $this->getCartOperator();
$cartOperator->removeItem($cart, $item);
}
private function getCartOperator($cartManager = null)
{
if (null === $cartManager) {
$cartManager = $this->getMockCartManager();
}
return $this->getMockBuilder('Sylius\Bundle\CartBundle\Operator\CartOperator')
->setConstructorArgs(array($cartManager))
->getMockForAbstractClass()
;
}
private function getCart()
{
return $this->getMockForAbstractClass('Sylius\Bundle\CartBundle\Model\Cart');
}
private function getItem()
{
return $this->getMockForAbstractClass('Sylius\Bundle\CartBundle\Model\Item');
}
private function getMockCartManager()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\CartManagerInterface');
}
private function getMockCart()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\CartInterface');
}
private function getMockItem()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\ItemInterface');
}
}

View file

@ -0,0 +1,98 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\CartBundle\Tests\Provider;
use Sylius\Bundle\CartBundle\Provider\CartProvider;
/**
* Default cart provider test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class CartOperatorTest extends \PHPUnit_Framework_TestCase
{
public function testGetCartReturnsNewCartWhenCartStorageReturnsNullIdentifier()
{
$cart = $this->getMockCart();
$cartStorage = $this->getMockCartStorage();
$cartStorage->expects($this->once())
->method('getCurrentCartIdentifier')
->will($this->returnValue(null))
;
$cartStorage->expects($this->once())
->method('setCurrentCartIdentifier')
->with($this->equalTo($cart))
;
$cartManager = $this->getMockCartManager();
$cartManager->expects($this->once())
->method('createCart')
->will($this->returnValue($cart))
;
$cartManager->expects($this->once())
->method('persistCart')
->with($this->equalTo($cart))
;
$cartProvider = new CartProvider($cartStorage, $cartManager);
$this->assertEquals($cart, $cartProvider->getCart());
}
public function testGetCartReturnsExistingCartBasedOnCartStorage()
{
$cartStorage = $this->getMockCartStorage();
$cartStorage->expects($this->once())
->method('getCurrentCartIdentifier')
->will($this->returnValue(123))
;
$cart = $this->getMockCart();
$cartManager = $this->getMockCartManager();
$cartManager->expects($this->once())
->method('findCart')
->with($this->equalTo(123))
->will($this->returnValue($cart))
;
$cartProvider = new CartProvider($cartStorage, $cartManager);
$this->assertEquals($cart, $cartProvider->getCart());
}
public function testSimpleGetSetCart()
{
$cart = $this->getMockCart();
$cartProvider = new CartProvider($this->getMockCartStorage(), $this->getMockCartManager());
$cartProvider->setCart($cart);
$this->assertEquals($cart, $cartProvider->getCart());
}
private function getMockCartStorage()
{
return $this->getMock('Sylius\Bundle\CartBundle\Storage\CartStorageInterface');
}
private function getMockCartManager()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\CartManagerInterface');
}
private function getMockCart()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\CartInterface');
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\CartBundle\Tests\Storage;
use Sylius\Bundle\CartBundle\Storage\SessionCartStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* Session storage test.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class SessionCartStorageTest extends \PHPUnit_Framework_TestCase
{
public function testGetCurrentCartIdentifier()
{
$cart = $this->getMockCart();
$cart->expects($this->once())
->method('getId')
->will($this->returnValue(123))
;
$storage = new SessionCartStorage($this->getSession());
$storage->setCurrentCartIdentifier($cart);
$this->assertEquals(123, $storage->getCurrentCartIdentifier());
}
private function getSession()
{
return new Session(new MockArraySessionStorage());
}
private function getMockCart()
{
return $this->getMock('Sylius\Bundle\CartBundle\Model\CartInterface');
}
}