Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace App\Domain\Webshop;
  5.  
  6. use Money\Money;
  7. use Ramsey\Uuid\Uuid;
  8.  
  9. class Cart implements CartInterface
  10. {
  11.     private $uuid;
  12.     /**
  13.      * @var CartItem[]
  14.      */
  15.     private $cartItems;
  16.  
  17.     public function __construct()
  18.     {
  19.         $this->uuid = Uuid::uuid4()->toString();
  20.         $this->clear();
  21.     }
  22.  
  23.     /**
  24.      * @return int
  25.      */
  26.     public function countItems(): int
  27.     {
  28.         return count($this->cartItems);
  29.     }
  30.  
  31.     /**
  32.      * @return string
  33.      */
  34.     public function getId(): string
  35.     {
  36.         return $this->uuid;
  37.     }
  38.  
  39.     /**
  40.      * @param CartItem $cartItem
  41.      */
  42.     public function add(CartItem $cartItem): void
  43.     {
  44.         $this->cartItems[] = $cartItem;
  45.     }
  46.  
  47.     public function clear(): void
  48.     {
  49.         $this->cartItems = [];
  50.     }
  51.  
  52.     public function getTotalQuantity()
  53.     {
  54.         $totalQuantity = 0;
  55.         foreach ($this->cartItems as $cartItem) {
  56.             $totalQuantity += $cartItem->getQuantity()->asInt();
  57.         }
  58.         return $totalQuantity;
  59.     }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. class CartItem implements CartItemInterface
  66. {
  67.     private $name;
  68.     private $quantity;
  69.     private $price;
  70.  
  71.     public function __construct(CartItemName $cartItemName, CartItemQuantity $cartItemQuantity, CartItemUnitPrice $cartItemUnitPrice)
  72.     {
  73.         $this->name = $cartItemName;
  74.         $this->quantity = $cartItemQuantity;
  75.         $this->price = $cartItemUnitPrice;
  76.     }
  77.  
  78.     public function getName(): CartItemName
  79.     {
  80.         return $this->name;
  81.     }
  82.  
  83.     public function getQuantity(): CartItemQuantity
  84.     {
  85.         return $this->quantity;
  86.     }
  87.  
  88.     public function getTotalPrice(): Money
  89.     {
  90.         return $this->price->asMoney()->multiply($this->quantity->asInt());
  91.     }
  92. }
  93.  
  94.  
  95.  
  96. class CartSpec extends ObjectBehavior
  97. {
  98.     function it_is_initializable()
  99.     {
  100.         $this->shouldHaveType(Cart::class);
  101.         $this->shouldImplement(CartInterface::class);
  102.     }
  103.  
  104.     function it_returns_total_quantity_in_cart(CartItem $cartItem1, CartItem $cartItem2, CartItemName $cartItemName, CartItemQuantity $cartItemQuantity, CartItemUnitPrice $cartItemUnitPrice)
  105.     {
  106. //        $cartItemQuantity->asInt()->willReturn(3);
  107. //        $cartItem1->beConstructedWith([$cartItemName->getWrappedObject(), $cartItemQuantity->getWrappedObject(), $cartItemUnitPrice->getWrappedObject()]);
  108.         $cartItemQuantity = new CartItemQuantity(3);
  109.         $cartItem1->getQuantity()->willReturn($cartItemQuantity);
  110.         $this->add($cartItem1);
  111.         $this->add($cartItem2);
  112.         $this->getTotalQuantity()->shouldReturn(3);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement