Skip to content

Commit

Permalink
IVM-19 Extract default InvoiceConfiguration creation out from service…
Browse files Browse the repository at this point in the history
… to repo decoration

Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
  • Loading branch information
Sieg committed Feb 15, 2025
1 parent b560539 commit 7ec57a6
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Change filename prefix for admin panel invoice download to be format with some possible placeholders instead
- Moved new FilenameCalculator class to Document domain, as they are used not only for emails now
- InvoiceConfigurationRepositoryInterface::getByOrderId doesnt return null anymore

### Removed
- Not used methods leftovers from ModuleSettings
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/InvoiceConfigurationNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Invoice\Exception;

class InvoiceConfigurationNotFound extends \Exception
{
}
14 changes: 9 additions & 5 deletions src/Repository/InvoiceConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\ForwardCompatibility\Result;
use FreshAdvance\Invoice\DataType\InvoiceConfiguration;
use FreshAdvance\Invoice\DataType\InvoiceConfigurationInterface;
use FreshAdvance\Invoice\Exception\InvoiceConfigurationNotFound;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;

class InvoiceConfigurationRepository implements InvoiceConfigurationRepositoryInterface
Expand All @@ -21,7 +22,7 @@ public function __construct(
) {
}

public function getByOrderId(string $orderId): ?InvoiceConfigurationInterface
public function getByOrderId(string $orderId): InvoiceConfigurationInterface
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->select('*')
Expand All @@ -41,14 +42,17 @@ public function getByOrderId(string $orderId): ?InvoiceConfigurationInterface
);
}

return null;
throw new InvoiceConfigurationNotFound();
}

public function save(InvoiceConfigurationInterface $invoiceConfiguration): void
{
$this->getByOrderId($invoiceConfiguration->getOrderId())
? $this->updateInvoiceConfiguration($invoiceConfiguration)
: $this->createInvoiceConfiguration($invoiceConfiguration);
try {
$this->getByOrderId($invoiceConfiguration->getOrderId());
$this->updateInvoiceConfiguration($invoiceConfiguration);
} catch (InvoiceConfigurationNotFound $e) {
$this->createInvoiceConfiguration($invoiceConfiguration);
}
}

protected function createInvoiceConfiguration(InvoiceConfigurationInterface $invoiceConfiguration): void
Expand Down
44 changes: 44 additions & 0 deletions src/Repository/InvoiceConfigurationRepositoryDecoration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Invoice\Repository;

use FreshAdvance\Invoice\DataType\InvoiceConfiguration;
use FreshAdvance\Invoice\DataType\InvoiceConfigurationInterface;
use FreshAdvance\Invoice\Exception\InvoiceConfigurationNotFound;
use FreshAdvance\Invoice\Settings\ModuleSettingsInterface;

class InvoiceConfigurationRepositoryDecoration implements InvoiceConfigurationRepositoryInterface
{
public function __construct(
private InvoiceConfigurationRepositoryInterface $originalRepository,
private ModuleSettingsInterface $moduleSettings,
) {
}

public function getByOrderId(string $orderId): InvoiceConfigurationInterface
{
try {
$result = $this->originalRepository->getByOrderId($orderId);
} catch (InvoiceConfigurationNotFound) {
$result = new InvoiceConfiguration(
orderId: $orderId,
date: $this->moduleSettings->getInvoiceDateFormat(),
number: $this->moduleSettings->getInvoiceNumberFormat(),
);
}

return $result;
}

public function save(InvoiceConfigurationInterface $invoiceConfiguration): void
{
$this->originalRepository->save($invoiceConfiguration);
}
}
2 changes: 1 addition & 1 deletion src/Repository/InvoiceConfigurationRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

interface InvoiceConfigurationRepositoryInterface
{
public function getByOrderId(string $orderId): ?InvoiceConfigurationInterface;
public function getByOrderId(string $orderId): InvoiceConfigurationInterface;

public function save(InvoiceConfigurationInterface $invoiceConfiguration): void;
}
5 changes: 5 additions & 0 deletions src/Repository/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ services:
FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryInterface:
class: FreshAdvance\Invoice\Repository\InvoiceConfigurationRepository

FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryDecoration:
decorates: FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryInterface
arguments:
$originalRepository: '@.inner'

