/** * @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); }
function it_merges_equal_items(OrderItemInterface $item1, OrderItemInterface $item2) { $item1->setOrder($this)->shouldBeCalled(); $item1->equals($item2)->willReturn(true); $item2->equals($item1)->willReturn(true); $item1->merge($item2, false)->willReturn($this)->shouldBeCalled(); $this->addItem($item1)->addItem($item2); $this->countItems()->shouldReturn(1); }
function it_calculates_correct_total_when_adjustment_is_bigger_than_cost(OrderItemInterface $item, AdjustmentInterface $adjustment) { $item->getTotal()->willReturn(45000); $item->equals(Argument::any())->willReturn(false); $item->setOrder($this)->shouldBeCalled(); $adjustment->isNeutral()->willReturn(false); $adjustment->getAmount()->willReturn(-100000); $adjustment->setAdjustable($this)->shouldBeCalled(); $this->addItem($item); $this->addAdjustment($adjustment); $this->getTotal()->shouldReturn(0); }
/** * {@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_throws_exception_when_merging_unequal_item(OrderItemInterface $item) { $item->equals($this)->willReturn(false); $this->shouldThrow(new \RuntimeException('Given item cannot be merged.'))->duringMerge($item); }