Session cart storage should have its spec

This commit is contained in:
Paweł Jędrzejewski 2012-11-01 23:50:44 +01:00
parent 6e2c1bdb4f
commit c392203a0e
2 changed files with 67 additions and 4 deletions

View file

@ -22,6 +22,8 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
*/ */
class SessionCartStorage implements CartStorageInterface class SessionCartStorage implements CartStorageInterface
{ {
const KEY = '_sylius.cart-id';
/** /**
* Session. * Session.
* *
@ -29,14 +31,23 @@ class SessionCartStorage implements CartStorageInterface
*/ */
protected $session; protected $session;
/**
* Key to store the cart id in session.
*
* @var string
*/
protected $key;
/** /**
* Constructor. * Constructor.
* *
* @param SessionInterface $session * @param SessionInterface $session
* @param string $key
*/ */
public function __construct(SessionInterface $session) public function __construct(SessionInterface $session, $key = self::KEY)
{ {
$this->session = $session; $this->session = $session;
$this->key = $key;
} }
/** /**
@ -44,7 +55,7 @@ class SessionCartStorage implements CartStorageInterface
*/ */
public function getCurrentCartIdentifier() public function getCurrentCartIdentifier()
{ {
return $this->session->get('_sylius.cart-id'); return $this->session->get($this->key);
} }
/** /**
@ -52,7 +63,7 @@ class SessionCartStorage implements CartStorageInterface
*/ */
public function setCurrentCartIdentifier(CartInterface $cart) public function setCurrentCartIdentifier(CartInterface $cart)
{ {
$this->session->set('_sylius.cart-id', $cart->getId()); $this->session->set($this->key, $cart->getId());
} }
/** /**
@ -60,6 +71,6 @@ class SessionCartStorage implements CartStorageInterface
*/ */
public function resetCurrentCartIdentifier() public function resetCurrentCartIdentifier()
{ {
$this->session->remove('_sylius.cart-id'); $this->session->remove($this->key);
} }
} }

View file

@ -0,0 +1,52 @@
<?php
namespace spec\Sylius\Bundle\CartBundle\Storage;
use PHPSpec2\ObjectBehavior;
use Sylius\Bundle\CartBundle\Storage\SessionCartStorage as SessionCartStorageObject;
/**
* Session cart storage spec.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
class SessionCartStorage extends ObjectBehavior
{
/**
* @param Symfony\Component\HttpFoundation\Session\SessionInterface $session
*/
function let($session)
{
$this->beConstructedWith($session);
}
function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Bundle\CartBundle\Storage\SessionCartStorage');
}
function it_should_retrieve_cart_id_via_session($session)
{
$session->get(SessionCartStorageObject::KEY)->willReturn(7);
$this->getCurrentCartIdentifier()->shouldReturn(7);
}
/**
* @param Sylius\Bundle\CartBundle\Model\CartInterface $cart
*/
function it_should_set_cart_id_via_session($session, $cart)
{
$cart->getId()->willReturn(3);
$session->set(SessionCartStorageObject::KEY, 3)->shouldBeCalled();
$this->setCurrentCartIdentifier($cart);
}
function it_should_remove_the_saved_id_from_session_on_reset($session)
{
$session->remove(SessionCartStorageObject::KEY)->shouldBeCalled();
$this->resetCurrentCartIdentifier();
}
}