Exemplo n.º 1
0
 /**
  * @param array $options
  *
  * @return FormView
  */
 public function getItemFormView(array $options = [])
 {
     $cartItem = $this->orderItemFactory->createNew();
     $this->orderItemQuantityModifier->modify($cartItem, 1);
     $form = $this->formFactory->create('sylius_cart_item', $cartItem, $options);
     return $form->createView();
 }
Exemplo n.º 2
0
 /**
  * @param array $options
  *
  * @return FormView
  */
 public function getItemFormView(array $options = [])
 {
     /** @var OrderItemInterface $cartItem */
     $cartItem = $this->orderItemFactory->createNew();
     $this->orderItemQuantityModifier->modify($cartItem, 1);
     $form = $this->formFactory->create(CartItemType::class, $cartItem, $options);
     return $form->createView();
 }
Exemplo n.º 3
0
 function it_uses_given_options_when_creating_a_form(FactoryInterface $itemFactory, FormFactoryInterface $formFactory, OrderItemQuantityModifierInterface $orderItemQuantityModifier, FormInterface $form, FormView $formView, OrderItemInterface $item)
 {
     $itemFactory->createNew()->willReturn($item);
     $orderItemQuantityModifier->modify($item, 1)->shouldBeCalled();
     $formFactory->create('sylius_cart_item', $item, ['foo' => 'bar'])->willReturn($form);
     $form->createView()->willReturn($formView);
     $this->getItemFormView(['foo' => 'bar'])->shouldReturn($formView);
 }
Exemplo n.º 4
0
 /**
  * @param CartInterface $cart
  * @param CartItemInterface $item
  */
 private function resolveCartItem(CartInterface $cart, CartItemInterface $item)
 {
     foreach ($cart->getItems() as $existingItem) {
         if ($item->equals($existingItem)) {
             $this->orderItemQuantityModifier->modify($existingItem, $existingItem->getQuantity() + $item->getQuantity());
             return;
         }
     }
     $cart->addItem($item);
 }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
0
 /**
  * @param CartItemEvent $event
  */
 public function addItem(CartItemEvent $event)
 {
     $cart = $event->getCart();
     $item = $event->getItem();
     foreach ($cart->getItems() as $existingItem) {
         if ($item->equals($existingItem)) {
             $this->orderItemQuantityModifier->modify($existingItem, $existingItem->getQuantity() + $item->getQuantity());
             return;
         }
     }
     $cart->addItem($item);
 }
 function it_reverts_a_product(OrderItemQuantityModifierInterface $orderItemQuantityModifier, RepositoryInterface $variantRepository, FactoryInterface $itemFactory, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, PromotionInterface $promotion)
 {
     $variantRepository->find(500)->willReturn($variant);
     $itemFactory->createNew()->willReturn($item);
     $item->setUnitPrice(2)->willReturn($item);
     $item->setVariant($variant)->willReturn($item);
     $orderItemQuantityModifier->modify($item, 3)->shouldBeCalled();
     $item->equals($item)->willReturn(true);
     $item->setImmutable(true)->shouldBeCalled();
     $order->getItems()->willReturn([$item]);
     $order->removeItem($item)->shouldBeCalled();
     $this->revert($order, ['variant' => 500, 'quantity' => 3, 'price' => 2], $promotion);
 }
 /**
  * {@inheritdoc}
  */
 public function mapFormsToData($forms, &$data)
 {
     $formsOtherThanQuantity = [];
     foreach ($forms as $key => $form) {
         if ('quantity' === $form->getName()) {
             $targetQuantity = $form->getData();
             $this->orderItemQuantityModifier->modify($data, $targetQuantity);
             continue;
         }
         $formsOtherThanQuantity[] = $form;
     }
     if (!empty($formsOtherThanQuantity)) {
         $this->propertyPathDataMapper->mapFormsToData($formsOtherThanQuantity, $data);
     }
 }
Exemplo n.º 9
0
 function it_merges_cart_items_if_equal(OrderItemQuantityModifierInterface $orderItemQuantityModifier, CartItemEvent $event, CartInterface $cart, CartItemInterface $cartItem, Collection $items, OrderItemInterface $existingItem, \Iterator $iterator)
 {
     $event->getCart()->willReturn($cart);
     $event->getItem()->willReturn($cartItem);
     $cart->getItems()->willReturn($items);
     $items->getIterator()->willReturn($iterator);
     $iterator->rewind()->shouldBeCalled();
     $iterator->valid()->willReturn(true, false);
     $iterator->current()->willReturn($existingItem);
     $cartItem->equals($existingItem)->willReturn(true);
     $cartItem->getQuantity()->willReturn(3);
     $existingItem->getQuantity()->willReturn(1);
     $orderItemQuantityModifier->modify($existingItem, 4)->shouldBeCalled();
     $this->addItem($event);
 }
