Add specification for CartProvider, clean it up a bit

This commit is contained in:
Paweł Jędrzejewski 2012-11-02 15:18:35 +01:00
parent c392203a0e
commit 2ab02dc838
2 changed files with 92 additions and 19 deletions

View file

@ -60,27 +60,22 @@ class CartProvider implements CartProviderInterface
*/
public function getCart()
{
if (null == $this->cart) {
if (null !== $this->cart) {
return $this->cart;
}
$cartIdentifier = $this->storage->getCurrentCartIdentifier();
if ($cartIdentifier) {
$cart = $this->getCartByIdentifier($cartIdentifier);
if ($cart) {
$this->cart = $cart;
return $cart;
}
if ($cartIdentifier && $cart = $this->getCartByIdentifier($cartIdentifier)) {
return $this->cart = $cart;
}
$cart = $this->cartManager->create();
$this->cartManager->persist($cart);
$this->storage->setCurrentCartIdentifier($cart);
$this->cart = $cart;
}
$this->setCart($cart);
return $this->cart;
return $cart;
}
/**

View file

@ -0,0 +1,78 @@
<?php
namespace spec\Sylius\Bundle\CartBundle\Provider;
use PHPSpec2\ObjectBehavior;
/**
* Cart provider spec.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class CartProvider extends ObjectBehavior
{
/**
* @param Sylius\Bundle\CartBundle\Storage\CartStorageInterface $cartStorage
* @param Sylius\Bundle\ResourceBundle\Manager\ResourceManagerInterface $cartManager
*/
function let($cartStorage, $cartManager)
{
$this->beConstructedWith($cartStorage, $cartManager);
}
function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Bundle\CartBundle\Provider\CartProvider');
}
function it_should_reset_current_cart_identifier_when_abandoning_cart($cartStorage)
{
$cartStorage->resetCurrentCartIdentifier()->shouldBeCalled();
$this->abandonCart();
}
/**
* @param Sylius\Bundle\CartBundle\Model\CartInterface $cart
*/
function it_should_set_current_cart_identifier_when_setting_cart($cartStorage, $cart)
{
$cartStorage->setCurrentCartIdentifier($cart)->shouldBeCalled();
$this->setCart($cart);
}
/**
* @param Sylius\Bundle\CartBundle\Model\CartInterface $cart
*/
function it_should_look_for_cart_by_identifier_if_any($cartStorage, $cartManager, $cart)
{
$cartStorage->getCurrentCartIdentifier()->willReturn(3);
$cartManager->find(3)->shouldBeCalled()->willReturn($cart);
$this->getCart()->shouldReturn($cart);
}
/**
* @param Sylius\Bundle\CartBundle\Model\CartInterface $cart
*/
function it_should_create_new_cart_if_there_is_no_identifier_in_storage($cartStorage, $cartManager, $cart)
{
$cartStorage->getCurrentCartIdentifier()->willReturn(null);
$cartManager->create()->willReturn($cart);
$this->getCart()->shouldReturn($cart);
}
/**
* @param Sylius\Bundle\CartBundle\Model\CartInterface $cart
*/
function it_should_create_new_cart_if_identifier_is_wrong($cartStorage, $cartManager, $cart)
{
$cartStorage->getCurrentCartIdentifier()->willReturn(7);
$cartManager->find(7)->shouldBeCalled()->willReturn(null);
$cartManager->create()->willReturn($cart);
$this->getCart()->shouldReturn($cart);
}
}