Skip to content

Commit

Permalink
Introduce additional method to check if order is already paid
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 19, 2024
1 parent 2d63885 commit 3613261
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/Entity/EditableOrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

interface EditableOrderInterface extends OrderInterface, InitialTotalAwareOrderInterface
{
public function isAlreadyPaid(): bool;
}
16 changes: 16 additions & 0 deletions src/Entity/EditableOrderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Entity;

use Sylius\Component\Core\OrderPaymentStates;

/** @method getPaymentState() */
trait EditableOrderTrait
{
public function isAlreadyPaid(): bool
{
return $this->getPaymentState() === OrderPaymentStates::STATE_PAID;
}
}
8 changes: 4 additions & 4 deletions src/Preparer/OrderPreparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Setono\SyliusOrderEditPlugin\Preparer;

use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Sylius\Component\Core\Inventory\Operator\OrderInventoryOperatorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Webmozart\Assert\Assert;

Expand All @@ -17,11 +17,11 @@ public function __construct(
) {
}

public function prepareToUpdate(int $orderId): OrderInterface
public function prepareToUpdate(int $orderId): EditableOrderInterface
{
/** @var OrderInterface|null $order */
/** @var EditableOrderInterface|null $order */
$order = $this->orderRepository->find($orderId);
Assert::isInstanceOf($order, OrderInterface::class);
Assert::isInstanceOf($order, EditableOrderInterface::class);

$this->orderInventoryOperator->cancel($order);

Expand Down
4 changes: 2 additions & 2 deletions src/Preparer/OrderPreparerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Setono\SyliusOrderEditPlugin\Preparer;

use Sylius\Component\Core\Model\OrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;

interface OrderPreparerInterface
{
public function prepareToUpdate(int $orderId): OrderInterface;
public function prepareToUpdate(int $orderId): EditableOrderInterface;
}
5 changes: 3 additions & 2 deletions src/Provider/UpdatedOrderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\SyliusOrderEditPlugin\Provider;

use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Exception\OrderUpdateException;
use Sylius\Bundle\OrderBundle\Form\Type\OrderType;
use Sylius\Component\Core\Model\OrderInterface;
Expand All @@ -17,7 +18,7 @@ public function __construct(private readonly FormFactoryInterface $formFactory)
{
}

public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): OrderInterface
public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): EditableOrderInterface
{
$form = $this->formFactory->create(
OrderType::class,
Expand All @@ -33,7 +34,7 @@ public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request
}

$data = $form->getData();
Assert::isInstanceOf($data, OrderInterface::class);
Assert::isInstanceOf($data, EditableOrderInterface::class);

return $data;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Provider/UpdatedOrderProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Setono\SyliusOrderEditPlugin\Provider;

use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\HttpFoundation\Request;

interface UpdatedOrderProviderInterface
{
public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): OrderInterface;
public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): EditableOrderInterface;
}
7 changes: 3 additions & 4 deletions src/Updated/OrderUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Event\OrderUpdated;
use Setono\SyliusOrderEditPlugin\Event\PaidOrderTotalChanged;
use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface;
use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -31,7 +30,7 @@ public function __construct(
public function update(Request $request, int $orderId): void
{
$order = $this->oldOrderProvider->prepareToUpdate($orderId);
/** @var InitialTotalAwareOrderInterface $oldOrder */
/** @var EditableOrderInterface $oldOrder */
$oldOrder = clone $order;

$updatedOrder = $this->updatedOrderProvider->provideFromOldOrderAndRequest($order, $request);
Expand All @@ -40,7 +39,7 @@ public function update(Request $request, int $orderId): void
$this->entityManager->flush();

$this->eventBus->dispatch(new OrderUpdated($orderId));
if ($updatedOrder->getPaymentState() === OrderPaymentStates::STATE_PAID) {
if ($updatedOrder->isAlreadyPaid()) {
$this->eventBus->dispatch(new PaidOrderTotalChanged($orderId, $oldOrder->getTotal(), $updatedOrder->getTotal()));
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Application/src/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderTrait;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderTrait;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_order')]
class Order extends \Sylius\Component\Core\Model\Order implements EditableOrderInterface
{
use EditableOrderTrait;
use InitialTotalAwareOrderTrait;
}
10 changes: 5 additions & 5 deletions tests/Unit/Provider/OrderPreparerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparer;
use Sylius\Component\Core\Inventory\Operator\OrderInventoryOperatorInterface;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;

final class OrderPreparerTest extends TestCase
Expand All @@ -25,11 +25,11 @@ public function testItProvidesOrderAndCancelsItsStock(): void
$inventoryOperator->reveal(),
);

$order = new Order();
$orderRepository->find(1)->willReturn($order);
$inventoryOperator->cancel($order)->shouldBeCalled();
$order = $this->prophesize(EditableOrderInterface::class);
$orderRepository->find(1)->willReturn($order->reveal());
$inventoryOperator->cancel($order->reveal())->shouldBeCalled();

self::assertSame($order, $provider->prepareToUpdate(1));
self::assertSame($order->reveal(), $provider->prepareToUpdate(1));
}

public function testItThrowsExceptionIfOrderWithGivenIdDoesNotExist(): void
Expand Down
30 changes: 15 additions & 15 deletions tests/Unit/Provider/UpdatedOrderProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Exception\OrderUpdateException;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProvider;
use Sylius\Bundle\OrderBundle\Form\Type\OrderType;
use Sylius\Component\Core\Model\Order;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -24,19 +24,19 @@ public function testItProvidesUpdateOrderFromRequest(): void
$form = $this->prophesize(FormInterface::class);

$provider = new UpdatedOrderProvider($formFactory->reveal());
$order = new Order();
$newOrder = new Order();
$order = $this->prophesize(EditableOrderInterface::class);
$newOrder = $this->prophesize(EditableOrderInterface::class);
$request = new Request();

$formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$formFactory->create(OrderType::class, $order->reveal(), ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);

$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);

$form->getData()->willReturn($newOrder);
$form->getData()->willReturn($newOrder->reveal());

self::assertSame($newOrder, $provider->provideFromOldOrderAndRequest($order, $request));
self::assertSame($newOrder->reveal(), $provider->provideFromOldOrderAndRequest($order->reveal(), $request));
}

