Skip to content

Commit

Permalink
Use order instead of order number in invoice repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 7, 2021
1 parent 081b080 commit e480edc
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 23 deletions.
6 changes: 3 additions & 3 deletions spec/CommandHandler/SendInvoiceEmailHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]');
Expand All @@ -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();
Expand Down
12 changes: 7 additions & 5 deletions spec/Creator/InvoiceCreatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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])
;
}
Expand Down
4 changes: 2 additions & 2 deletions spec/EventProducer/OrderPaymentPaidProducerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/CommandHandler/SendInvoiceEmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Creator/InvoiceCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/ORM/InvoiceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Doctrine/ORM/InvoiceRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/EventProducer/OrderPaymentPaidProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/Migrations/Version20210607115930.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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\InvoicingPlugin\Migrations;
Expand Down

0 comments on commit e480edc

Please sign in to comment.