Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/Creator/InvoiceCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
51 changes: 0 additions & 51 deletions tests/Unit/Creator/InvoiceCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down