public function testItThrowsExceptionIfFormIsNotSubmitted(): void
Expand All @@ -45,17 +45,17 @@ public function testItThrowsExceptionIfFormIsNotSubmitted(): void
$form = $this->prophesize(FormInterface::class);

$provider = new UpdatedOrderProvider($formFactory->reveal());
$order = new Order();
$order = $this->prophesize(EditableOrderInterface::class);
$request = new Request();

$formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$formFactory->create(OrderType::class, $order->reveal(), ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);

$form->isSubmitted()->willReturn(false);

$this->expectException(OrderUpdateException::class);

$provider->provideFromOldOrderAndRequest($order, $request);
$provider->provideFromOldOrderAndRequest($order->reveal(), $request);
}

public function testItThrowsExceptionIfFormIsNotValid(): void
Expand All @@ -64,18 +64,18 @@ public function testItThrowsExceptionIfFormIsNotValid(): void
$form = $this->prophesize(FormInterface::class);

$provider = new UpdatedOrderProvider($formFactory->reveal());
$order = new Order();
$order = $this->prophesize(EditableOrderInterface::class);
$request = new Request();

$formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$formFactory->create(OrderType::class, $order->reveal(), ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);

$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);

$this->expectException(OrderUpdateException::class);

$provider->provideFromOldOrderAndRequest($order, $request);
$provider->provideFromOldOrderAndRequest($order->reveal(), $request);
}

public function testItThrowsExceptionIfFormDataIsNotOrder(): void
Expand All @@ -84,10 +84,10 @@ public function testItThrowsExceptionIfFormDataIsNotOrder(): void
$form = $this->prophesize(FormInterface::class);

$provider = new UpdatedOrderProvider($formFactory->reveal());
$order = new Order();
$order = $this->prophesize(EditableOrderInterface::class);
$request = new Request();

$formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$formFactory->create(OrderType::class, $order->reveal(), ['validation_groups' => 'sylius', 'csrf_protection' => false])->willReturn($form);
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);

$form->isSubmitted()->willReturn(true);
Expand All @@ -97,6 +97,6 @@ public function testItThrowsExceptionIfFormDataIsNotOrder(): void

$this->expectException(\InvalidArgumentException::class);

$provider->provideFromOldOrderAndRequest($order, $request);
$provider->provideFromOldOrderAndRequest($order->reveal(), $request);
}
}
14 changes: 6 additions & 8 deletions tests/Unit/Updater/OrderUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface;
use Setono\SyliusOrderEditPlugin\Updated\OrderUpdater;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -46,14 +44,14 @@ public function testItUpdatesOrder(): void
);

$order = $this->prophesize(EditableOrderInterface::class);
$updatedOrder = $this->prophesize(OrderInterface::class);
$updatedOrder = $this->prophesize(EditableOrderInterface::class);

$orderPreparer->prepareToUpdate(1)->willReturn($order);
$updatedOrderProvider->provideFromOldOrderAndRequest($order->reveal(), $request)->willReturn($updatedOrder);
$postUpdateChangesChecker->check(Argument::type(OrderInterface::class), $updatedOrder->reveal())->shouldBeCalled();
$postUpdateChangesChecker->check(Argument::type(EditableOrderInterface::class), $updatedOrder->reveal())->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();

$updatedOrder->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT);
$updatedOrder->isAlreadyPaid()->willReturn(false);

$eventBus
->dispatch(new OrderUpdated(1))
Expand Down Expand Up @@ -84,14 +82,14 @@ public function testItDispatchesAdditionalEventIfOrderWasAlreadyPaid(): void
);

$order = $this->prophesize(EditableOrderInterface::class);
$updatedOrder = $this->prophesize(OrderInterface::class);
$updatedOrder = $this->prophesize(EditableOrderInterface::class);

$orderPreparer->prepareToUpdate(1)->willReturn($order);
$updatedOrderProvider->provideFromOldOrderAndRequest($order->reveal(), $request)->willReturn($updatedOrder);
$postUpdateChangesChecker->check(Argument::type(OrderInterface::class), $updatedOrder->reveal())->shouldBeCalled();
$postUpdateChangesChecker->check(Argument::type(EditableOrderInterface::class), $updatedOrder->reveal())->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();

$updatedOrder->getPaymentState()->willReturn(OrderPaymentStates::STATE_PAID);
$updatedOrder->isAlreadyPaid()->willReturn(true);
$order->getTotal()->willReturn(1000);
$updatedOrder->getTotal()->willReturn(900);

Expand Down

0 comments on commit 3613261

Please sign in to comment.