diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index 15b18b96..a242e801 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -13,7 +13,6 @@ namespace Sylius\InvoicingPlugin\Creator; -use Doctrine\ORM\Exception\ORMException; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface; @@ -49,19 +48,13 @@ public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): voi $invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime); - if (!$this->hasEnabledPdfFileGenerator) { - $this->invoiceRepository->add($invoice); + $this->invoiceRepository->add($invoice); + if (!$this->hasEnabledPdfFileGenerator) { return; } $invoicePdf = $this->invoicePdfFileGenerator->generate($invoice); $this->invoiceFileManager->save($invoicePdf); - - try { - $this->invoiceRepository->add($invoice); - } catch (ORMException) { - $this->invoiceFileManager->remove($invoicePdf); - } } } diff --git a/tests/Unit/Creator/InvoiceCreatorTest.php b/tests/Unit/Creator/InvoiceCreatorTest.php index deafe858..0d2be340 100644 --- a/tests/Unit/Creator/InvoiceCreatorTest.php +++ b/tests/Unit/Creator/InvoiceCreatorTest.php @@ -162,57 +162,6 @@ public function it_creates_invoice_without_generating_pdf_file(): void $creator('0000001', $invoiceDateTime); } - #[Test] - public function it_removes_saved_invoice_file_if_database_update_fails(): void - { - $order = $this->createMock(OrderInterface::class); - $invoice = $this->createMock(InvoiceInterface::class); - $invoicePdf = new InvoicePdf('invoice.pdf', 'CONTENT'); - $invoiceDateTime = new \DateTimeImmutable('2019-02-25'); - - $this->orderRepository - ->expects(self::once()) - ->method('findOneByNumber') - ->with('0000001') - ->willReturn($order); - - $this->invoiceRepository - ->expects(self::once()) - ->method('findOneByOrder') - ->with($order) - ->willReturn(null); - - $this->invoiceGenerator - ->expects(self::once()) - ->method('generateForOrder') - ->with($order, $invoiceDateTime) - ->willReturn($invoice); - - $this->invoicePdfFileGenerator - ->expects(self::once()) - ->method('generate') - ->with($invoice) - ->willReturn($invoicePdf); - - $this->invoiceFileManager - ->expects(self::once()) - ->method('save') - ->with($invoicePdf); - - $this->invoiceRepository - ->expects(self::once()) - ->method('add') - ->with($invoice) - ->willThrowException(new EntityNotFoundException()); - - $this->invoiceFileManager - ->expects(self::once()) - ->method('remove') - ->with($invoicePdf); - - ($this->creator)('0000001', $invoiceDateTime); - } - #[Test] public function it_throws_an_exception_when_invoice_was_already_created_for_given_order(): void {