Created
November 5, 2014 10:16
-
-
Save Crinsane/112ed02e886af8dd8f8e to your computer and use it in GitHub Desktop.
PHPSpec test and classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Crinsane\ShoppingCart; | |
use Countable; | |
use Illuminate\Contracts\Events\Dispatcher; | |
use Symfony\Component\HttpFoundation\Session\SessionInterface; | |
class Cart implements Countable { | |
/** | |
* Implementation of the Symfony SessionInterface | |
* | |
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface | |
*/ | |
protected $session; | |
/** | |
* Implementation of the Illuminate Dispatcher contract | |
* | |
* @var \Illuminate\Contracts\Events\Dispatcher | |
*/ | |
protected $events; | |
/** | |
* The collection of items in the cart | |
* | |
* @var \Crinsane\ShoppingCart\CartItemCollection | |
*/ | |
protected $items; | |
/** | |
* Cart constructor | |
* | |
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session | |
* @param \Illuminate\Contracts\Events\Dispatcher $events | |
*/ | |
public function __construct(SessionInterface $session, Dispatcher $events) | |
{ | |
$this->session = $session; | |
$this->events = $events; | |
$this->items = new CartItemCollection(); | |
} | |
/** | |
* Add one or more cart items to the cart | |
* | |
* @param \Crinsane\ShoppingCart\CartItem $item | |
* @param int $qty | |
*/ | |
public function add(CartItem $item, $qty = 1) | |
{ | |
$rowId = $this->generateRowId($item); | |
$this->items->put($rowId, [ | |
'id' => $item->getId(), | |
'name' => $item->getName(), | |
'qty' => $qty, | |
'price' => $item->getPrice(), | |
'item' => $item | |
]); | |
} | |
/** | |
* Generate a unique rowId | |
* | |
* @param \Crinsane\ShoppingCart\CartItem $item | |
* @return string | |
*/ | |
protected function generateRowId(CartItem $item) | |
{ | |
return md5($item->getId() . serialize($item->getOptions())); | |
} | |
/** | |
* (PHP 5 >= 5.1.0)<br/> | |
* Count elements of an object | |
* | |
* @link http://php.net/manual/en/countable.count.php | |
* @return int The custom count as an integer. | |
* </p> | |
* <p> | |
* The return value is cast to an integer. | |
*/ | |
public function count() | |
{ | |
return $this->items->sum('qty'); | |
} | |
/** | |
* Count the number of rows in the collection | |
* | |
* @return int | |
*/ | |
public function rows() | |
{ | |
return $this->items->count(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Crinsane\ShoppingCart; | |
interface CartItem { | |
/** | |
* Get the unique ID of the item | |
* | |
* @return int|string | |
*/ | |
public function getId(); | |
/** | |
* Get the name of the item | |
* | |
* @return string | |
*/ | |
public function getName(); | |
/** | |
* Get the price of the item | |
* | |
* @return float | |
*/ | |
public function getPrice(); | |
/** | |
* Get an array of options | |
* | |
* @return array | |
*/ | |
public function getOptions(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace spec\Crinsane\ShoppingCart; | |
use Crinsane\ShoppingCart\CartItem; | |
use Illuminate\Contracts\Events\Dispatcher; | |
use PhpSpec\ObjectBehavior; | |
use Prophecy\Argument; | |
use Symfony\Component\HttpFoundation\Session\SessionInterface; | |
class CartSpec extends ObjectBehavior { | |
public function let(SessionInterface $session, Dispatcher $events) | |
{ | |
$session->beADoubleOf('Symfony\Component\HttpFoundation\Session\SessionInterface'); | |
$events->beADoubleOf('Illuminate\Contracts\Events\Dispatcher'); | |
$this->beConstructedWith($session, $events); | |
} | |
public function it_is_initializable() | |
{ | |
$this->shouldHaveType('Crinsane\ShoppingCart\Cart'); | |
} | |
public function it_stores_a_collection_of_cart_items(CartItem $cartItem) | |
{ | |
$this->add($cartItem, 1); | |
$this->shouldHaveCount(1); | |
} | |
public function it_can_add_multiple_instances_of_a_cart_item(CartItem $cartItem1, CartItem $cartItem2) | |
{ | |
$cartItem1->getId()->willReturn(1); | |
$cartItem1->getName()->willReturn(null); | |
$cartItem1->getPrice()->willReturn(null); | |
$cartItem1->getOptions()->willReturn([]); | |
$cartItem2->getId()->willReturn(2); | |
$cartItem2->getName()->willReturn(null); | |
$cartItem2->getPrice()->willReturn(null); | |
$cartItem2->getOptions()->willReturn([]); | |
$this->add($cartItem1); | |
$this->add($cartItem2); | |
$this->shouldHaveCount(2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment