Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from bartoszpietrzak1994/i-want-to-be-informed…
Browse files Browse the repository at this point in the history
…-when-none-of-order-items-is-in-stock

I want to be informed when none of order items is in stock
  • Loading branch information
GSadee committed Jun 20, 2018
2 parents 31bd2c8 + 76cca52 commit b1bf876
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
10 changes: 5 additions & 5 deletions features/being_notified_about_changes_in_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ Feature: Being notified about changes in order
And I should see exactly 2 notifications

@ui
Scenario: Reordering previously placed order when several items are out of stock
Scenario: Reordering previously placed order when every item is out of stock
Given the product "Angel T-Shirt" is out of stock
And the product "Awesome Mug" is out of stock
When I browse my orders
And I click reorder button next to the order "#00000666"
Then I should be on my cart summary page
And I should be notified that products "Angel T-Shirt", "Awesome Mug" are out of stock
And I should be notified that previous order total was "$148.00"
And I should see exactly 2 notifications
Then I should not proceed to my cart summary page
And I should be notified that none of items from previously placed order is available
And I should see exactly 1 notification

@ui
Scenario: Reordering previously placed order when there is insufficient item's quantity in stock
Expand Down Expand Up @@ -76,3 +75,4 @@ Feature: Being notified about changes in order
And I should be notified that "Angel T-Shirt" price has changed
And I should be notified that previous order total was "$148.00"
And I should see exactly 2 notifications

