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/BillingRegistryService.php b/src/product/Application/BillingRegistryService.php index b0dcba24..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; @@ -31,9 +28,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, + ) ); } @@ -49,36 +51,15 @@ public function getRepresentationsByType(string $representationClass): array return $representations; } - public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface + public function getTariffTypeDefinitionByTariffName(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); - } - 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); + throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found'); } public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface @@ -112,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 @@ -146,28 +132,23 @@ public function getBehaviors(string $behaviorClassWrapper): \Generator public function getAggregate(string $type): AggregateInterface { - $type = $this->convertStringTypeToType($type); - - foreach ($this->registry->priceTypes() as $priceTypeDefinition) { - if ($priceTypeDefinition->hasType($type)) { - return $priceTypeDefinition->getAggregate(); - } - } - - throw new AggregateNotFoundException('Aggregate was not found'); + return $this->getPriceTypeDefinitionByPriceTypeName($type)->getAggregate(); } - public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface + public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface { - $tariffType = $behavior->getTariffType(); + $type = $this->convertStringTypeToType($typeName); - foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { - if ($tariffTypeDefinition->belongToTariffType($tariffType)) { - return $tariffTypeDefinition; + foreach ($this->registry->priceTypes() as $priceTypeDefinition) { + if ($priceTypeDefinition->hasType($type)) { + return $priceTypeDefinition; } } - throw new TariffTypeDefinitionNotFoundException('Tariff type definition 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 9f9c6ac7..7d38ddb3 100644 --- a/src/product/Application/BillingRegistryServiceInterface.php +++ b/src/product/Application/BillingRegistryServiceInterface.php @@ -7,13 +7,13 @@ 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; -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 { /** * @param string $representationClass @@ -21,8 +21,50 @@ interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseIn */ public function getRepresentationsByType(string $representationClass): array; - public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface; - + /** + * @deprecated - please use getPriceTypeDefinitionByPriceTypeName() method instead + * @param string $type + * @return AggregateInterface + */ public function getAggregate(string $type): AggregateInterface; + /** + * @param string $typeName + * @return PriceTypeDefinitionInterface + * @throws PriceTypeDefinitionNotFoundException + */ + public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface; + + /** + * @param string $tariffName + * @return TariffTypeDefinitionInterface + * @throws TariffTypeDefinitionNotFoundException + */ + public function getTariffTypeDefinitionByTariffName(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/BillingRegistryTariffServiseInterface.php b/src/product/Application/BillingRegistryTariffServiseInterface.php deleted file mode 100644 index 595ffc8b..00000000 --- a/src/product/Application/BillingRegistryTariffServiseInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -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/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); - } -} diff --git a/tests/unit/product/Application/BillingRegistryServiceTest.php b/tests/unit/product/Application/BillingRegistryServiceTest.php index 939646fa..d936d24f 100644 --- a/tests/unit/product/Application/BillingRegistryServiceTest.php +++ b/tests/unit/product/Application/BillingRegistryServiceTest.php @@ -2,12 +2,11 @@ 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; -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\TariffTypeDefinition; @@ -15,6 +14,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; @@ -36,12 +36,6 @@ public function testGetRepresentationsByTypeThrowsOnInvalidClass(): void $this->registryService->getRepresentationsByType('InvalidClass'); } - public function testGetAggregateThrowsExceptionWhenNotFound(): void - { - $this->expectException(AggregateNotFoundException::class); - $this->registryService->getAggregate('non-existent-type'); - } - public function testGetRepresentationsByInterfaceReturnsAllRepresentations(): void { $tariffType = new DummyTariffType(); @@ -49,10 +43,14 @@ public function testGetRepresentationsByInterfaceReturnsAllRepresentations(): vo $tariffTypeDefinition ->withPrices() - ->priceType(Type::anyId('dummy')) + ->priceType(Type::anyId('dummy 1')) ->documentRepresentation() - ->attach(new InvoiceRepresentation("Invoice")) - ->attach(new PaymentRequestRepresentation("Payment Request")) + ->attach(new TestRepresentation("Representation 1")) + ->end() + ->end() + ->priceType(Type::anyId('dummy 2')) + ->documentRepresentation() + ->attach(new TestRepresentation("Representation 2")) ->end() ->end() ->end(); @@ -72,9 +70,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); @@ -95,17 +96,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); @@ -127,11 +128,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()); @@ -139,11 +140,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); @@ -164,9 +165,59 @@ public function testGetBehaviorThrowsExceptionWhenNotFound(): void $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)); -// } + public function testGetTariffDefinitionByTariffName(): void + { + $tariffType = new DummyTariffType(); + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); + + $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 + ->withPrices() + ->priceType($type) + ->end() + ->end(); + + $this->registry->addTariffType($tariffTypeDefinition); + + $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->expectException(PriceTypeDefinitionNotFoundException::class); + $this->registryService->getPriceTypeDefinitionByPriceTypeName('fake'); + } } diff --git a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php b/tests/unit/product/Application/BillingRegistryTariffServiceTest.php deleted file mode 100644 index 03e86912..00000000 --- a/tests/unit/product/Application/BillingRegistryTariffServiceTest.php +++ /dev/null @@ -1,61 +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->getTariffDefinitionByName('dummy'); - - $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->getTariffDefinitionByName('dummy'); - $this->assertTrue($this->registryService->hasBehaviour($tariff, TestBehavior::class)); - } - -} 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 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 @@ +