FreshAdvance\Invoice\Repository\ShopRepositoryInterface:
class: FreshAdvance\Invoice\Repository\ShopRepository
8 changes: 0 additions & 8 deletions src/Service/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ public function __construct(
public function getInvoiceDataByOrderId(string $orderId): InvoiceDataInterface
{
$order = $this->orderRepository->getByOrderId($orderId);

$configuration = $this->invoiceConfigRepo->getByOrderId($orderId);
if (!$configuration) {
$configuration = new InvoiceConfiguration(
orderId: $orderId,
date: $this->moduleSettings->getInvoiceDateFormat(),
number: $this->moduleSettings->getInvoiceNumberFormat(),
);
}

return new InvoiceData(
order: $order,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use FreshAdvance\Invoice\DataType\InvoiceConfiguration;
use FreshAdvance\Invoice\DataType\InvoiceConfigurationInterface;
use FreshAdvance\Invoice\Exception\InvoiceConfigurationNotFound;
use FreshAdvance\Invoice\Repository\InvoiceConfigurationRepository;
use FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryInterface;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
Expand Down Expand Up @@ -38,12 +39,6 @@ public function setUp(): void
);
}

public function testGetNotExistingOrderInvoice(): void
{
$sut = $this->getSut();
$this->assertNull($sut->getByOrderId(self::TEST_NOT_EXISTING_ORDER_ID));
}

public function testExistingOrderInvoice(): void
{
$sut = $this->getSut();
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Exception/InvoiceConfigurationNotFoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

declare(strict_types=1);

namespace Exception;

use FreshAdvance\Invoice\Exception\InvoiceConfigurationNotFound;
use PHPUnit\Framework\TestCase;

class InvoiceConfigurationNotFoundTest extends TestCase
{
public function testException(): void
{
$sut = new InvoiceConfigurationNotFound();
$this->assertInstanceOf(\Exception::class, $sut);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Invoice\Tests\Unit\Repository;

use FreshAdvance\Invoice\DataType\InvoiceConfigurationInterface;
use FreshAdvance\Invoice\Exception\InvoiceConfigurationNotFound;
use FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryDecoration;
use FreshAdvance\Invoice\Repository\InvoiceConfigurationRepositoryInterface;
use FreshAdvance\Invoice\Settings\ModuleSettingsInterface;
use PHPUnit\Framework\TestCase;

class InvoiceConfigurationRepositoryDecorationTest extends TestCase
{
public function testDecorationProxiesToOriginalMethods(): void
{
$invoiceConfigurationStub = $this->createStub(InvoiceConfigurationInterface::class);

$originalRepository = $this->createMock(InvoiceConfigurationRepositoryInterface::class);
$originalRepository->method('getByOrderId')
->with($orderId = uniqid())
->willReturn($invoiceConfigurationStub);
$originalRepository->expects($this->once())
->method('save')
->with($invoiceConfigurationStub);

$sut = $this->getSut(
invoiceConfigurationRepository: $originalRepository,
);

$this->assertSame($invoiceConfigurationStub, $sut->getByOrderId($orderId));
$sut->save($invoiceConfigurationStub);
}

public function testDecorationReturnsInvoiceConfigurationWithDefaultsIfNoneFound(): void
{
$moduleSettingsStub = $this->createStub(ModuleSettingsInterface::class);
$moduleSettingsStub->method('getInvoiceDateFormat')->willReturn($dateFormat = uniqid());
$moduleSettingsStub->method('getInvoiceNumberFormat')->willReturn($numberFormat = uniqid());

$originalRepositoryMock = $this->createMock(InvoiceConfigurationRepositoryInterface::class);
$originalRepositoryMock->method('getByOrderId')
->with($orderId = uniqid())
->willThrowException(new InvoiceConfigurationNotFound);

$sut = $this->getSut(
invoiceConfigurationRepository: $originalRepositoryMock,
moduleSettings: $moduleSettingsStub,
);

$configuration = $sut->getByOrderId($orderId);

$this->assertSame($orderId, $configuration->getOrderId());
$this->assertSame($dateFormat, $configuration->getDate());
$this->assertSame($numberFormat, $configuration->getNumber());
}

private function getSut(
InvoiceConfigurationRepositoryInterface $invoiceConfigurationRepository = null,
ModuleSettingsInterface $moduleSettings = null,
): InvoiceConfigurationRepositoryInterface {
$invoiceConfigurationRepository ??= $this->createStub(InvoiceConfigurationRepositoryInterface::class);

return new InvoiceConfigurationRepositoryDecoration(
originalRepository: $invoiceConfigurationRepository,
moduleSettings: $moduleSettings ?? $this->createStub(ModuleSettingsInterface::class),
);
}
}
26 changes: 0 additions & 26 deletions tests/Unit/Service/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,6 @@ public function testGetInvoiceData(): void
$this->assertSame($invoiceConfigurationStub, $result->getInvoiceConfiguration());
}

public function testGetDefaultInvoiceData(): void
{
$sut = $this->getSut(
orderRepository: $this->createConfiguredMock(OrderRepositoryInterface::class, [
'getByOrderId' => $this->createConfiguredMock(OrderModel::class, [
'getShopId' => 3,
'getId' => uniqid()
])
]),
invoiceConfigRepo: $this->createConfiguredMock(InvoiceConfigurationRepositoryInterface::class, [
'getByOrderId' => null
]),
moduleSettings: $this->createConfiguredMock(ModuleSettingsInterface::class, [
'getInvoiceDateFormat' => $dateFormat = uniqid(),
'getInvoiceNumberFormat' => $numberFormat = uniqid(),
])
);

$result = $sut->getInvoiceDataByOrderId(uniqid());

$configuration = $result->getInvoiceConfiguration();

$this->assertSame($dateFormat, $configuration->getDate());
$this->assertSame($numberFormat, $configuration->getNumber());
}

public function testSaveOrderInvoiceData(): void
{
$configurationStub = $this->createStub(InvoiceConfigurationInterface::class);
Expand Down

0 comments on commit 7ec57a6

Please sign in to comment.