27 changes: 24 additions & 3 deletions spec/Reorder/ReordererSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function it_creates_and_persists_reorder_from_existing_order(
ReorderEligibilityChecker $reorderEligibilityChecker,
ChannelInterface $channel,
OrderInterface $order,
OrderInterface $reorder
OrderInterface $reorder,
OrderItemInterface $firstOrderItem,
OrderItemInterface $secondOrderItem
): void {
$order->getTotal()->willReturn(100);
$order->getCurrencyCode()->willReturn('USD');
Expand All @@ -74,6 +76,11 @@ function it_creates_and_persists_reorder_from_existing_order(

$reorderEligibilityChecker->check($order, $reorder)->willReturn([]);

$reorder->getItems()->willReturn(new ArrayCollection([
$firstOrderItem->getWrappedObject(),
$secondOrderItem->getWrappedObject()
]));

$this->reorder($order, $channel);
}

Expand All @@ -86,7 +93,9 @@ function it_checks_if_orders_totals_differ(
OrderInterface $reorder,
MoneyFormatterInterface $moneyFormatter,
ArrayCollection $promotions,
ReorderEligibilityCheckerResponse $reorderEligibilityCheckerResponse
ReorderEligibilityCheckerResponse $reorderEligibilityCheckerResponse,
OrderItemInterface $firstOrderItem,
OrderItemInterface $secondOrderItem
): void {
$order->getTotal()->willReturn(100);
$order->getCurrencyCode()->willReturn('USD');
Expand All @@ -95,6 +104,11 @@ function it_checks_if_orders_totals_differ(
$reorder->getTotal()->willReturn(150);
$reorder->getPromotions()->willReturn($promotions);

$reorder->getItems()->willReturn(new ArrayCollection([
$firstOrderItem->getWrappedObject(),
$secondOrderItem->getWrappedObject()
]));

$moneyFormatter->format(100, 'USD')->willReturn('$1.00');

$reorderEligibilityCheckerResponse->getMessage()->willReturn(
Expand All @@ -121,7 +135,9 @@ function it_checks_if_promotion_is_no_longer_available(
MoneyFormatterInterface $moneyFormatter,
PromotionInterface $firstPromotion,
PromotionInterface $secondPromotion,
ReorderEligibilityCheckerResponse $reorderEligibilityCheckerResponse
ReorderEligibilityCheckerResponse $reorderEligibilityCheckerResponse,
OrderItemInterface $firstOrderItem,
OrderItemInterface $secondOrderItem
): void {
$order->getPromotions()->willReturn(new ArrayCollection([
$firstPromotion->getWrappedObject(),
Expand All @@ -133,6 +149,11 @@ function it_checks_if_promotion_is_no_longer_available(

$reorder->getPromotions()->willReturn(new ArrayCollection());

$reorder->getItems()->willReturn(new ArrayCollection([
$firstOrderItem->getWrappedObject(),
$secondOrderItem->getWrappedObject()
]));

$moneyFormatter->format(100, 'USD')->willReturn('$1.00');

$reorderEligibilityCheckerResponse->getMessage()->willReturn(
Expand Down
19 changes: 17 additions & 2 deletions src/Controller/CustomerReorderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\CustomerReorderPlugin\Controller;

use Nette\InvalidStateException;
use Sylius\Bundle\CoreBundle\Storage\CartSessionStorage;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class CustomerReorderAction
Expand All @@ -36,20 +38,25 @@ final class CustomerReorderAction
/** @var UrlGeneratorInterface */
private $urlGenerator;

/** @var Session */
private $session;

public function __construct(
CartSessionStorage $cartSessionStorage,
ChannelContextInterface $channelContext,
CartContextInterface $cartContext,
OrderRepositoryInterface $orderRepository,
ReordererInterface $reorderService,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
Session $session
) {
$this->cartSessionStorage = $cartSessionStorage;
$this->channelContext = $channelContext;
$this->cartContext = $cartContext;
$this->orderRepository = $orderRepository;
$this->reorderer = $reorderService;
$this->urlGenerator = $urlGenerator;
$this->session = $session;
}

public function __invoke(Request $request): Response
Expand All @@ -60,7 +67,15 @@ public function __invoke(Request $request): Response
$channel = $this->channelContext->getChannel();
assert($channel instanceof ChannelInterface);

$reorder = $this->reorderer->reorder($order, $channel);
$reorder = null;

try {
$reorder = $this->reorderer->reorder($order, $channel);
} catch (InvalidStateException $exception) {
$this->session->getFlashBag()->add('info', $exception->getMessage());
return new RedirectResponse($this->urlGenerator->generate('sylius_shop_account_order_index'));
}

assert($reorder instanceof OrderInterface);

$this->cartSessionStorage->setForChannel($channel, $reorder);
Expand Down
5 changes: 5 additions & 0 deletions src/Reorder/Reorderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\CustomerReorderPlugin\Reorder;

use Doctrine\ORM\EntityManagerInterface;
use Nette\InvalidStateException;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
Expand Down Expand Up @@ -60,6 +61,10 @@ public function reorder(OrderInterface $order, ChannelInterface $channel): Order
$reorder = $this->orderFactory->createFromExistingOrder($order, $channel);
assert($reorder instanceof OrderInterface);

if (empty($reorder->getItems()->getValues())) {
throw new InvalidStateException('sylius.reorder.none_of_items_is_available');
}

$reorderEligibilityChecks = $this->reorderEligibilityChecker->check($order, $reorder);
$this->reorderEligibilityCheckerResponseProcessor->process($reorderEligibilityChecks);

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="Sylius\CustomerReorderPlugin\Reorder\Reorderer" />
<argument type="service" id="router" />
<argument type="service" id="session" />
</service>
<service id="Sylius\CustomerReorderPlugin\Reorder\Reorderer">
<argument type="service" id="Sylius\CustomerReorderPlugin\Factory\OrderFactory" />
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/flashes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ sylius:
previous_order_total: 'Previous order total: %order_total%.'
promotion_not_enabled: 'Following promotions: %promotion_names% are no longer enabled, which have affected order total.'
insufficient_quantity: 'Following items: %order_items% are not available in expected quantity, which have affected order total.'
none_of_items_is_available: 'None of items from previously placed order is available. Unable to place reorder.'
17 changes: 16 additions & 1 deletion tests/Behat/Context/Reorder/ReorderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function iShouldBeNotifiedThatPromotionIsNoLongerEnabled(string $promotio
}

/**
* @Then I should see exactly :count notifications
* @Then I should see exactly :count notification(s)
*/
public function iShouldSeeExactlyNotifications(int $count): void
{
Expand All @@ -135,6 +135,21 @@ public function iShouldNotSeeAnyNotifications(): void
assert(count($this->session->getPage()->findAll('css', '.sylius-flash-message')) === 0);
}

/**
* @Then I should not proceed to my cart summary page
*/
public function iShouldNotProceedToMyCartSummaryPage(): void
{
assert(strpos($this->session->getCurrentUrl(), '/cart') === false);
}

/**
* @Then I should be notified that none of items from previously placed order is available
*/
public function iShouldBeNotifiedThatNoneOfItemsFromPreviouslyPlacedOrderIsAvailable(): void
{
$this->assertFlashMessageWithTextExists('None of items from previously placed order is available. Unable to place reorder.');
}

/**
* @Then /^I should have shipping address filled with (address "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/
Expand Down

0 comments on commit b1bf876

Please sign in to comment.