From 04c3026ede26417ab50106786e2b99e62bc7e2fc Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 6 Jun 2025 17:31:11 +0300 Subject: [PATCH 01/13] HP-2512 Cleaned up BillingRegistryService --- .../Application/BillingRegistryService.php | 23 +++---------------- .../BillingRegistryServiceInterface.php | 9 +------- .../BillingRegistryTariffServiceInterface.php | 16 +++++++++++++ .../BillingRegistryTariffServiseInterface.php | 18 --------------- .../BillingRegistryTariffServiceTest.php | 4 ++-- 5 files changed, 22 insertions(+), 48 deletions(-) create mode 100644 src/product/Application/BillingRegistryTariffServiceInterface.php delete mode 100644 src/product/Application/BillingRegistryTariffServiseInterface.php diff --git a/src/product/Application/BillingRegistryService.php b/src/product/Application/BillingRegistryService.php index b0dcba24..eac48ee8 100644 --- a/src/product/Application/BillingRegistryService.php +++ b/src/product/Application/BillingRegistryService.php @@ -49,19 +49,15 @@ public function getRepresentationsByType(string $representationClass): array return $representations; } - public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface + public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface { foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { - if ($tariffName === $tariffTypeDefinition->tariffType()->name) { + if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) { return $tariffTypeDefinition; } } - return null; - } - public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool - { - return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper); + throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found'); } public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface { @@ -157,19 +153,6 @@ public function getAggregate(string $type): AggregateInterface throw new AggregateNotFoundException('Aggregate was not found'); } - public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface - { - $tariffType = $behavior->getTariffType(); - - foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { - if ($tariffTypeDefinition->belongToTariffType($tariffType)) { - return $tariffTypeDefinition; - } - } - - throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found'); - } - public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator { foreach ($this->registry->priceTypes() as $priceTypeDefinition) { diff --git a/src/product/Application/BillingRegistryServiceInterface.php b/src/product/Application/BillingRegistryServiceInterface.php index 9f9c6ac7..8c2bc083 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -2,18 +2,12 @@ namespace hiqdev\php\billing\product\Application; -use Generator; use hiqdev\php\billing\product\AggregateInterface; -use hiqdev\php\billing\product\behavior\BehaviorInterface; -use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; -use hiqdev\php\billing\product\behavior\InvalidBehaviorException; use hiqdev\php\billing\product\invoice\RepresentationInterface; -use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; use hiqdev\php\billing\product\quantity\FractionQuantityData; use hiqdev\php\billing\product\quantity\QuantityFormatterInterface; -use hiqdev\php\billing\product\TariffTypeDefinitionInterface; -interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface +interface BillingRegistryServiceInterface extends BillingRegistryTariffServiceInterface, BillingRegistryBehaviorServiceInterface { /** * @param string $representationClass @@ -24,5 +18,4 @@ public function getRepresentationsByType(string $representationClass): array; public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface; public function getAggregate(string $type): AggregateInterface; - } diff --git a/src/product/Application/BillingRegistryTariffServiceInterface.php b/src/product/Application/BillingRegistryTariffServiceInterface.php new file mode 100644 index 00000000..d3e0eb2d --- /dev/null +++ b/src/product/Application/BillingRegistryTariffServiceInterface.php @@ -0,0 +1,16 @@ +registry->addTariffType($tariffTypeDefinition); - $tariff = $this->registryService->getTariffDefinitionByName('dummy'); + $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); $this->assertSame($tariffType->name(), $tariff->tariffType()->name()); } @@ -54,7 +54,7 @@ public function testCheckBehaviorInTariff(): void $this->registry->addTariffType($tariffTypeDefinition); - $tariff = $this->registryService->getTariffDefinitionByName('dummy'); + $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); $this->assertTrue($this->registryService->hasBehaviour($tariff, TestBehavior::class)); } From bcff484c96ea50dce6b565871be3ba08a58ea740 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 09:21:43 +0300 Subject: [PATCH 02/13] HP-2512 removed invalid PHPUnit test --- .../BillingRegistryTariffServiceTest.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php b/tests/unit/product/Application/BillingRegistryTariffServiceTest.php index 7923427b..6d9cd841 100644 --- a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryTariffServiceTest.php @@ -42,20 +42,4 @@ public function testGetTariffDefinitionByName(): void $this->assertSame($tariffType->name(), $tariff->tariffType()->name()); } - - public function testCheckBehaviorInTariff(): void - { - $tariffType = new DummyTariffType(); - $tariffTypeDefinition = new TariffTypeDefinition($tariffType); - $dummyBehavior = new TestBehavior('dummy'); - $tariffTypeDefinition - ->withBehaviors() - ->attach($dummyBehavior); - - $this->registry->addTariffType($tariffTypeDefinition); - - $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); - $this->assertTrue($this->registryService->hasBehaviour($tariff, TestBehavior::class)); - } - } From bbf2803d56de3a1e09266235af092b41f9fa88d7 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:01:01 +0300 Subject: [PATCH 03/13] HP-2512 moved Representation class to php-billing package --- src/product/invoice/Representation.php | 39 +++++++++++++++++++ .../BillingRegistryServiceTest.php | 7 ++-- .../product/invoice/TestRepresentation.php | 9 +++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/product/invoice/Representation.php create mode 100644 tests/unit/product/invoice/TestRepresentation.php diff --git a/src/product/invoice/Representation.php b/src/product/invoice/Representation.php new file mode 100644 index 00000000..c8b5806a --- /dev/null +++ b/src/product/invoice/Representation.php @@ -0,0 +1,39 @@ +sql) === '') { + throw new InvalidRepresentationException('Representation SQL cannot be empty.'); + } + } + + public function getSql(): string + { + return $this->sql; + } + + public function setType(TypeInterface $type): RepresentationInterface + { + $this->type = $type; + + return $this; + } + + public function getType(): TypeInterface + { + return $this->type; + } +} diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index 939646fa..88872139 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -2,8 +2,6 @@ namespace hiqdev\php\billing\tests\unit\product\Application; -use hiqdev\billing\registry\invoice\InvoiceRepresentation; -use hiqdev\billing\registry\invoice\PaymentRequestRepresentation; use hiqdev\php\billing\product\Application\BillingRegistryService; use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; use hiqdev\php\billing\product\BillingRegistry; @@ -15,6 +13,7 @@ use hiqdev\php\billing\tests\unit\product\behavior\TestBehavior; use hiqdev\php\billing\tests\unit\product\Domain\Model\DummyTariffType; use hiqdev\php\billing\tests\unit\product\Domain\Model\FakeTariffType; +use hiqdev\php\billing\tests\unit\product\invoice\TestRepresentation; use hiqdev\php\billing\type\Type; use PHPUnit\Framework\TestCase; @@ -51,8 +50,8 @@ public function testGetRepresentationsByInterfaceReturnsAllRepresentations(): vo ->withPrices() ->priceType(Type::anyId('dummy')) ->documentRepresentation() - ->attach(new InvoiceRepresentation("Invoice")) - ->attach(new PaymentRequestRepresentation("Payment Request")) + ->attach(new TestRepresentation("Invoice")) + ->attach(new TestRepresentation("Payment Request")) ->end() ->end() ->end(); diff --git a/tests/unit/product/invoice/TestRepresentation.php b/tests/unit/product/invoice/TestRepresentation.php new file mode 100644 index 00000000..6f22e2bb --- /dev/null +++ b/tests/unit/product/invoice/TestRepresentation.php @@ -0,0 +1,9 @@ + Date: Sun, 8 Jun 2025 10:06:44 +0300 Subject: [PATCH 04/13] HP-2512 fixed PHPUnit tests --- .../product/Application/BillingRegistryServiceTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index 88872139..865f9746 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -48,10 +48,14 @@ public function testGetRepresentationsByInterfaceReturnsAllRepresentations(): vo $tariffTypeDefinition ->withPrices() - ->priceType(Type::anyId('dummy')) + ->priceType(Type::anyId('dummy 1')) ->documentRepresentation() - ->attach(new TestRepresentation("Invoice")) - ->attach(new TestRepresentation("Payment Request")) + ->attach(new TestRepresentation("Representation 1")) + ->end() + ->end() + ->priceType(Type::anyId('dummy 2')) + ->documentRepresentation() + ->attach(new TestRepresentation("Representation 2")) ->end() ->end() ->end(); From cb9c3919505ce45e4bed8253e02ca536ceadb8fa Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:10:28 +0300 Subject: [PATCH 05/13] HP-2512 removed PHPUnit test duplicate --- .../BillingRegistryBehaviorServiceTest.php | 129 ------------------ 1 file changed, 129 deletions(-) delete mode 100644 tests/unit/product/Application/BillingRegistryBehaviorServiceTest.php diff --git a/tests/unit/product/Application/BillingRegistryBehaviorServiceTest.php b/tests/unit/product/Application/BillingRegistryBehaviorServiceTest.php deleted file mode 100644 index 22d48cb4..00000000 --- a/tests/unit/product/Application/BillingRegistryBehaviorServiceTest.php +++ /dev/null @@ -1,129 +0,0 @@ -registry = new BillingRegistry(); - $this->registryService = new BillingRegistryService($this->registry); - } - - public function testGetBehavior(): void - { - $tariffType = new DummyTariffType(); - $tariffTypeDefinition = new TariffTypeDefinition($tariffType); - $dummyBehavior = new TestBehavior('dummy'); - $type = Type::anyId('dummy'); - $tariffTypeDefinition - ->withPrices() - ->priceType($type) - ->withBehaviors() - ->attach($dummyBehavior); - - $this->registry->addTariffType($tariffTypeDefinition); - - $behavior = $this->registryService->getBehavior($type->getName(), TestBehavior::class); - - $this->assertSame($dummyBehavior->getContext(), $behavior->getContext()); - } - - public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void - { - $tariffType = new DummyTariffType(); - $tariffTypeDefinition = new TariffTypeDefinition($tariffType); - $type1 = Type::anyId('type,dummy1'); - $type2 = Type::anyId('type,dummy2'); - $dummyBehavior1 = new TestBehavior('dummy 1'); - $dummyBehavior2 = new TestBehavior('dummy 2'); - $dummyBehavior3 = new FakeBehavior('dummy 3'); - - $tariffTypeDefinition - ->withPrices() - ->priceType($type1) - ->withBehaviors() - ->attach($dummyBehavior1) - ->end() - ->end() - ->priceType($type2) - ->withBehaviors() - ->attach($dummyBehavior2) - ->attach($dummyBehavior3) - ->end() - ->end() - ->end(); - - $this->registry->addTariffType($tariffTypeDefinition); - - $behavior = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); - $this->assertSame($dummyBehavior1->getContext(), $behavior->getContext()); - - $behavior = $this->registryService->getBehavior($type2->getName(), TestBehavior::class); - $this->assertSame($dummyBehavior2->getContext(), $behavior->getContext()); - - $behavior = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); - $this->assertSame($dummyBehavior3->getContext(), $behavior->getContext()); - } - - public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void - { - $tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType()); - $testBehavior = new TestBehavior('dummy'); - $type1 = Type::anyId('type,dummy1'); - $tariffTypeDefinition1 - ->withPrices() - ->priceType($type1) - ->withBehaviors() - ->attach($testBehavior) - ->end() - ->end() - ->end(); - - $tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType()); - $fakeBehavior = new FakeBehavior('dummy'); - $type2 = Type::anyId('type,dummy2'); - $tariffTypeDefinition2 - ->withPrices() - ->priceType($type2) - ->withBehaviors() - ->attach($fakeBehavior) - ->end() - ->end() - ->end(); - - $this->registry->addTariffType($tariffTypeDefinition1); - $this->registry->addTariffType($tariffTypeDefinition2); - - /** @var TestBehavior $testBehaviorActual */ - $testBehaviorActual = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); - $this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext()); - - /** @var FakeBehavior $fakeBehaviorActual */ - $fakeBehaviorActual = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); - $this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext()); - } - - public function testGetBehaviorThrowsExceptionWhenNotFound(): void - { - $this->expectException(BehaviorNotFoundException::class); - $this->registryService->getBehavior('non-existent-type', TestBehavior::class); - } -} From 17e59dd4a752ece6b4344b85cc96eb911df47f49 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:13:06 +0300 Subject: [PATCH 06/13] HP-2512 made BillingRegistryServiceTest is more readable --- .../BillingRegistryServiceTest.php | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index 865f9746..8d854958 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -75,9 +75,12 @@ public function testGetBehavior(): void $type = Type::anyId('dummy'); $tariffTypeDefinition ->withPrices() - ->priceType($type) - ->withBehaviors() - ->attach($dummyBehavior); + ->priceType($type) + ->withBehaviors() + ->attach($dummyBehavior) + ->end() + ->end() + ->end(); $this->registry->addTariffType($tariffTypeDefinition); @@ -98,17 +101,17 @@ public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void $tariffTypeDefinition ->withPrices() - ->priceType($type1) - ->withBehaviors() - ->attach($dummyBehavior1) - ->end() - ->end() - ->priceType($type2) - ->withBehaviors() - ->attach($dummyBehavior2) - ->attach($dummyBehavior3) - ->end() - ->end() + ->priceType($type1) + ->withBehaviors() + ->attach($dummyBehavior1) + ->end() + ->end() + ->priceType($type2) + ->withBehaviors() + ->attach($dummyBehavior2) + ->attach($dummyBehavior3) + ->end() + ->end() ->end(); $this->registry->addTariffType($tariffTypeDefinition); @@ -130,11 +133,11 @@ public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void $type1 = Type::anyId('type,dummy1'); $tariffTypeDefinition1 ->withPrices() - ->priceType($type1) - ->withBehaviors() - ->attach($testBehavior) - ->end() - ->end() + ->priceType($type1) + ->withBehaviors() + ->attach($testBehavior) + ->end() + ->end() ->end(); $tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType()); @@ -142,11 +145,11 @@ public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void $type2 = Type::anyId('type,dummy2'); $tariffTypeDefinition2 ->withPrices() - ->priceType($type2) - ->withBehaviors() - ->attach($fakeBehavior) - ->end() - ->end() + ->priceType($type2) + ->withBehaviors() + ->attach($fakeBehavior) + ->end() + ->end() ->end(); $this->registry->addTariffType($tariffTypeDefinition1); From 0e898f791892d6b64460d4b7c712c0d867012660 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:13:34 +0300 Subject: [PATCH 07/13] HP-2512 tiny --- .../unit/product/Application/BillingRegistryServiceTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index 8d854958..e89a1276 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -169,10 +169,4 @@ public function testGetBehaviorThrowsExceptionWhenNotFound(): void $this->expectException(BehaviorNotFoundException::class); $this->registryService->getBehavior('non-existent-type', TestBehavior::class); } - -// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void -// { -// $this->expectException(QuantityFormatterNotFoundException::class); -// $this->registryService->createQuantityFormatter('non-existent-type', $this->createMock(FractionQuantityData::class)); -// } } From 1812faa6756df28bfdcfb8a7105c99dc1e1ffef0 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:20:59 +0300 Subject: [PATCH 08/13] HP-2512 removed BillingRegistryTariffServiceTest PHPUnit test because we don't have BillingRegistryTariffService class --- .../BillingRegistryServiceTest.php | 16 +++++++ .../BillingRegistryTariffServiceTest.php | 45 ------------------- 2 files changed, 16 insertions(+), 45 deletions(-) delete mode 100644 tests/unit/product/Application/BillingRegistryTariffServiceTest.php diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index e89a1276..7eb684fd 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -169,4 +169,20 @@ public function testGetBehaviorThrowsExceptionWhenNotFound(): void $this->expectException(BehaviorNotFoundException::class); $this->registryService->getBehavior('non-existent-type', TestBehavior::class); } + + public function testGetTariffDefinitionByName(): void + { + $tariffType = new DummyTariffType(); + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); + $dummyBehavior = new TestBehavior('dummy'); + $tariffTypeDefinition + ->withBehaviors() + ->attach($dummyBehavior); + + $this->registry->addTariffType($tariffTypeDefinition); + + $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); + + $this->assertSame($tariffType->name(), $tariff->tariffType()->name()); + } } diff --git a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php b/tests/unit/product/Application/BillingRegistryTariffServiceTest.php deleted file mode 100644 index 6d9cd841..00000000 --- a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php +++ /dev/null @@ -1,45 +0,0 @@ -registry = new BillingRegistry(); - $this->registryService = new BillingRegistryService($this->registry); - } - - public function testGetTariffDefinitionByName(): void - { - $tariffType = new DummyTariffType(); - $tariffTypeDefinition = new TariffTypeDefinition($tariffType); - $dummyBehavior = new TestBehavior('dummy'); - $tariffTypeDefinition - ->withBehaviors() - ->attach($dummyBehavior); - - $this->registry->addTariffType($tariffTypeDefinition); - - $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); - - $this->assertSame($tariffType->name(), $tariff->tariffType()->name()); - } -} From 48f33beb52769aa207dcf736dd887cdcee1d41ac Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 10:27:38 +0300 Subject: [PATCH 09/13] HP-2512 removed the next interfaces because this does not make any sense and only confusing - BillingRegistryBehaviorServiceInterface - BillingRegistryTariffServiceInterface --- ...illingRegistryBehaviorServiceInterface.php | 41 ------------------ .../BillingRegistryServiceInterface.php | 42 ++++++++++++++++++- .../BillingRegistryTariffServiceInterface.php | 16 ------- 3 files changed, 41 insertions(+), 58 deletions(-) delete mode 100644 src/product/Application/BillingRegistryBehaviorServiceInterface.php delete mode 100644 src/product/Application/BillingRegistryTariffServiceInterface.php diff --git a/src/product/Application/BillingRegistryBehaviorServiceInterface.php b/src/product/Application/BillingRegistryBehaviorServiceInterface.php deleted file mode 100644 index f0c79766..00000000 --- a/src/product/Application/BillingRegistryBehaviorServiceInterface.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ - public function getBehaviors(string $behaviorClassWrapper): Generator; - - /** - * Find all PriceTypeDefinition in registry by specified Behavior class. - * - * @param string $behaviorClassWrapper - * @return Generator - */ - public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator; - -} diff --git a/src/product/Application/BillingRegistryServiceInterface.php b/src/product/Application/BillingRegistryServiceInterface.php index 8c2bc083..458cd73e 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -2,12 +2,19 @@ namespace hiqdev\php\billing\product\Application; +use Generator; use hiqdev\php\billing\product\AggregateInterface; +use hiqdev\php\billing\product\behavior\BehaviorInterface; +use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; +use hiqdev\php\billing\product\behavior\InvalidBehaviorException; +use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException; use hiqdev\php\billing\product\invoice\RepresentationInterface; +use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; use hiqdev\php\billing\product\quantity\FractionQuantityData; use hiqdev\php\billing\product\quantity\QuantityFormatterInterface; +use hiqdev\php\billing\product\TariffTypeDefinitionInterface; -interface BillingRegistryServiceInterface extends BillingRegistryTariffServiceInterface, BillingRegistryBehaviorServiceInterface +interface BillingRegistryServiceInterface { /** * @param string $representationClass @@ -18,4 +25,37 @@ public function getRepresentationsByType(string $representationClass): array; public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface; public function getAggregate(string $type): AggregateInterface; + + /** + * @param string $tariffName + * @return TariffTypeDefinitionInterface + * @throws TariffTypeDefinitionNotFoundException + */ + public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface; + + /** + * @param string $type - full type like 'overuse,lb_capacity_unit' + * @param string $behaviorClassWrapper + * @return BehaviorInterface + * @throws BehaviorNotFoundException + * @throws InvalidBehaviorException + */ + public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface; + + + /** + * Find all behaviors attached to any TariffType or PriceType by specified Behavior class. + * + * @param string $behaviorClassWrapper + * @return Generator + */ + public function getBehaviors(string $behaviorClassWrapper): Generator; + + /** + * Find all PriceTypeDefinition in registry by specified Behavior class. + * + * @param string $behaviorClassWrapper + * @return Generator + */ + public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator; } diff --git a/src/product/Application/BillingRegistryTariffServiceInterface.php b/src/product/Application/BillingRegistryTariffServiceInterface.php deleted file mode 100644 index d3e0eb2d..00000000 --- a/src/product/Application/BillingRegistryTariffServiceInterface.php +++ /dev/null @@ -1,16 +0,0 @@ - Date: Sun, 8 Jun 2025 10:34:58 +0300 Subject: [PATCH 10/13] HP-2512 tiny --- src/product/Application/BillingRegistryService.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/product/Application/BillingRegistryService.php b/src/product/Application/BillingRegistryService.php index eac48ee8..8c4371ec 100644 --- a/src/product/Application/BillingRegistryService.php +++ b/src/product/Application/BillingRegistryService.php @@ -31,9 +31,14 @@ public function getRepresentationsByType(string $representationClass): array throw new InvalidRepresentationException("Class '$representationClass' does not exist"); } - if (class_exists($representationClass) && !is_subclass_of($representationClass, RepresentationInterface::class)) { + if (class_exists($representationClass) + && !is_subclass_of($representationClass, RepresentationInterface::class) + ) { throw new InvalidBehaviorException( - sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass) + sprintf( + 'Representation class "%s" does not implement RepresentationInterface', + $representationClass, + ) ); } From a3e00eec797367de7c06cd378578b104bda7dda5 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 11:22:48 +0300 Subject: [PATCH 11/13] HP-2512 created BillingRegistryService::getPriceTypeDefinitionByPriceTypeName() method --- .../Application/BillingRegistryService.php | 43 ++++++------- .../BillingRegistryServiceInterface.php | 13 ++-- .../PriceTypeDefinitionNotFoundException.php | 10 +++ .../BillingRegistryServiceTest.php | 61 +++++++++++++++---- .../product/Domain/Model/DummyTariffType.php | 4 +- 5 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 src/product/Exception/PriceTypeDefinitionNotFoundException.php diff --git a/src/product/Application/BillingRegistryService.php b/src/product/Application/BillingRegistryService.php index 8c4371ec..b61b62df 100644 --- a/src/product/Application/BillingRegistryService.php +++ b/src/product/Application/BillingRegistryService.php @@ -7,14 +7,11 @@ use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; use hiqdev\php\billing\product\behavior\InvalidBehaviorException; use hiqdev\php\billing\product\BillingRegistryInterface; -use hiqdev\php\billing\product\Exception\AggregateNotFoundException; +use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException; use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException; use hiqdev\php\billing\product\invoice\InvalidRepresentationException; use hiqdev\php\billing\product\invoice\RepresentationInterface; use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; -use hiqdev\php\billing\product\quantity\FractionQuantityData; -use hiqdev\php\billing\product\quantity\QuantityFormatterInterface; -use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException; use hiqdev\php\billing\product\TariffTypeDefinitionInterface; use hiqdev\php\billing\type\Type; use hiqdev\php\billing\type\TypeInterface; @@ -54,7 +51,7 @@ public function getRepresentationsByType(string $representationClass): array return $representations; } - public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface + public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface { foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) { @@ -65,23 +62,6 @@ public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDef throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found'); } - public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface { - $type = $this->convertStringTypeToType($type); - - foreach ($this->registry->priceTypes() as $priceTypeDefinition) { - if ($priceTypeDefinition->hasType($type)) { - return $priceTypeDefinition->createQuantityFormatter($data); - } - } - - throw new QuantityFormatterNotFoundException('Quantity formatter not found'); - } - - private function convertStringTypeToType(string $type): TypeInterface - { - return Type::anyId($type); - } - public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface { if (!class_exists($behaviorClassWrapper)) { @@ -113,6 +93,11 @@ public function getBehavior(string $type, string $behaviorClassWrapper): Behavio ); } + private function convertStringTypeToType(string $type): TypeInterface + { + return Type::anyId($type); + } + private function findBehaviorInPriceType( PriceTypeDefinitionInterface $priceTypeDefinition, string $behaviorClassWrapper @@ -147,15 +132,23 @@ public function getBehaviors(string $behaviorClassWrapper): \Generator public function getAggregate(string $type): AggregateInterface { - $type = $this->convertStringTypeToType($type); + return $this->getPriceTypeDefinitionByPriceTypeName($type)->getAggregate(); + } + + public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface + { + $type = $this->convertStringTypeToType($typeName); foreach ($this->registry->priceTypes() as $priceTypeDefinition) { if ($priceTypeDefinition->hasType($type)) { - return $priceTypeDefinition->getAggregate(); + return $priceTypeDefinition; } } - throw new AggregateNotFoundException('Aggregate was not found'); + throw new PriceTypeDefinitionNotFoundException(sprintf( + 'PriceTypeDefinition was not found for %s type', + $typeName, + )); } public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator diff --git a/src/product/Application/BillingRegistryServiceInterface.php b/src/product/Application/BillingRegistryServiceInterface.php index 458cd73e..13cd8972 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -10,8 +10,6 @@ use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException; use hiqdev\php\billing\product\invoice\RepresentationInterface; use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; -use hiqdev\php\billing\product\quantity\FractionQuantityData; -use hiqdev\php\billing\product\quantity\QuantityFormatterInterface; use hiqdev\php\billing\product\TariffTypeDefinitionInterface; interface BillingRegistryServiceInterface @@ -22,16 +20,21 @@ interface BillingRegistryServiceInterface */ public function getRepresentationsByType(string $representationClass): array; - public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface; - + /** + * @deprecated - please use getPriceTypeDefinitionByTypeName() method instead + * @param string $type + * @return AggregateInterface + */ public function getAggregate(string $type): AggregateInterface; + public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface; + /** * @param string $tariffName * @return TariffTypeDefinitionInterface * @throws TariffTypeDefinitionNotFoundException */ - public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface; + public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface; /** * @param string $type - full type like 'overuse,lb_capacity_unit' diff --git a/src/product/Exception/PriceTypeDefinitionNotFoundException.php b/src/product/Exception/PriceTypeDefinitionNotFoundException.php new file mode 100644 index 00000000..ec0d8ef2 --- /dev/null +++ b/src/product/Exception/PriceTypeDefinitionNotFoundException.php @@ -0,0 +1,10 @@ +registryService->getRepresentationsByType('InvalidClass'); } - public function testGetAggregateThrowsExceptionWhenNotFound(): void - { - $this->expectException(AggregateNotFoundException::class); - $this->registryService->getAggregate('non-existent-type'); - } - public function testGetRepresentationsByInterfaceReturnsAllRepresentations(): void { $tariffType = new DummyTariffType(); @@ -170,19 +165,59 @@ public function testGetBehaviorThrowsExceptionWhenNotFound(): void $this->registryService->getBehavior('non-existent-type', TestBehavior::class); } - public function testGetTariffDefinitionByName(): void + public function testGetTariffDefinitionByTariffName(): void { $tariffType = new DummyTariffType(); $tariffTypeDefinition = new TariffTypeDefinition($tariffType); - $dummyBehavior = new TestBehavior('dummy'); + + $this->registry->addTariffType($tariffTypeDefinition); + + $tariffTypeDefinition = $this->registryService->getTariffTypeDefinitionByTariffName('dummy'); + + $this->assertSame($tariffType->name(), $tariffTypeDefinition->tariffType()->name()); + } + + public function testGetTariffDefinitionByTariffName_ThrowsException(): void + { + $tariffType = new DummyTariffType(); + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); + + $this->registry->addTariffType($tariffTypeDefinition); + + $this->expectException(TariffTypeDefinitionNotFoundException::class); + $this->registryService->getTariffTypeDefinitionByTariffName('fake'); + } + + public function testGetPriceTypeDefinitionByPriceTypeName(): void + { + $tariffTypeDefinition = new TariffTypeDefinition(new DummyTariffType()); + $type = Type::anyId('dummy'); $tariffTypeDefinition - ->withBehaviors() - ->attach($dummyBehavior); + ->withPrices() + ->priceType($type) + ->end() + ->end(); $this->registry->addTariffType($tariffTypeDefinition); - $tariff = $this->registryService->getTariffTypeDefinitionByName('dummy'); + $priceTypeDefinition = $this->registryService->getPriceTypeDefinitionByPriceTypeName('dummy'); + + $this->assertSame($type, $priceTypeDefinition->type()); + } + + public function testGetPriceTypeDefinitionByPriceTypeName_ThrowsException(): void + { + $tariffTypeDefinition = new TariffTypeDefinition(new DummyTariffType()); + $type = Type::anyId('dummy'); + $tariffTypeDefinition + ->withPrices() + ->priceType($type) + ->end() + ->end(); + + $this->registry->addTariffType($tariffTypeDefinition); - $this->assertSame($tariffType->name(), $tariff->tariffType()->name()); + $this->expectException(PriceTypeDefinitionNotFoundException::class); + $this->registryService->getPriceTypeDefinitionByPriceTypeName('fake'); } } diff --git a/tests/unit/product/Domain/Model/DummyTariffType.php b/tests/unit/product/Domain/Model/DummyTariffType.php index e5c98c5d..0e6e086e 100644 --- a/tests/unit/product/Domain/Model/DummyTariffType.php +++ b/tests/unit/product/Domain/Model/DummyTariffType.php @@ -4,11 +4,9 @@ class DummyTariffType extends TariffType { - public string $name = 'dummy'; - public function name(): string { - return $this->name; + return 'dummy'; } public function label(): string From 5f0f6496e85fbba1f6559e6d3730d720fa2cf976 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 11:33:49 +0300 Subject: [PATCH 12/13] HP-2512 tiny --- src/product/Application/BillingRegistryServiceInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/product/Application/BillingRegistryServiceInterface.php b/src/product/Application/BillingRegistryServiceInterface.php index 13cd8972..fdaf8230 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -21,7 +21,7 @@ interface BillingRegistryServiceInterface public function getRepresentationsByType(string $representationClass): array; /** - * @deprecated - please use getPriceTypeDefinitionByTypeName() method instead + * @deprecated - please use getPriceTypeDefinitionByPriceTypeName() method instead * @param string $type * @return AggregateInterface */ From f146bba78b1a21c3db43c3ee822f55878a11823e Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 8 Jun 2025 11:35:17 +0300 Subject: [PATCH 13/13] HP-2512 tiny --- src/product/Application/BillingRegistryServiceInterface.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/product/Application/BillingRegistryServiceInterface.php b/src/product/Application/BillingRegistryServiceInterface.php index fdaf8230..7d38ddb3 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -7,6 +7,7 @@ use hiqdev\php\billing\product\behavior\BehaviorInterface; use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; use hiqdev\php\billing\product\behavior\InvalidBehaviorException; +use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException; use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException; use hiqdev\php\billing\product\invoice\RepresentationInterface; use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; @@ -27,6 +28,11 @@ public function getRepresentationsByType(string $representationClass): array; */ public function getAggregate(string $type): AggregateInterface; + /** + * @param string $typeName + * @return PriceTypeDefinitionInterface + * @throws PriceTypeDefinitionNotFoundException + */ public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface; /**