Skip to content

Commit

Permalink
Move invoice number generation to invoice generator decorator in orde…
Browse files Browse the repository at this point in the history
…r space

Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Sep 22, 2024
1 parent d510b09 commit 7a9c8cd
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 22 deletions.
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ imports:
- { resource: src/Service/services.yaml }
- { resource: src/Settings/services.yaml }
- { resource: src/Language/Service/services.yaml }
- { resource: src/Order/services.yaml }
- { resource: src/Transput/services.yaml }

services:
Expand Down
4 changes: 1 addition & 3 deletions src/Document/MpdfDocument/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function __construct(
protected TemplateRendererInterface $templateRenderer,
protected LanguageInterface $shopLanguage,
protected DocumentLayoutSettingsServiceInterface $layoutSettingsService,
protected NumberWordingServiceInterface $numberWordingService,
protected OrderServiceInterface $orderService,
protected NumberWordingServiceInterface $numberWordingService
) {
}

Expand All @@ -44,7 +43,6 @@ public function generate(InvoiceDataInterface $invoiceData): void
mkdir(Path::getDirectory($invoiceFilePath), 0777, true);
}

$this->orderService->prepareOrderInvoiceNumber($invoiceData);
$this->pdfProcessor->OutputFile($invoiceFilePath);
}

Expand Down
29 changes: 29 additions & 0 deletions src/Order/Decoration/InvoiceGeneratorDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\Invoice\Order\Decoration;

use FreshAdvance\Invoice\DataType\InvoiceDataInterface;
use FreshAdvance\Invoice\Document\InvoiceGeneratorInterface;
use FreshAdvance\Invoice\Service\OrderServiceInterface;

class InvoiceGeneratorDecorator implements InvoiceGeneratorInterface
{
public function __construct(
private InvoiceGeneratorInterface $originalGenerator,
private OrderServiceInterface $orderService,
) {
}

public function generate(InvoiceDataInterface $invoiceData): void
{
$this->orderService->prepareOrderInvoiceNumber($invoiceData);
$this->originalGenerator->generate($invoiceData);
}
}
9 changes: 9 additions & 0 deletions src/Order/Decoration/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
public: false
autowire: true

FreshAdvance\Invoice\Order\Decoration\InvoiceGeneratorDecorator:
decorates: FreshAdvance\Invoice\Document\InvoiceGeneratorInterface
arguments:
$originalGenerator: '@.inner'
7 changes: 7 additions & 0 deletions src/Order/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
imports:
- { resource: Decoration/services.yaml }

services:
_defaults:
public: false
autowire: true
19 changes: 0 additions & 19 deletions tests/Integration/Document/MpdfDocument/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,12 @@ public function testGetBinaryPdfFromData(): void
$this->assertDirectoryExists($tempDirectory->url() . '/somePath/');
}

public function testGenerateTriggersOrderNumberingUpdate(): void
{
$invoiceData = $this->createConfiguredMock(InvoiceDataInterface::class, [
'getOrder' => $this->createStub(Order::class)
]);

$sut = $this->getSut(
orderService: $orderServiceSpy = $this->createMock(OrderServiceInterface::class),
);

$orderServiceSpy->expects($this->once())
->method('prepareOrderInvoiceNumber')
->with($invoiceData);

$sut->generate($invoiceData);
}

public function getSut(
Mpdf $pdfProcessor = null,
TemplateRendererInterface $templateRenderer = null,
LanguageProxy $shopLanguage = null,
DocumentLayoutSettingsServiceInterface $layoutSettingsService = null,
NumberWordingServiceInterface $numberWordingService = null,
OrderServiceInterface $orderService = null,
): Builder {
$layoutSettingsService ??= $this->createStub(DocumentLayoutSettingsServiceInterface::class);

Expand All @@ -107,7 +89,6 @@ public function getSut(
shopLanguage: $shopLanguage ?? $this->createStub(LanguageProxy::class),
layoutSettingsService: $layoutSettingsService,
numberWordingService: $numberWordingService ?? $this->createStub(NumberWordingServiceInterface::class),
orderService: $orderService ?? $this->createStub(OrderServiceInterface::class),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\Invoice\Tests\Integration\Order\Decoration;

use FreshAdvance\Invoice\Document\InvoiceGeneratorInterface;
use FreshAdvance\Invoice\Order\Decoration\InvoiceGeneratorDecorator;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;

/** @covers \FreshAdvance\Invoice\Order\Decoration\InvoiceGeneratorDecorator */
class InvoiceGeneratorDecoratorTest extends IntegrationTestCase
{
public function testOriginalServiceIsDecorated(): void
{
$sut = ContainerFactory::getInstance()->getContainer()->get(InvoiceGeneratorInterface::class);

$this->assertInstanceOf(InvoiceGeneratorDecorator::class, $sut);
}
}
62 changes: 62 additions & 0 deletions tests/Unit/Order/Decoration/InvoiceGeneratorDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\Invoice\Tests\Unit\Order\Decoration;

use FreshAdvance\Invoice\DataType\InvoiceDataInterface;
use FreshAdvance\Invoice\Document\InvoiceGeneratorInterface;
use FreshAdvance\Invoice\Order\Decoration\InvoiceGeneratorDecorator;
use FreshAdvance\Invoice\Service\OrderServiceInterface;
use OxidEsales\Eshop\Application\Model\Order;
use PHPUnit\Framework\TestCase;

/** @covers \FreshAdvance\Invoice\Order\Decoration\InvoiceGeneratorDecorator */
class InvoiceGeneratorDecoratorTest extends TestCase
{
public function testOriginalGeneratorCalledWithCorrectParameter(): void
{
$invoiceDataStub = $this->createStub(InvoiceDataInterface::class);

$generatorSpy = $this->createMock(InvoiceGeneratorInterface::class);
$generatorSpy->expects($this->once())
->method('generate')
->with($invoiceDataStub);

$sut = $this->getSut($generatorSpy);

$sut->generate($invoiceDataStub);
}

public function testGenerateTriggersOrderNumberingUpdate(): void
{
$invoiceData = $this->createConfiguredMock(InvoiceDataInterface::class, [
'getOrder' => $this->createStub(Order::class)
]);

$sut = $this->getSut(
orderService: $orderServiceSpy = $this->createMock(OrderServiceInterface::class),
);

$orderServiceSpy->expects($this->once())
->method('prepareOrderInvoiceNumber')
->with($invoiceData);

$sut->generate($invoiceData);
}

public function getSut(
InvoiceGeneratorInterface $originalGenerator = null,
OrderServiceInterface $orderService = null,
): InvoiceGeneratorInterface {
return new InvoiceGeneratorDecorator(
originalGenerator: $originalGenerator ?? $this->createStub(InvoiceGeneratorInterface::class),
orderService: $orderService ?? $this->createStub(OrderServiceInterface::class),
);
}
}

0 comments on commit 7a9c8cd

Please sign in to comment.