|
| 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 BillingRegistryBehaviorServiceTest 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 testGetBehavior(): void |
| 31 | + { |
| 32 | + $tariffType = new DummyTariffType(); |
| 33 | + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); |
| 34 | + $dummyBehavior = new TestBehavior('dummy'); |
| 35 | + $type = Type::anyId('dummy'); |
| 36 | + $tariffTypeDefinition |
| 37 | + ->withPrices() |
| 38 | + ->priceType($type) |
| 39 | + ->withBehaviors() |
| 40 | + ->attach($dummyBehavior); |
| 41 | + |
| 42 | + $this->registry->addTariffType($tariffTypeDefinition); |
| 43 | + |
| 44 | + $behavior = $this->registryService->getBehavior($type->getName(), TestBehavior::class); |
| 45 | + |
| 46 | + $this->assertSame($dummyBehavior->getContext(), $behavior->getContext()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void |
| 50 | + { |
| 51 | + $tariffType = new DummyTariffType(); |
| 52 | + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); |
| 53 | + $type1 = Type::anyId('type,dummy1'); |
| 54 | + $type2 = Type::anyId('type,dummy2'); |
| 55 | + $dummyBehavior1 = new TestBehavior('dummy 1'); |
| 56 | + $dummyBehavior2 = new TestBehavior('dummy 2'); |
| 57 | + $dummyBehavior3 = new FakeBehavior('dummy 3'); |
| 58 | + |
| 59 | + $tariffTypeDefinition |
| 60 | + ->withPrices() |
| 61 | + ->priceType($type1) |
| 62 | + ->withBehaviors() |
| 63 | + ->attach($dummyBehavior1) |
| 64 | + ->end() |
| 65 | + ->end() |
| 66 | + ->priceType($type2) |
| 67 | + ->withBehaviors() |
| 68 | + ->attach($dummyBehavior2) |
| 69 | + ->attach($dummyBehavior3) |
| 70 | + ->end() |
| 71 | + ->end() |
| 72 | + ->end(); |
| 73 | + |
| 74 | + $this->registry->addTariffType($tariffTypeDefinition); |
| 75 | + |
| 76 | + $behavior = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); |
| 77 | + $this->assertSame($dummyBehavior1->getContext(), $behavior->getContext()); |
| 78 | + |
| 79 | + $behavior = $this->registryService->getBehavior($type2->getName(), TestBehavior::class); |
| 80 | + $this->assertSame($dummyBehavior2->getContext(), $behavior->getContext()); |
| 81 | + |
| 82 | + $behavior = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); |
| 83 | + $this->assertSame($dummyBehavior3->getContext(), $behavior->getContext()); |
| 84 | + } |
| 85 | + |
| 86 | + public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void |
| 87 | + { |
| 88 | + $tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType()); |
| 89 | + $testBehavior = new TestBehavior('dummy'); |
| 90 | + $type1 = Type::anyId('type,dummy1'); |
| 91 | + $tariffTypeDefinition1 |
| 92 | + ->withPrices() |
| 93 | + ->priceType($type1) |
| 94 | + ->withBehaviors() |
| 95 | + ->attach($testBehavior) |
| 96 | + ->end() |
| 97 | + ->end() |
| 98 | + ->end(); |
| 99 | + |
| 100 | + $tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType()); |
| 101 | + $fakeBehavior = new FakeBehavior('dummy'); |
| 102 | + $type2 = Type::anyId('type,dummy2'); |
| 103 | + $tariffTypeDefinition2 |
| 104 | + ->withPrices() |
| 105 | + ->priceType($type2) |
| 106 | + ->withBehaviors() |
| 107 | + ->attach($fakeBehavior) |
| 108 | + ->end() |
| 109 | + ->end() |
| 110 | + ->end(); |
| 111 | + |
| 112 | + $this->registry->addTariffType($tariffTypeDefinition1); |
| 113 | + $this->registry->addTariffType($tariffTypeDefinition2); |
| 114 | + |
| 115 | + /** @var TestBehavior $testBehaviorActual */ |
| 116 | + $testBehaviorActual = $this->registryService->getBehavior($type1->getName(), TestBehavior::class); |
| 117 | + $this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext()); |
| 118 | + |
| 119 | + /** @var FakeBehavior $fakeBehaviorActual */ |
| 120 | + $fakeBehaviorActual = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class); |
| 121 | + $this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testGetBehaviorThrowsExceptionWhenNotFound(): void |
| 125 | + { |
| 126 | + $this->expectException(BehaviorNotFoundException::class); |
| 127 | + $this->registryService->getBehavior('non-existent-type', TestBehavior::class); |
| 128 | + } |
| 129 | +} |
0 commit comments