Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace App\Domain\Webshop;
- use Money\Money;
- use Ramsey\Uuid\Uuid;
- class Cart implements CartInterface
- {
- private $uuid;
- /**
- * @var CartItem[]
- */
- private $cartItems;
- public function __construct()
- {
- $this->uuid = Uuid::uuid4()->toString();
- $this->clear();
- }
- /**
- * @return int
- */
- public function countItems(): int
- {
- return count($this->cartItems);
- }
- /**
- * @return string
- */
- public function getId(): string
- {
- return $this->uuid;
- }
- /**
- * @param CartItem $cartItem
- */
- public function add(CartItem $cartItem): void
- {
- $this->cartItems[] = $cartItem;
- }
- public function clear(): void
- {
- $this->cartItems = [];
- }
- public function getTotalQuantity()
- {
- $totalQuantity = 0;
- foreach ($this->cartItems as $cartItem) {
- $totalQuantity += $cartItem->getQuantity()->asInt();
- }
- return $totalQuantity;
- }
- }
- class CartItem implements CartItemInterface
- {
- private $name;
- private $quantity;
- private $price;
- public function __construct(CartItemName $cartItemName, CartItemQuantity $cartItemQuantity, CartItemUnitPrice $cartItemUnitPrice)
- {
- $this->name = $cartItemName;
- $this->quantity = $cartItemQuantity;
- $this->price = $cartItemUnitPrice;
- }
- public function getName(): CartItemName
- {
- return $this->name;
- }
- public function getQuantity(): CartItemQuantity
- {
- return $this->quantity;
- }
- public function getTotalPrice(): Money
- {
- return $this->price->asMoney()->multiply($this->quantity->asInt());
- }
- }
- class CartSpec extends ObjectBehavior
- {
- function it_is_initializable()
- {
- $this->shouldHaveType(Cart::class);
- $this->shouldImplement(CartInterface::class);
- }
- function it_returns_total_quantity_in_cart(CartItem $cartItem1, CartItem $cartItem2, CartItemName $cartItemName, CartItemQuantity $cartItemQuantity, CartItemUnitPrice $cartItemUnitPrice)
- {
- // $cartItemQuantity->asInt()->willReturn(3);
- // $cartItem1->beConstructedWith([$cartItemName->getWrappedObject(), $cartItemQuantity->getWrappedObject(), $cartItemUnitPrice->getWrappedObject()]);
- $cartItemQuantity = new CartItemQuantity(3);
- $cartItem1->getQuantity()->willReturn($cartItemQuantity);
- $this->add($cartItem1);
- $this->add($cartItem2);
- $this->getTotalQuantity()->shouldReturn(3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement