/** * {@inheritdoc} */ public function isStockSufficient(StockableInterface $stockable, $quantity) { if (true === $this->backorders || $stockable->isAvailableOnDemand()) { return true; } return $quantity <= $stockable->getOnHand() - $stockable->getOnHold(); }
function it_partially_fills_backordered_units_and_updates_stock_accordingly(StockableInterface $stockable, InventoryUnitInterface $inventoryUnit1, InventoryUnitInterface $inventoryUnit2, ObjectRepository $repository) { $stockable->getOnHand()->shouldBeCalled()->willReturn(5); $stockable->setOnHand(3)->shouldBeCalled(); $inventoryUnit1->setInventoryState(InventoryUnitInterface::STATE_SOLD)->shouldBeCalled(); $inventoryUnit2->setInventoryState(InventoryUnitInterface::STATE_SOLD)->shouldBeCalled(); $repository->findBy(['stockable' => $stockable, 'inventoryState' => InventoryUnitInterface::STATE_BACKORDERED], ['createdAt' => 'ASC'])->willReturn([$inventoryUnit1, $inventoryUnit2]); $this->fillBackorders($stockable); }
/** * {@inheritdoc} */ public function release(StockableInterface $stockable, $quantity) { if ($quantity < 0) { throw new \InvalidArgumentException('Quantity of units must be greater than 0.'); } $this->eventDispatcher->dispatch(SyliusStockableEvents::PRE_RELEASE, new GenericEvent($stockable)); $stockable->setOnHold($stockable->getOnHold() - $quantity); $this->eventDispatcher->dispatch(SyliusStockableEvents::POST_RELEASE, new GenericEvent($stockable)); }
/** * {@inheritdoc} */ public function isStockSufficient(StockableInterface $stockable, $quantity) { if ($stockable instanceof SoftDeletableInterface && $stockable->isDeleted()) { return false; } if ($this->backorders || $stockable->isAvailableOnDemand()) { return true; } return $quantity <= $stockable->getOnHand() - $stockable->getOnHold(); }
function it_performs_kernel_exception_action_successfully(UrlGeneratorInterface $router, SessionInterface $session, TranslatorInterface $translator, GetResponseForExceptionEvent $event, FlashBagInterface $flashBag, StockableInterface $stockable) { $stockable->getOnHand()->willReturn('30'); $stockable->getInventoryName()->willReturn('Inventory Name'); $event->getException()->willReturn(new InsufficientStockException($stockable->getWrappedObject(), 42)); $event->setResponse(Argument::type(RedirectResponse::class))->shouldBeCalled(); $translator->trans('sylius.checkout.out_of_stock', ['%quantity%' => '30', '%name%' => 'Inventory Name'], 'flashes')->willReturn('message translated'); $flashBag->add('notice', 'message translated')->shouldBeCalled(); $session->getBag('flashes')->willReturn($flashBag); $router->generate('redirect_to_url')->willReturn('url'); $this->onKernelException($event); }
function it_performs_kernel_exception_action_successfully(UrlGeneratorInterface $router, SessionInterface $session, TranslatorInterface $translator, GetResponseForExceptionEvent $event, InsufficientStockException $exception, FlashBagInterface $flashBag, StockableInterface $stockable) { $stockable->getOnHand()->shouldBeCalled()->willReturn('30'); $stockable->getInventoryName()->shouldBeCalled()->willReturn('Inventory Name'); $exception->getStockable()->shouldBeCalledTimes(2)->willReturn($stockable); $event->getException()->shouldBeCalled()->willReturn($exception); $event->setResponse(Argument::type('Symfony\\Component\\HttpFoundation\\RedirectResponse'))->shouldBeCalled(); $translator->trans('sylius.checkout.out_of_stock', array('%quantity%' => '30', '%name%' => 'Inventory Name'), 'flashes')->shouldBeCalled()->willReturn('message translated'); $flashBag->add('notice', 'message translated')->shouldBeCalled(); $session->getBag('flashes')->shouldBeCalled()->willReturn($flashBag); $router->generate('redirect_to_url')->shouldBeCalled()->willReturn('url'); $this->onKernelException($event)->shouldReturn(null); }
/** * {@inheritdoc} */ public function fillBackorders(StockableInterface $stockable) { $onHand = $stockable->getOnHand(); if ($onHand <= 0) { return; } $units = $this->repository->findBy(array('stockable' => $stockable, 'inventoryState' => InventoryUnitInterface::STATE_BACKORDERED), array('createdAt' => 'ASC')); foreach ($units as $unit) { $unit->setInventoryState(InventoryUnitInterface::STATE_SOLD); if (--$onHand === 0) { break; } } $stockable->setOnHand($onHand); }
/** * @param StockableInterface $stockable * @param int $quantity * @param \Exception|null $previousException */ public function __construct(StockableInterface $stockable, $quantity, \Exception $previousException = null) { $this->stockable = $stockable; parent::__construct(sprintf('Only %d %s(s) on hand, %d requested.', $stockable->getOnHand(), $stockable->getInventoryName(), $quantity), 0, $previousException); }
function it_increases_stockable_on_hand(StockableInterface $stockable) { $stockable->getOnHand()->shouldBeCalled()->willReturn(2); $stockable->setOnHand(7)->shouldBeCalled(); $this->increase($stockable, 5); }
/** * {@inheritdoc} */ public function isStockSufficient(StockableInterface $stockable, $quantity) { return !$stockable->isTracked() || $quantity <= $stockable->getOnHand() - $stockable->getOnHold(); }
/** * {@inheritdoc} */ public function getInventoryName() { return $this->stockable->getInventoryName(); }
function it_recognizes_stock_sufficient_if_its_available_on_demand_and_backorders_are_disabled(StockableInterface $stockable) { $this->beConstructedWith(false); $stockable->isAvailableOnDemand()->willReturn(true); $stockable->getOnHand()->willReturn(0); $this->isStockSufficient($stockable, 999)->shouldReturn(true); $stockable->getOnHand()->willReturn(-5); $this->isStockSufficient($stockable, 3)->shouldReturn(true); }
function it_returns_its_stockable_name(StockableInterface $stockable) { $stockable->getInventoryName()->willReturn('[IPHONE5] iPhone 5'); $this->setStockable($stockable); $this->getInventoryName()->shouldReturn('[IPHONE5] iPhone 5'); }
function it_recognizes_stockable_as_available_or_sufficent_if_it_is_not_tracked(StockableInterface $stockable) { $stockable->isTracked()->willReturn(false); $this->isStockAvailable($stockable)->shouldReturn(true); $this->isStockSufficient($stockable, 42)->shouldReturn(true); }
function it_returns_its_stockable_sku(StockableInterface $stockable) { $stockable->getSku()->willReturn('IPHONE5'); $this->setStockable($stockable); $this->getSku()->shouldReturn('IPHONE5'); }