From e480edceedc72357fd28467a05d4726259b3ed2b Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Mon, 7 Jun 2021 15:17:23 +0200 Subject: [PATCH] Use order instead of order number in invoice repository --- spec/CommandHandler/SendInvoiceEmailHandlerSpec.php | 6 +++--- spec/Creator/InvoiceCreatorSpec.php | 12 +++++++----- spec/EventProducer/OrderPaymentPaidProducerSpec.php | 4 ++-- src/CommandHandler/SendInvoiceEmailHandler.php | 8 ++++---- src/Creator/InvoiceCreator.php | 8 ++++---- src/Doctrine/ORM/InvoiceRepository.php | 5 +++-- src/Doctrine/ORM/InvoiceRepositoryInterface.php | 3 ++- src/Entity/Invoice.php | 2 +- src/EventProducer/OrderPaymentPaidProducer.php | 2 +- src/Migrations/Version20210607115930.php | 9 +++++++++ 10 files changed, 36 insertions(+), 23 deletions(-) diff --git a/spec/CommandHandler/SendInvoiceEmailHandlerSpec.php b/spec/CommandHandler/SendInvoiceEmailHandlerSpec.php index 21e6b765..8e9a3a47 100644 --- a/spec/CommandHandler/SendInvoiceEmailHandlerSpec.php +++ b/spec/CommandHandler/SendInvoiceEmailHandlerSpec.php @@ -40,10 +40,10 @@ public function it_requests_an_email_with_an_invoice_to_be_sent( OrderInterface $order, CustomerInterface $customer ): void { - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn($invoice); - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $invoiceRepository->findOneByOrder($order)->willReturn($invoice); + $order->getCustomer()->willReturn($customer); $customer->getEmail()->willReturn('shop@example.com'); @@ -60,8 +60,8 @@ public function it_does_not_request_an_email_to_be_sent_if_invoice_was_not_found OrderInterface $order, CustomerInterface $customer ): void { - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn(null); $orderRepository->findOneByNumber('0000001')->willReturn($order); + $invoiceRepository->findOneByOrder($order)->willReturn(null); $order->getCustomer()->shouldNotBeCalled(); $customer->getEmail()->shouldNotBeCalled(); diff --git a/spec/Creator/InvoiceCreatorSpec.php b/spec/Creator/InvoiceCreatorSpec.php index efe5efcd..0c2f267c 100644 --- a/spec/Creator/InvoiceCreatorSpec.php +++ b/spec/Creator/InvoiceCreatorSpec.php @@ -45,10 +45,10 @@ public function it_creates_invoice_for_order( OrderInterface $order, InvoiceInterface $invoice ): void { - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn(null); - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $invoiceRepository->findOneByOrder($order)->willReturn(null); + $invoiceDateTime = new \DateTimeImmutable('2019-02-25'); $invoiceGenerator->generateForOrder($order, $invoiceDateTime)->willReturn($invoice); @@ -62,17 +62,19 @@ public function it_throws_an_exception_when_invoice_was_already_created_for_give InvoiceRepositoryInterface $invoiceRepository, OrderRepositoryInterface $orderRepository, InvoiceGeneratorInterface $invoiceGenerator, + OrderInterface $order, InvoiceInterface $invoice ): void { - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn($invoice); + $orderRepository->findOneByNumber('0000001')->willReturn($order); + $invoiceRepository->findOneByOrder($order)->willReturn($invoice); $invoiceDateTime = new \DateTimeImmutable('2019-02-25'); - $orderRepository->findOneByNumber(Argument::any())->shouldNotBeCalled(); $invoiceGenerator->generateForOrder(Argument::any(), Argument::any())->shouldNotBeCalled(); $invoiceRepository->add(Argument::any())->shouldNotBeCalled(); - $this->shouldThrow(InvoiceAlreadyGenerated::withOrderNumber('0000001')) + $this + ->shouldThrow(InvoiceAlreadyGenerated::withOrderNumber('0000001')) ->during('__invoke', ['0000001', $invoiceDateTime]) ; } diff --git a/spec/EventProducer/OrderPaymentPaidProducerSpec.php b/spec/EventProducer/OrderPaymentPaidProducerSpec.php index d0bfddb2..66da15f8 100644 --- a/spec/EventProducer/OrderPaymentPaidProducerSpec.php +++ b/spec/EventProducer/OrderPaymentPaidProducerSpec.php @@ -50,7 +50,7 @@ public function it_dispatches_order_payment_paid_event_for_payment( $event = new OrderPaymentPaid('0000001', $dateTime); - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn($invoice); + $invoiceRepository->findOneByOrder($order)->willReturn($invoice); $eventBus->dispatch($event)->shouldBeCalled()->willReturn(new Envelope($event)); @@ -80,7 +80,7 @@ public function it_does_not_dispatch_event_when_there_is_no_invoice_related_to_o ): void { $payment->getOrder()->willReturn($order); $order->getNumber()->willReturn('0000001'); - $invoiceRepository->findOneByOrderNumber('0000001')->willReturn(null); + $invoiceRepository->findOneByOrder($order)->willReturn(null); $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); $dateTimeProvider->__invoke()->shouldNotBeCalled(); diff --git a/src/CommandHandler/SendInvoiceEmailHandler.php b/src/CommandHandler/SendInvoiceEmailHandler.php index f0bd696b..bd530099 100644 --- a/src/CommandHandler/SendInvoiceEmailHandler.php +++ b/src/CommandHandler/SendInvoiceEmailHandler.php @@ -43,16 +43,16 @@ public function __construct( public function __invoke(SendInvoiceEmail $command): void { + /** @var OrderInterface $order */ + $order = $this->orderRepository->findOneByNumber($command->orderNumber()); + /** @var InvoiceInterface|null $invoice */ - $invoice = $this->invoiceRepository->findOneByOrderNumber($command->orderNumber()); + $invoice = $this->invoiceRepository->findOneByOrder($order); if (null === $invoice) { return; } - /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneByNumber($command->orderNumber()); - if (null === $order->getCustomer()) { return; } diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index b15bae18..9a87d062 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -43,16 +43,16 @@ public function __construct( public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void { + /** @var OrderInterface $order */ + $order = $this->orderRepository->findOneByNumber($orderNumber); + /** @var InvoiceInterface|null $invoice */ - $invoice = $this->invoiceRepository->findOneByOrderNumber($orderNumber); + $invoice = $this->invoiceRepository->findOneByOrder($order); if (null !== $invoice) { throw InvoiceAlreadyGenerated::withOrderNumber($orderNumber); } - /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneByNumber($orderNumber); - $invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime); $this->invoiceRepository->add($invoice); diff --git a/src/Doctrine/ORM/InvoiceRepository.php b/src/Doctrine/ORM/InvoiceRepository.php index 634b67d8..b70cb0f5 100644 --- a/src/Doctrine/ORM/InvoiceRepository.php +++ b/src/Doctrine/ORM/InvoiceRepository.php @@ -14,14 +14,15 @@ namespace Sylius\InvoicingPlugin\Doctrine\ORM; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; class InvoiceRepository extends EntityRepository implements InvoiceRepositoryInterface { - public function findOneByOrderNumber(string $orderNumber): ?InvoiceInterface + public function findOneByOrder(OrderInterface $order): ?InvoiceInterface { /** @var InvoiceInterface|null $invoice */ - $invoice = $this->findOneBy(['orderNumber' => $orderNumber]); + $invoice = $this->findOneBy(['order' => $order]); return $invoice; } diff --git a/src/Doctrine/ORM/InvoiceRepositoryInterface.php b/src/Doctrine/ORM/InvoiceRepositoryInterface.php index 2bfa960b..1713e6ba 100644 --- a/src/Doctrine/ORM/InvoiceRepositoryInterface.php +++ b/src/Doctrine/ORM/InvoiceRepositoryInterface.php @@ -13,10 +13,11 @@ namespace Sylius\InvoicingPlugin\Doctrine\ORM; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; interface InvoiceRepositoryInterface extends RepositoryInterface { - public function findOneByOrderNumber(string $orderNumber): ?InvoiceInterface; + public function findOneByOrder(OrderInterface $order): ?InvoiceInterface; } diff --git a/src/Entity/Invoice.php b/src/Entity/Invoice.php index bfcbb8c3..c81f8841 100644 --- a/src/Entity/Invoice.php +++ b/src/Entity/Invoice.php @@ -116,7 +116,7 @@ public function order(): OrderInterface public function orderNumber(): string { - return $this->order->getNumber(); + return (string) $this->order->getNumber(); } public function issuedAt(): \DateTimeInterface diff --git a/src/EventProducer/OrderPaymentPaidProducer.php b/src/EventProducer/OrderPaymentPaidProducer.php index 8820470d..6ed5f20c 100644 --- a/src/EventProducer/OrderPaymentPaidProducer.php +++ b/src/EventProducer/OrderPaymentPaidProducer.php @@ -63,6 +63,6 @@ private function shouldEventBeDispatched(PaymentInterface $payment): bool /** @var OrderInterface $order */ $order = $payment->getOrder(); - return null !== $order && null !== $this->invoiceRepository->findOneByOrderNumber($order->getNumber()); + return null !== $order && null !== $this->invoiceRepository->findOneByOrder($order); } } diff --git a/src/Migrations/Version20210607115930.php b/src/Migrations/Version20210607115930.php index 32689eac..b087b072 100644 --- a/src/Migrations/Version20210607115930.php +++ b/src/Migrations/Version20210607115930.php @@ -1,5 +1,14 @@