Skip to content

Commit ef98123

Browse files
HP-1751 created BillingRegistryTest phpunit test
1 parent 4332ccb commit ef98123

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

src/product/BillingRegistry.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
66
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
7+
use hiqdev\php\billing\product\Exception\BillingRegistryLockedException;
78
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
89
use hiqdev\php\billing\product\invoice\RepresentationInterface;
910
use hiqdev\php\billing\product\price\PriceTypeDefinition;
@@ -24,7 +25,7 @@ class BillingRegistry implements BillingRegistryInterface
2425
public function addTariffType(TariffTypeDefinitionInterface $tariffType): void
2526
{
2627
if ($this->locked) {
27-
throw new \RuntimeException("BillingRegistry is locked and cannot be modified.");
28+
throw new BillingRegistryLockedException("BillingRegistry is locked and cannot be modified.");
2829
}
2930

3031
$this->tariffTypes[] = $tariffType;

src/product/Domain/Model/TariffTypeInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
interface TariffTypeInterface
66
{
7+
public function name(): string;
78

9+
public function label(): string;
810
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\Exception;
4+
5+
class BillingRegistryLockedException extends \LogicException
6+
{
7+
8+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

tests/unit/product/DummyBehavior.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\tests\unit\product;
4+
5+
use hiqdev\billing\registry\behavior\Behavior;
6+
7+
class DummyBehavior extends Behavior
8+
{
9+
private $context;
10+
11+
public function __construct($context)
12+
{
13+
$this->context = $context;
14+
}
15+
16+
public function getContext()
17+
{
18+
return $this->context;
19+
}
20+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\tests\unit\product;
4+
5+
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
6+
7+
class DummyTariffType implements TariffTypeInterface {
8+
public function name(): string
9+
{
10+
return 'dummy';
11+
}
12+
13+
public function label(): string
14+
{
15+
return 'Dummy';
16+
}
17+
}

0 commit comments

Comments
 (0)