|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace hiqdev\php\billing\tests\unit\product; |
| 4 | + |
| 5 | +use hiqdev\php\billing\product\BillingRegistry; |
| 6 | +use hiqdev\php\billing\product\Exception\BillingRegistryLockedException; |
| 7 | +use hiqdev\php\billing\product\TariffTypeDefinition; |
| 8 | +use hiqdev\php\billing\product\TariffTypeDefinitionInterface; |
| 9 | +use hiqdev\php\billing\product\Exception\AggregateNotFoundException; |
| 10 | +use hiqdev\php\billing\product\invoice\InvalidRepresentationException; |
| 11 | +use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException; |
| 12 | +use hiqdev\php\billing\product\behavior\BehaviorInterface; |
| 13 | +use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; |
| 14 | +use hiqdev\php\billing\type\Type; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class BillingRegistryTest extends TestCase |
| 18 | +{ |
| 19 | + private BillingRegistry $registry; |
| 20 | + |
| 21 | + private TariffTypeDefinitionInterface $tariffTypeDefinition; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->registry = new BillingRegistry(); |
| 26 | + $this->tariffTypeDefinition = new TariffTypeDefinition(new DummyTariffType()); |
| 27 | + } |
| 28 | + |
| 29 | + public function testAddTariffTypeAndRetrievePriceTypes(): void |
| 30 | + { |
| 31 | + $type = Type::anyId('dummy'); |
| 32 | + |
| 33 | + $this->tariffTypeDefinition->withPrices() |
| 34 | + ->priceType($type); |
| 35 | + |
| 36 | + $this->registry->addTariffType($this->tariffTypeDefinition); |
| 37 | + |
| 38 | + $priceTypes = iterator_to_array($this->registry->priceTypes()); |
| 39 | + |
| 40 | + $this->assertCount(1, $priceTypes); |
| 41 | + $this->assertSame($type, $priceTypes[0]); |
| 42 | + } |
| 43 | + |
| 44 | + public function testLockPreventsModification(): void |
| 45 | + { |
| 46 | + $this->registry->lock(); |
| 47 | + |
| 48 | + $this->expectException(BillingRegistryLockedException::class); |
| 49 | + |
| 50 | + $this->registry->addTariffType($this->tariffTypeDefinition); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetRepresentationsByTypeThrowsOnInvalidClass(): void |
| 54 | + { |
| 55 | + $this->expectException(InvalidRepresentationException::class); |
| 56 | + $this->registry->getRepresentationsByType('InvalidClass'); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGetAggregateThrowsExceptionWhenNotFound(): void |
| 60 | + { |
| 61 | + $this->expectException(AggregateNotFoundException::class); |
| 62 | + $this->registry->getAggregate('non-existent-type'); |
| 63 | + } |
| 64 | + |
| 65 | + public function testGetBehavior(): void |
| 66 | + { |
| 67 | + $tariffType = new DummyTariffType(); |
| 68 | + $tariffTypeDefinition = new TariffTypeDefinition($tariffType); |
| 69 | + $dummyBehavior = new DummyBehavior('dummy'); |
| 70 | + $tariffTypeDefinition->withBehaviors() |
| 71 | + ->attach($dummyBehavior); |
| 72 | + |
| 73 | + $this->registry->addTariffType($this->tariffTypeDefinition); |
| 74 | + $behavior = $this->registry->getBehavior($tariffType->name(), DummyBehavior::class); |
| 75 | + |
| 76 | + $this->assertSame($dummyBehavior->getContext(), $behavior->getContext()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testGetBehaviorThrowsExceptionWhenNotFound(): void |
| 80 | + { |
| 81 | + $this->expectException(BehaviorNotFoundException::class); |
| 82 | + $this->registry->getBehavior('non-existent-type', BehaviorInterface::class); |
| 83 | + } |
| 84 | + |
| 85 | +// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void |
| 86 | +// { |
| 87 | +// $this->expectException(QuantityFormatterNotFoundException::class); |
| 88 | +// $this->registry->createQuantityFormatter('non-existent-type', $this->createMock(FractionQuantityData::class)); |
| 89 | +// } |
| 90 | +} |
0 commit comments