diff --git a/src/Entity/EditableOrderInterface.php b/src/Entity/EditableOrderInterface.php index 9ca48ad..86945c0 100644 --- a/src/Entity/EditableOrderInterface.php +++ b/src/Entity/EditableOrderInterface.php @@ -8,4 +8,5 @@ interface EditableOrderInterface extends OrderInterface, InitialTotalAwareOrderInterface { + public function isAlreadyPaid(): bool; } diff --git a/src/Entity/EditableOrderTrait.php b/src/Entity/EditableOrderTrait.php new file mode 100644 index 0000000..f242d6d --- /dev/null +++ b/src/Entity/EditableOrderTrait.php @@ -0,0 +1,16 @@ +getPaymentState() === OrderPaymentStates::STATE_PAID; + } +} diff --git a/src/Preparer/OrderPreparer.php b/src/Preparer/OrderPreparer.php index 680acfc..f57ed8d 100644 --- a/src/Preparer/OrderPreparer.php +++ b/src/Preparer/OrderPreparer.php @@ -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; @@ -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); diff --git a/src/Preparer/OrderPreparerInterface.php b/src/Preparer/OrderPreparerInterface.php index 46390fe..1e7193a 100644 --- a/src/Preparer/OrderPreparerInterface.php +++ b/src/Preparer/OrderPreparerInterface.php @@ -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; } diff --git a/src/Provider/UpdatedOrderProvider.php b/src/Provider/UpdatedOrderProvider.php index 4c6d40e..cf60885 100644 --- a/src/Provider/UpdatedOrderProvider.php +++ b/src/Provider/UpdatedOrderProvider.php @@ -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; @@ -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, @@ -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; } diff --git a/src/Provider/UpdatedOrderProviderInterface.php b/src/Provider/UpdatedOrderProviderInterface.php index b0f6113..6ca66f9 100644 --- a/src/Provider/UpdatedOrderProviderInterface.php +++ b/src/Provider/UpdatedOrderProviderInterface.php @@ -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; } diff --git a/src/Updated/OrderUpdater.php b/src/Updated/OrderUpdater.php index a8968c5..b0afa83 100644 --- a/src/Updated/OrderUpdater.php +++ b/src/Updated/OrderUpdater.php @@ -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; @@ -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); @@ -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())); } } diff --git a/tests/Application/src/Entity/Order.php b/tests/Application/src/Entity/Order.php index 62493fd..aa45ffc 100644 --- a/tests/Application/src/Entity/Order.php +++ b/tests/Application/src/Entity/Order.php @@ -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; } diff --git a/tests/Unit/Provider/OrderPreparerTest.php b/tests/Unit/Provider/OrderPreparerTest.php index 70607ca..8c0d714 100644 --- a/tests/Unit/Provider/OrderPreparerTest.php +++ b/tests/Unit/Provider/OrderPreparerTest.php @@ -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 @@ -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 diff --git a/tests/Unit/Provider/UpdatedOrderProviderTest.php b/tests/Unit/Provider/UpdatedOrderProviderTest.php index 2b259e8..08803b6 100644 --- a/tests/Unit/Provider/UpdatedOrderProviderTest.php +++ b/tests/Unit/Provider/UpdatedOrderProviderTest.php @@ -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; @@ -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 @@ -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 @@ -64,10 +64,10 @@ 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); @@ -75,7 +75,7 @@ public function testItThrowsExceptionIfFormIsNotValid(): void $this->expectException(OrderUpdateException::class); - $provider->provideFromOldOrderAndRequest($order, $request); + $provider->provideFromOldOrderAndRequest($order->reveal(), $request); } public function testItThrowsExceptionIfFormDataIsNotOrder(): void @@ -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); @@ -97,6 +97,6 @@ public function testItThrowsExceptionIfFormDataIsNotOrder(): void $this->expectException(\InvalidArgumentException::class); - $provider->provideFromOldOrderAndRequest($order, $request); + $provider->provideFromOldOrderAndRequest($order->reveal(), $request); } } diff --git a/tests/Unit/Updater/OrderUpdaterTest.php b/tests/Unit/Updater/OrderUpdaterTest.php index 3b50efc..c231f14 100644 --- a/tests/Unit/Updater/OrderUpdaterTest.php +++ b/tests/Unit/Updater/OrderUpdaterTest.php @@ -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; @@ -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)) @@ -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);