Exemplo n.º 10
0
 /**
  * @param array $configuration
  *
  * @return OrderItemInterface
  */
 protected function createItem(array $configuration)
 {
     $variant = $this->variantRepository->find($configuration['variant']);
     $promotionItem = $this->itemFactory->createNew();
     $promotionItem->setVariant($variant);
     $promotionItem->setUnitPrice((int) $configuration['price']);
     $this->orderItemQuantityModifier->modify($promotionItem, (int) $configuration['quantity']);
     return $promotionItem;
 }
Exemplo n.º 11
0
 /**
  * @param ProductVariantInterface $productVariant
  * @param int $price
  * @param int $quantity
  *
  * @return OrderInterface
  */
 private function addProductVariantToOrder(ProductVariantInterface $productVariant, $price, $quantity = 1)
 {
     $order = $this->sharedStorage->get('order');
     /** @var OrderItemInterface $item */
     $item = $this->orderItemFactory->createNew();
     $item->setVariant($productVariant);
     $item->setUnitPrice($productVariant->getPrice());
     $this->itemQuantityModifier->modify($item, $quantity);
     $order->addItem($item);
     return $order;
 }
Exemplo n.º 12
0
 /**
  * @param ProductVariantInterface $productVariant
  * @param int $price
  *
  * @return OrderInterface
  */
 private function addSingleProductVariantToOrder(ProductVariantInterface $productVariant, $price)
 {
     $order = $this->sharedStorage->get('order');
     /** @var OrderItemInterface $item */
     $item = $this->orderItemFactory->createNew();
     $item->setVariant($productVariant);
     $item->setUnitPrice($price);
     $this->itemQuantityModifier->modify($item, 1);
     $order->addItem($item);
     $this->orderRecalculator->recalculate($order);
     return $order;
 }
Exemplo n.º 13
0
 /**
  * @Given the customer bought single :product
  */
 public function theCustomerBoughtSingle(ProductInterface $product)
 {
     /** @var OrderInterface $order */
     $order = $this->sharedStorage->get('order');
     /** @var OrderItemInterface $item */
     $item = $this->orderItemFactory->createNew();
     $item->setVariant($product->getMasterVariant());
     $item->setUnitPrice($product->getPrice());
     $this->itemQuantityModifier->modify($item, 1);
     $order->addItem($item);
     $this->objectManager->flush();
 }
Exemplo n.º 14
0
 /**
  * @param OrderInterface $order
  */
 private function generateItems(OrderInterface $order)
 {
     $numberOfItems = rand(1, 5);
     $products = $this->productRepository->findAll();
     for ($i = 0; $i < $numberOfItems; $i++) {
         $item = $this->orderItemFactory->createNew();
         $product = $this->faker->randomElement($products);
         $variant = $this->faker->randomElement($product->getVariants()->toArray());
         $item->setVariant($variant);
         $this->orderItemQuantityModifier->modify($item, rand(1, 5));
         $order->addItem($item);
     }
 }
Exemplo n.º 15
0
 function it_adds_single_item_by_customer(FactoryInterface $orderItemFactory, OrderInterface $order, OrderItemInterface $item, OrderItemQuantityModifierInterface $itemQuantityModifier, ProductInterface $product, SharedStorageInterface $sharedStorage, ProductVariantInterface $variant, ObjectManager $objectManager)
 {
     $sharedStorage->get('order')->willReturn($order);
     $orderItemFactory->createNew()->willReturn($item);
     $product->getMasterVariant()->willReturn($variant);
     $product->getPrice()->willReturn(1234);
     $itemQuantityModifier->modify($item, 1)->shouldBeCalled();
     $item->setVariant($variant)->shouldBeCalled();
     $item->setUnitPrice(1234)->shouldBeCalled();
     $order->addItem($item)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $this->theCustomerBoughtSingle($product);
 }
 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);
 }
 /**
  * {@inheritdoc}
  */
 public function modify(OrderItemInterface $orderItem, $targetQuantity)
 {
     $targetQuantity = min($targetQuantity, $this->limit);
     $this->decoratedOrderItemQuantityModifier->modify($orderItem, $targetQuantity);
 }