From a754a5a7223a39e2c94e6eaa1529b7105e2b5679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20J=C4=99drzejewski?= Date: Wed, 28 Mar 2012 21:20:54 +0200 Subject: [PATCH] modify storage interface and test it, add tests for operator and provider. --- .../CartBundle/Operator/CartOperator.php | 10 +- .../CartBundle/Provider/CartProvider.php | 3 +- .../Storage/CartStorageInterface.php | 6 +- .../CartBundle/Storage/SessionCartStorage.php | 5 +- .../Tests/Operator/CartOperatorTest.php | 129 ++++++++++++++++++ .../Tests/Provider/CartProviderTest.php | 98 +++++++++++++ .../Tests/Storage/SessionStorageTest.php | 49 +++++++ 7 files changed, 291 insertions(+), 9 deletions(-) create mode 100644 src/Sylius/Bundle/CartBundle/Tests/Operator/CartOperatorTest.php create mode 100644 src/Sylius/Bundle/CartBundle/Tests/Provider/CartProviderTest.php create mode 100644 src/Sylius/Bundle/CartBundle/Tests/Storage/SessionStorageTest.php diff --git a/src/Sylius/Bundle/CartBundle/Operator/CartOperator.php b/src/Sylius/Bundle/CartBundle/Operator/CartOperator.php index 559aac3d6f..37c5b11014 100644 --- a/src/Sylius/Bundle/CartBundle/Operator/CartOperator.php +++ b/src/Sylius/Bundle/CartBundle/Operator/CartOperator.php @@ -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; + } } } diff --git a/src/Sylius/Bundle/CartBundle/Provider/CartProvider.php b/src/Sylius/Bundle/CartBundle/Provider/CartProvider.php index 7b7bb4f755..a510aaedca 100644 --- a/src/Sylius/Bundle/CartBundle/Provider/CartProvider.php +++ b/src/Sylius/Bundle/CartBundle/Provider/CartProvider.php @@ -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); } } diff --git a/src/Sylius/Bundle/CartBundle/Storage/CartStorageInterface.php b/src/Sylius/Bundle/CartBundle/Storage/CartStorageInterface.php index 84f6ac82f4..68bc145c7b 100644 --- a/src/Sylius/Bundle/CartBundle/Storage/CartStorageInterface.php +++ b/src/Sylius/Bundle/CartBundle/Storage/CartStorageInterface.php @@ -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); } diff --git a/src/Sylius/Bundle/CartBundle/Storage/SessionCartStorage.php b/src/Sylius/Bundle/CartBundle/Storage/SessionCartStorage.php index 5ac8443776..cfd66641d9 100644 --- a/src/Sylius/Bundle/CartBundle/Storage/SessionCartStorage.php +++ b/src/Sylius/Bundle/CartBundle/Storage/SessionCartStorage.php @@ -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()); } } diff --git a/src/Sylius/Bundle/CartBundle/Tests/Operator/CartOperatorTest.php b/src/Sylius/Bundle/CartBundle/Tests/Operator/CartOperatorTest.php new file mode 100644 index 0000000000..4e15a24ba1 --- /dev/null +++ b/src/Sylius/Bundle/CartBundle/Tests/Operator/CartOperatorTest.php @@ -0,0 +1,129 @@ + + */ +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'); + } +} diff --git a/src/Sylius/Bundle/CartBundle/Tests/Provider/CartProviderTest.php b/src/Sylius/Bundle/CartBundle/Tests/Provider/CartProviderTest.php new file mode 100644 index 0000000000..9976c62ca7 --- /dev/null +++ b/src/Sylius/Bundle/CartBundle/Tests/Provider/CartProviderTest.php @@ -0,0 +1,98 @@ + + */ +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'); + } +} diff --git a/src/Sylius/Bundle/CartBundle/Tests/Storage/SessionStorageTest.php b/src/Sylius/Bundle/CartBundle/Tests/Storage/SessionStorageTest.php new file mode 100644 index 0000000000..af305649fc --- /dev/null +++ b/src/Sylius/Bundle/CartBundle/Tests/Storage/SessionStorageTest.php @@ -0,0 +1,49 @@ + + */ +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'); + } +} +