function it_does_nothing_if_target_quantity_is_below_0($orderItemUnitFactory, OrderItemInterface $orderItem) { $orderItem->getQuantity()->willReturn(3); $orderItemUnitFactory->createForItem(Argument::any())->shouldNotBeCalled(); $orderItem->addUnit(Argument::any())->shouldNotBeCalled(); $orderItem->removeUnit(Argument::any())->shouldNotBeCalled(); $this->modify($orderItem, -10); }
/** * @param OrderInterface $order * @param OrderItemInterface $item */ private function resolveOrderItem(OrderInterface $order, OrderItemInterface $item) { foreach ($order->getItems() as $existingItem) { if ($item->equals($existingItem)) { $this->orderItemQuantityModifier->modify($existingItem, $existingItem->getQuantity() + $item->getQuantity()); return; } } $order->addItem($item); }
function it_changes_quantity_of_an_item_if_same_order_item_already_exists(OrderInterface $order, OrderItemInterface $existingItem, OrderItemInterface $newItem, OrderItemQuantityModifierInterface $orderItemQuantityModifier, OrderProcessorInterface $orderProcessor) { $order->getItems()->willReturn([$existingItem]); $newItem->equals($existingItem)->willReturn(true); $existingItem->getQuantity()->willReturn(2); $newItem->getQuantity()->willReturn(3); $order->addItem($existingItem)->shouldNotBeCalled(); $orderItemQuantityModifier->modify($existingItem, 5)->shouldBeCalled(); $orderProcessor->process($order)->shouldBeCalled(); $this->addToOrder($order, $newItem); }
/** * {@inheritdoc} */ public function modify(OrderItemInterface $orderItem, $targetQuantity) { $currentQuantity = $orderItem->getQuantity(); if (0 >= $targetQuantity || $currentQuantity === $targetQuantity) { return; } if ($targetQuantity < $currentQuantity) { $this->decreaseUnitsNumber($orderItem, $currentQuantity - $targetQuantity); } elseif ($targetQuantity > $currentQuantity) { $this->increaseUnitsNumber($orderItem, $targetQuantity - $currentQuantity); } }
function it_has_total_quantity(OrderItemInterface $orderItem1, OrderItemInterface $orderItem2) { $orderItem1->getQuantity()->willReturn(10); $orderItem1->setOrder($this)->shouldBeCalled(); $orderItem1->getTotal()->willReturn(500); $orderItem2->getQuantity()->willReturn(30); $orderItem2->setOrder($this)->shouldBeCalled(); $orderItem2->equals($orderItem1)->willReturn(false); $orderItem2->getTotal()->willReturn(1000); $this->addItem($orderItem1); $this->addItem($orderItem2); $this->getTotalQuantity()->shouldReturn(40); }
/** * {@inheritdoc} */ public function merge(OrderItemInterface $orderItem, $throwOnInvalid = true) { if ($throwOnInvalid && !$orderItem->equals($this)) { throw new \RuntimeException('Given item cannot be merged.'); } if ($this !== $orderItem) { $this->quantity += $orderItem->getQuantity(); } return $this; }
function it_merges_a_known_equal_item_without_calling_equals(OrderItemInterface $item) { $this->setQuantity(3); $item->getQuantity()->willReturn(7); $item->equals($this)->shouldNotBeCalled(); $this->merge($item, false); $this->getQuantity()->shouldReturn(10); }
function it_restricts_max_item_quantity_to_the_stated_limit(OrderItemQuantityModifierInterface $itemQuantityModifier, OrderItemInterface $orderItem) { $orderItem->getQuantity()->willReturn(0); $itemQuantityModifier->modify($orderItem, 1000)->shouldBeCalled(); $this->modify($orderItem, 9999); }