From e4fbb72f1484960c9ce622279c85d3e1bcd13d6b Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 19 Jun 2024 10:51:00 +0200 Subject: [PATCH] PR review fixes --- src/Checker/PostUpdateChangesChecker.php | 4 ++-- .../PostUpdateChangesCheckerInterface.php | 7 ++---- .../SetonoSyliusOrderEditExtension.php | 8 +++++++ src/Entity/EditableOrderInterface.php | 6 ++++- src/Entity/EditableOrderTrait.php | 18 +++++++++++++- .../InitialTotalAwareOrderInterface.php | 12 ---------- src/Entity/InitialTotalAwareOrderTrait.php | 24 ------------------- ...rTotalChanged.php => PaidOrderUpdated.php} | 2 +- .../config/services/order_processing.xml | 2 +- src/Updated/OrderUpdater.php | 4 ++-- src/Updated/OrderUpdaterInterface.php | 3 +-- tests/Application/src/Entity/Order.php | 2 -- tests/Functional/OrderUpdateTest.php | 3 +-- .../Checker/PostUpdateChangesCheckerTest.php | 8 +++---- tests/Unit/Updater/OrderUpdaterTest.php | 8 +++---- 15 files changed, 48 insertions(+), 63 deletions(-) delete mode 100644 src/Entity/InitialTotalAwareOrderInterface.php delete mode 100644 src/Entity/InitialTotalAwareOrderTrait.php rename src/Event/{PaidOrderTotalChanged.php => PaidOrderUpdated.php} (85%) diff --git a/src/Checker/PostUpdateChangesChecker.php b/src/Checker/PostUpdateChangesChecker.php index 31a6376..29ea558 100644 --- a/src/Checker/PostUpdateChangesChecker.php +++ b/src/Checker/PostUpdateChangesChecker.php @@ -4,13 +4,13 @@ namespace Setono\SyliusOrderEditPlugin\Checker; -use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface; +use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException; use Sylius\Component\Core\Model\OrderInterface; final class PostUpdateChangesChecker implements PostUpdateChangesCheckerInterface { - public function check(InitialTotalAwareOrderInterface $previousOrder, OrderInterface $newOrder): void + public function check(EditableOrderInterface $previousOrder, OrderInterface $newOrder): void { if ($newOrder->getTotal() > $previousOrder->getInitialTotal()) { throw new NewOrderWrongTotalException(); diff --git a/src/Checker/PostUpdateChangesCheckerInterface.php b/src/Checker/PostUpdateChangesCheckerInterface.php index b9252f7..58e5342 100644 --- a/src/Checker/PostUpdateChangesCheckerInterface.php +++ b/src/Checker/PostUpdateChangesCheckerInterface.php @@ -4,13 +4,10 @@ namespace Setono\SyliusOrderEditPlugin\Checker; -use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface; +use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Sylius\Component\Core\Model\OrderInterface; interface PostUpdateChangesCheckerInterface { - public function check( - InitialTotalAwareOrderInterface $previousOrder, - OrderInterface $newOrder, - ): void; + public function check(EditableOrderInterface $previousOrder, OrderInterface $newOrder): void; } diff --git a/src/DependencyInjection/SetonoSyliusOrderEditExtension.php b/src/DependencyInjection/SetonoSyliusOrderEditExtension.php index b67041c..d760e20 100644 --- a/src/DependencyInjection/SetonoSyliusOrderEditExtension.php +++ b/src/DependencyInjection/SetonoSyliusOrderEditExtension.php @@ -58,6 +58,14 @@ public function prepend(ContainerBuilder $container): void 'templates' => [ 'action' => [ 'edit_order' => '@SetonoSyliusOrderEditPlugin/admin/order/grid/editOrder.html.twig', + ], + ], + ]); + + $container->prependExtensionConfig('framework', [ + 'messenger' => [ + 'buses' => [ + 'setono_sylius_order_edit.event_bus' => null, ], ], ]); diff --git a/src/Entity/EditableOrderInterface.php b/src/Entity/EditableOrderInterface.php index 86945c0..3fc0df0 100644 --- a/src/Entity/EditableOrderInterface.php +++ b/src/Entity/EditableOrderInterface.php @@ -6,7 +6,11 @@ use Sylius\Component\Core\Model\OrderInterface; -interface EditableOrderInterface extends OrderInterface, InitialTotalAwareOrderInterface +interface EditableOrderInterface extends OrderInterface { public function isAlreadyPaid(): bool; + + public function getInitialTotal(): int; + + public function setInitialTotal(int $initialTotal): void; } diff --git a/src/Entity/EditableOrderTrait.php b/src/Entity/EditableOrderTrait.php index f242d6d..8338ad8 100644 --- a/src/Entity/EditableOrderTrait.php +++ b/src/Entity/EditableOrderTrait.php @@ -4,13 +4,29 @@ namespace Setono\SyliusOrderEditPlugin\Entity; +use Doctrine\ORM\Mapping as ORM; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\OrderPaymentStates; -/** @method getPaymentState() */ +/** @mixin OrderInterface */ trait EditableOrderTrait { + /** @ORM\Column(type="integer") */ + #[ORM\Column(type: 'integer')] + private int $initialTotal = 0; + public function isAlreadyPaid(): bool { return $this->getPaymentState() === OrderPaymentStates::STATE_PAID; } + + public function getInitialTotal(): int + { + return $this->initialTotal; + } + + public function setInitialTotal(int $initialTotal): void + { + $this->initialTotal = $initialTotal; + } } diff --git a/src/Entity/InitialTotalAwareOrderInterface.php b/src/Entity/InitialTotalAwareOrderInterface.php deleted file mode 100644 index 8adcc43..0000000 --- a/src/Entity/InitialTotalAwareOrderInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -initialTotal; - } - - public function setInitialTotal(int $initialTotal): void - { - $this->initialTotal = $initialTotal; - } -} diff --git a/src/Event/PaidOrderTotalChanged.php b/src/Event/PaidOrderUpdated.php similarity index 85% rename from src/Event/PaidOrderTotalChanged.php rename to src/Event/PaidOrderUpdated.php index c2cee71..18fdbb8 100644 --- a/src/Event/PaidOrderTotalChanged.php +++ b/src/Event/PaidOrderUpdated.php @@ -4,7 +4,7 @@ namespace Setono\SyliusOrderEditPlugin\Event; -final class PaidOrderTotalChanged +final class PaidOrderUpdated { public function __construct(public int $orderId, public int $oldTotal, public int $newTotal) { diff --git a/src/Resources/config/services/order_processing.xml b/src/Resources/config/services/order_processing.xml index b26fa67..14e501b 100644 --- a/src/Resources/config/services/order_processing.xml +++ b/src/Resources/config/services/order_processing.xml @@ -55,7 +55,7 @@ - + diff --git a/src/Updated/OrderUpdater.php b/src/Updated/OrderUpdater.php index b0afa83..1211475 100644 --- a/src/Updated/OrderUpdater.php +++ b/src/Updated/OrderUpdater.php @@ -8,7 +8,7 @@ use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface; use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Setono\SyliusOrderEditPlugin\Event\OrderUpdated; -use Setono\SyliusOrderEditPlugin\Event\PaidOrderTotalChanged; +use Setono\SyliusOrderEditPlugin\Event\PaidOrderUpdated; use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface; use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface; use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface; @@ -40,7 +40,7 @@ public function update(Request $request, int $orderId): void $this->eventBus->dispatch(new OrderUpdated($orderId)); if ($updatedOrder->isAlreadyPaid()) { - $this->eventBus->dispatch(new PaidOrderTotalChanged($orderId, $oldOrder->getTotal(), $updatedOrder->getTotal())); + $this->eventBus->dispatch(new PaidOrderUpdated($orderId, $oldOrder->getTotal(), $updatedOrder->getTotal())); } } } diff --git a/src/Updated/OrderUpdaterInterface.php b/src/Updated/OrderUpdaterInterface.php index da9e4c2..9fb2f96 100644 --- a/src/Updated/OrderUpdaterInterface.php +++ b/src/Updated/OrderUpdaterInterface.php @@ -11,8 +11,7 @@ interface OrderUpdaterInterface { /** * @throws NewOrderWrongTotalException - * - * @throw \InvalidArgumentException + * @throws \InvalidArgumentException */ public function update(Request $request, int $orderId): void; } diff --git a/tests/Application/src/Entity/Order.php b/tests/Application/src/Entity/Order.php index aa45ffc..76fbfba 100644 --- a/tests/Application/src/Entity/Order.php +++ b/tests/Application/src/Entity/Order.php @@ -7,12 +7,10 @@ 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; } diff --git a/tests/Functional/OrderUpdateTest.php b/tests/Functional/OrderUpdateTest.php index cbed8de..74d4097 100644 --- a/tests/Functional/OrderUpdateTest.php +++ b/tests/Functional/OrderUpdateTest.php @@ -6,7 +6,6 @@ use Doctrine\ORM\EntityManagerInterface; use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; -use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface; use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes; use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart; use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart; @@ -194,7 +193,7 @@ public function testItAllowsToAddAndRemoveDiscountsForTheOrderItemMultipleTimes( private function placeOrderProgramatically( string $variantCode = '000F_office_grey_jeans-variant-0', int $quantity = 1, - ): Order|InitialTotalAwareOrderInterface { + ): EditableOrderInterface { /** @var MessageBusInterface $commandBus */ $commandBus = self::getContainer()->get('sylius.command_bus'); diff --git a/tests/Unit/Checker/PostUpdateChangesCheckerTest.php b/tests/Unit/Checker/PostUpdateChangesCheckerTest.php index 30c719d..ef3bb59 100644 --- a/tests/Unit/Checker/PostUpdateChangesCheckerTest.php +++ b/tests/Unit/Checker/PostUpdateChangesCheckerTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesChecker; -use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface; +use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException; use Sylius\Component\Core\Model\OrderInterface; @@ -24,7 +24,7 @@ public function testItThrowsExceptionIfNewOrderTotalIsBiggerThanThePreviousOne() $newOrder = $this->prophesize(OrderInterface::class); $newOrder->getTotal()->willReturn(1000); - $previousOrder = $this->prophesize(InitialTotalAwareOrderInterface::class); + $previousOrder = $this->prophesize(EditableOrderInterface::class); $previousOrder->getInitialTotal()->willReturn(500); $validator->check($previousOrder->reveal(), $newOrder->reveal()); @@ -37,7 +37,7 @@ public function testItDoesNothingIfNewOrderTotalIsSmallerThanThePreviousOne(): v $newOrder = $this->prophesize(OrderInterface::class); $newOrder->getTotal()->willReturn(500); - $previousOrder = $this->prophesize(InitialTotalAwareOrderInterface::class); + $previousOrder = $this->prophesize(EditableOrderInterface::class); $previousOrder->getInitialTotal()->willReturn(1000); $this->expectNotToPerformAssertions(); @@ -52,7 +52,7 @@ public function testItDoesNothingIfNewOrderTotalIsEqualToThePreviousOne(): void $newOrder = $this->prophesize(OrderInterface::class); $newOrder->getTotal()->willReturn(500); - $previousOrder = $this->prophesize(InitialTotalAwareOrderInterface::class); + $previousOrder = $this->prophesize(EditableOrderInterface::class); $previousOrder->getInitialTotal()->willReturn(500); $this->expectNotToPerformAssertions(); diff --git a/tests/Unit/Updater/OrderUpdaterTest.php b/tests/Unit/Updater/OrderUpdaterTest.php index c231f14..cb2cb06 100644 --- a/tests/Unit/Updater/OrderUpdaterTest.php +++ b/tests/Unit/Updater/OrderUpdaterTest.php @@ -11,7 +11,7 @@ use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface; use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Setono\SyliusOrderEditPlugin\Event\OrderUpdated; -use Setono\SyliusOrderEditPlugin\Event\PaidOrderTotalChanged; +use Setono\SyliusOrderEditPlugin\Event\PaidOrderUpdated; use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface; use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface; use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface; @@ -62,7 +62,7 @@ public function testItUpdatesOrder(): void $orderUpdater->update($request->reveal(), 1); } - public function testItDispatchesAdditionalEventIfOrderWasAlreadyPaid(): void + public function testItDispatchesAdditionalEventIfOrderWasAlreadyPaidAndTheTotalChanged(): void { $request = $this->prophesize(Request::class); $orderPreparer = $this->prophesize(OrderPreparerInterface::class); @@ -99,8 +99,8 @@ public function testItDispatchesAdditionalEventIfOrderWasAlreadyPaid(): void ->shouldBeCalled() ; $eventBus - ->dispatch(new PaidOrderTotalChanged(1, 1000, 900)) - ->willReturn(new Envelope(Argument::type(PaidOrderTotalChanged::class))) + ->dispatch(new PaidOrderUpdated(1, 1000, 900)) + ->willReturn(new Envelope(Argument::type(PaidOrderUpdated::class))) ->shouldBeCalled() ;