[Behat][API] new scenario covered

This commit is contained in:
Ernest Warwas 2022-05-17 12:24:42 +02:00
parent 163e76c7de
commit e7dbbe1da4
No known key found for this signature in database
GPG key ID: 53E6A337C5C09665
9 changed files with 87 additions and 11 deletions

View file

@ -24,6 +24,20 @@ Feature: Order promotions integrity
Then I should be informed that this promotion is no longer applied
And I should not see the thank you page
@ui @api
Scenario: Preventing customer from completing checkout with already expired promotion
Given this promotion gives "$10.00" discount to every order
And this promotion expires tomorrow
And I added product "PHP T-Shirt" to the cart
And I have specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Offline" payment method
And this promotion has already expired
When I try to confirm my order
Then I should be informed that this promotion is no longer applied
And I should not see the thank you page
When I confirm my order
Then I should see the thank you page
@ui @api
Scenario: Being able to completing checkout with several promotions
Given this promotion gives "12%" discount to every order

View file

@ -1010,7 +1010,7 @@ final class CheckoutContext implements Context
*/
public function iShouldNotSeeTheThankYouPage(): void
{
Assert::same($this->client->getLastResponse()->getStatusCode(), 422);
Assert::notSame($this->client->getLastResponse()->getStatusCode(), 200);
}
/**
@ -1158,7 +1158,7 @@ final class CheckoutContext implements Context
{
Assert::contains(
$this->client->getLastResponse()->getContent(),
sprintf('Order is no longer eligible for this promotion %s.', $promotion->getName())
'Order is no longer eligible for one of applied promotions. Your cart was recalculated.'
);
}

View file

@ -14,18 +14,18 @@ declare(strict_types=1);
namespace Sylius\Bundle\ApiBundle\Checker;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Bundle\ApiBundle\Exception\OrderNoLongerEligibleForPromotion;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
/** @experimental */
final class OrderIntegrityChecker implements OrderIntegrityCheckerInterface
{
public function __construct(private OrderProcessorInterface $orderProcessor)
{
public function __construct(
private OrderProcessorInterface $orderProcessor,
) {
}
public function check(OrderInterface $order): void
public function check(OrderInterface $order): bool
{
$previousPromotions = new ArrayCollection($order->getPromotions()->toArray());
@ -33,8 +33,10 @@ final class OrderIntegrityChecker implements OrderIntegrityCheckerInterface
foreach ($previousPromotions as $previousPromotion) {
if (!$order->getPromotions()->contains($previousPromotion)) {
throw new OrderNoLongerEligibleForPromotion($previousPromotion->getName());
return false;
}
}
return true;
}
}

View file

@ -18,5 +18,5 @@ use Sylius\Component\Core\Model\OrderInterface;
/** @experimental */
interface OrderIntegrityCheckerInterface
{
public function check(OrderInterface $order): void;
public function check(OrderInterface $order): bool;
}

View file

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ApiBundle\Command\Cart;
/** @experimental */
class InformAboutCartRecalculate
{
public function __construct()
{
}
}

View file

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ApiBundle\CommandHandler\Cart;
use Sylius\Bundle\ApiBundle\Command\Cart\InformAboutCartRecalculate;
use Sylius\Bundle\ApiBundle\Exception\OrderNoLongerEligibleForPromotion;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class InformAboutCartRecalculateHandler implements MessageHandlerInterface
{
public function __invoke(InformAboutCartRecalculate $command): void
{
throw new OrderNoLongerEligibleForPromotion();
}
}

View file

@ -15,6 +15,7 @@ namespace Sylius\Bundle\ApiBundle\CommandHandler\Checkout;
use SM\Factory\FactoryInterface;
use Sylius\Bundle\ApiBundle\Checker\OrderIntegrityCheckerInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\InformAboutCartRecalculate;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Component\Core\Model\OrderInterface;
@ -31,6 +32,7 @@ final class CompleteOrderHandler implements MessageHandlerInterface
public function __construct(
private OrderRepositoryInterface $orderRepository,
private FactoryInterface $stateMachineFactory,
private MessageBusInterface $commandBus,
private MessageBusInterface $eventBus,
private OrderIntegrityCheckerInterface $orderIntegrityChecker
) {
@ -50,7 +52,11 @@ final class CompleteOrderHandler implements MessageHandlerInterface
$cart->setNotes($completeOrder->notes);
}
$this->orderIntegrityChecker->check($cart);
if (!$this->orderIntegrityChecker->check($cart)) {
$this->commandBus->dispatch(new InformAboutCartRecalculate(), [new DispatchAfterCurrentBusStamp()]);
return $cart;
}
$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

View file

@ -16,8 +16,8 @@ namespace Sylius\Bundle\ApiBundle\Exception;
/** @experimental */
final class OrderNoLongerEligibleForPromotion extends \RuntimeException
{
public function __construct(string $promotionName)
public function __construct()
{
parent::__construct(sprintf('Order is no longer eligible for this promotion %s.', $promotionName));
parent::__construct('Order is no longer eligible for one of applied promotions. Your cart was recalculated.');
}
}

View file

@ -57,6 +57,11 @@
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Cart\InformAboutCartRecalculateHandler">
<tag name="messenger.message_handler" bus="sylius.command_bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\UpdateCartHandler">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Modifier\OrderAddressModifierInterface"/>
@ -90,6 +95,7 @@
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\CompleteOrderHandler">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.command_bus" />
<argument type="service" id="sylius.event_bus" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Checker\OrderIntegrityCheckerInterface" />
<tag name="messenger.message_handler" bus="sylius.command_bus" />