|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace hiqdev\php\billing\tests\unit\product\Application; |
| 4 | + |
| 5 | +use hiqdev\php\billing\product\Application\BillingRegistryService; |
| 6 | +use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; |
| 7 | +use hiqdev\php\billing\product\BillingRegistry; |
| 8 | +use hiqdev\php\billing\product\Exception\AggregateNotFoundException; |
| 9 | +use hiqdev\php\billing\product\invoice\InvalidRepresentationException; |
| 10 | +use hiqdev\php\billing\product\TariffTypeDefinition; |
| 11 | +use hiqdev\php\billing\tests\unit\product\behavior\FakeBehavior; |
| 12 | +use hiqdev\php\billing\tests\unit\product\behavior\TestBehavior; |
| 13 | +use hiqdev\php\billing\tests\unit\product\Domain\Model\DummyTariffType; |
| 14 | +use hiqdev\php\billing\tests\unit\product\Domain\Model\FakeTariffType; |
| 15 | +use hiqdev\php\billing\type\Type; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class BillingRegistryServiceTest extends TestCase |
| 19 | +{ |
| 20 | + private BillingRegistry $registry; |
| 21 | + |
| 22 | + private BillingRegistryService $registryService; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->registry = new BillingRegistry(); |
| 27 | + $this->registryService = new BillingRegistryService($this->registry); |
| 28 | + } |
| 29 | + |
| 30 | + public function testGetRepresentationsByTypeThrowsOnInvalidClass(): void |
| 31 | + { |
| 32 | + $this->expectException(InvalidRepresentationException::class); |
| 33 | + $this->registryService->getRepresentationsByType('InvalidClass'); |
| 34 | + } |
| 35 | + |
| 36 | + public function testGetAggregateThrowsExceptionWhenNotFound(): void |
| 37 | + { |
| 38 | + $this->expectException(AggregateNotFoundException::class); |
| 39 | + $this->registryService->getAggregate('non-existent-type'); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGetBehavior(): void |
| 43 | + { |
| 44 | + $tariffType = new DummyTariffType(); |
| 45 | + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); |
| 46 | + $dummyBehavior = new TestBehavior('dummy'); |
| 47 | + $type = Type::anyId('dummy'); |
| 48 | + $tariffTypeDefinition |
| 49 | + ->withPrices() |
| 50 | + ->priceType($type) |
| 51 | + ->withBehaviors() |
| 52 | + ->attach($dummyBehavior); |
| 53 | + |
| 54 | + $this->registry->addTariffType($tariffTypeDefinition); |
| 55 | + |
| 56 | + $behavior = $this->registryService->getBehavior($type->getName(), TestBehavior::class); |
| 57 | + |
| 58 | + $this->assertSame($dummyBehavior->getContext(), $behavior->getContext()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void |
| 62 | + { |
| 63 | + $tariffType = new DummyTariffType(); |
| 64 | + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); |
| 65 | + $type1 = Type::anyId('type,dummy1'); |
| 66 | + $type2 = Type::anyId('type,dummy2'); |
| 67 | + $dummyBehavior1 = new TestBehavior('dummy 1'); |
| 68 | + $dummyBehavior2 = new TestBehavior('dummy 2'); |
| 69 | + $dummyBehavior3 = new FakeBehavior('dummy 3'); |
| 70 | + |
| 71 | + $tariffTypeDefinition |
| 72 | + ->withPrices() |
| 73 | + ->priceType($type1) |
| 74 | + ->withBehaviors() |
| 75 | + ->attach($dummyBehavior1) |
| 76 | + ->end() |
| 77 | + ->end() |
| 78 | + ->priceType($type2) |
| 79 | + ->withBehaviors() |
| 80 | + ->attach($dummyBehavior2) |
| 81 | + ->attach($dummyBehavior3) |
| 82 | + ->end() |
| 83 | + ->end() |
| 84 | + ->end(); |
| 85 | + |
| 86 | + $this->registry->addTariffType($tariffTypeDefinition); |
| 87 | + |
| 88 | + $behavior = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); |
| 89 | + $this->assertSame($dummyBehavior1->getContext(), $behavior->getContext()); |
| 90 | + |
| 91 | + $behavior = $this->registryService->getBehavior($type2->getName(), TestBehavior::class); |
| 92 | + $this->assertSame($dummyBehavior2->getContext(), $behavior->getContext()); |
| 93 | + |
| 94 | + $behavior = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); |
| 95 | + $this->assertSame($dummyBehavior3->getContext(), $behavior->getContext()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void |
| 99 | + { |
| 100 | + $tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType()); |
| 101 | + $testBehavior = new TestBehavior('dummy'); |
| 102 | + $type1 = Type::anyId('type,dummy1'); |
| 103 | + $tariffTypeDefinition1 |
| 104 | + ->withPrices() |
| 105 | + ->priceType($type1) |
| 106 | + ->withBehaviors() |
| 107 | + ->attach($testBehavior) |
| 108 | + ->end() |
| 109 | + ->end() |
| 110 | + ->end(); |
| 111 | + |
| 112 | + $tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType()); |
| 113 | + $fakeBehavior = new FakeBehavior('dummy'); |
| 114 | + $type2 = Type::anyId('type,dummy2'); |
| 115 | + $tariffTypeDefinition2 |
| 116 | + ->withPrices() |
| 117 | + ->priceType($type2) |
| 118 | + ->withBehaviors() |
| 119 | + ->attach($fakeBehavior) |
| 120 | + ->end() |
| 121 | + ->end() |
| 122 | + ->end(); |
| 123 | + |
| 124 | + $this->registry->addTariffType($tariffTypeDefinition1); |
| 125 | + $this->registry->addTariffType($tariffTypeDefinition2); |
| 126 | + |
| 127 | + /** @var TestBehavior $testBehaviorActual */ |
| 128 | + $testBehaviorActual = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); |
| 129 | + $this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext()); |
| 130 | + |
| 131 | + /** @var FakeBehavior $fakeBehaviorActual */ |
| 132 | + $fakeBehaviorActual = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); |
| 133 | + $this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext()); |
| 134 | + } |
| 135 | + |
| 136 | + public function testGetBehaviorThrowsExceptionWhenNotFound(): void |
| 137 | + { |
| 138 | + $this->expectException(BehaviorNotFoundException::class); |
| 139 | + $this->registryService->getBehavior('non-existent-type', TestBehavior::class); |
| 140 | + } |
| 141 | + |
| 142 | + // public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void |
| 143 | +// { |
| 144 | +// $this->expectException(QuantityFormatterNotFoundException::class); |
| 145 | +// $this->registryService->createQuantityFormatter('non-existent-type', $this->createMock(FractionQuantityData::class)); |
| 146 | +// } |
| 147 | +} |
0 commit comments