-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IVM-19 Extract default InvoiceConfiguration creation out from service…
… to repo decoration Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
- Loading branch information
Showing
11 changed files
with
172 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Repository/InvoiceConfigurationRepositoryDecoration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/Unit/Repository/InvoiceConfigurationRepositoryDecorationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters