Skip to content

Commit 8eba047

Browse files
HP-2419 Created PHPUnit test for BillingRegistryService
1 parent 27bec10 commit 8eba047

2 files changed

Lines changed: 147 additions & 124 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

tests/unit/product/BillingRegistryTest.php

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
use hiqdev\php\billing\product\price\PriceTypeDefinition;
88
use hiqdev\php\billing\product\TariffTypeDefinition;
99
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
10-
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
11-
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
12-
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
13-
use hiqdev\php\billing\tests\unit\product\behavior\FakeBehavior;
14-
use hiqdev\php\billing\tests\unit\product\behavior\TestBehavior;
1510
use hiqdev\php\billing\tests\unit\product\Domain\Model\DummyTariffType;
16-
use hiqdev\php\billing\tests\unit\product\Domain\Model\FakeTariffType;
1711
use hiqdev\php\billing\type\Type;
1812
use PHPUnit\Framework\TestCase;
1913

@@ -55,122 +49,4 @@ public function testLockPreventsModification(): void
5549

5650
$this->registry->addTariffType($this->tariffTypeDefinition);
5751
}
58-
59-
public function testGetRepresentationsByTypeThrowsOnInvalidClass(): void
60-
{
61-
$this->expectException(InvalidRepresentationException::class);
62-
$this->registry->getRepresentationsByType('InvalidClass');
63-
}
64-
65-
public function testGetAggregateThrowsExceptionWhenNotFound(): void
66-
{
67-
$this->expectException(AggregateNotFoundException::class);
68-
$this->registry->getAggregate('non-existent-type');
69-
}
70-
71-
public function testGetBehavior(): void
72-
{
73-
$tariffType = new DummyTariffType();
74-
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
75-
$dummyBehavior = new TestBehavior('dummy');
76-
$type = Type::anyId('dummy');
77-
$tariffTypeDefinition
78-
->withPrices()
79-
->priceType($type)
80-
->withBehaviors()
81-
->attach($dummyBehavior);
82-
83-
$this->registry->addTariffType($tariffTypeDefinition);
84-
85-
$behavior = $this->registry->getBehavior($type->getName(), TestBehavior::class);
86-
87-
$this->assertSame($dummyBehavior->getContext(), $behavior->getContext());
88-
}
89-
90-
public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void
91-
{
92-
$tariffType = new DummyTariffType();
93-
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
94-
$type1 = Type::anyId('type,dummy1');
95-
$type2 = Type::anyId('type,dummy2');
96-
$dummyBehavior1 = new TestBehavior('dummy 1');
97-
$dummyBehavior2 = new TestBehavior('dummy 2');
98-
$dummyBehavior3 = new FakeBehavior('dummy 3');
99-
100-
$tariffTypeDefinition
101-
->withPrices()
102-
->priceType($type1)
103-
->withBehaviors()
104-
->attach($dummyBehavior1)
105-
->end()
106-
->end()
107-
->priceType($type2)
108-
->withBehaviors()
109-
->attach($dummyBehavior2)
110-
->attach($dummyBehavior3)
111-
->end()
112-
->end()
113-
->end();
114-
115-
$this->registry->addTariffType($tariffTypeDefinition);
116-
117-
$behavior = $this->registry->getBehavior($type1->getName(), TestBehavior::class);
118-
$this->assertSame($dummyBehavior1->getContext(), $behavior->getContext());
119-
120-
$behavior = $this->registry->getBehavior($type2->getName(), TestBehavior::class);
121-
$this->assertSame($dummyBehavior2->getContext(), $behavior->getContext());
122-
123-
$behavior = $this->registry->getBehavior($type2->getName(), FakeBehavior::class);
124-
$this->assertSame($dummyBehavior3->getContext(), $behavior->getContext());
125-
}
126-
127-
public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void
128-
{
129-
$tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType());
130-
$testBehavior = new TestBehavior('dummy');
131-
$type1 = Type::anyId('type,dummy1');
132-
$tariffTypeDefinition1
133-
->withPrices()
134-
->priceType($type1)
135-
->withBehaviors()
136-
->attach($testBehavior)
137-
->end()
138-
->end()
139-
->end();
140-
141-
$tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType());
142-
$fakeBehavior = new FakeBehavior('dummy');
143-
$type2 = Type::anyId('type,dummy2');
144-
$tariffTypeDefinition2
145-
->withPrices()
146-
->priceType($type2)
147-
->withBehaviors()
148-
->attach($fakeBehavior)
149-
->end()
150-
->end()
151-
->end();
152-
153-
$this->registry->addTariffType($tariffTypeDefinition1);
154-
$this->registry->addTariffType($tariffTypeDefinition2);
155-
156-
/** @var TestBehavior $testBehaviorActual */
157-
$testBehaviorActual = $this->registry->getBehavior($type1->getName(), TestBehavior::class);
158-
$this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext());
159-
160-
/** @var FakeBehavior $fakeBehaviorActual */
161-
$fakeBehaviorActual = $this->registry->getBehavior($type2->getName(), FakeBehavior::class);
162-
$this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext());
163-
}
164-
165-
public function testGetBehaviorThrowsExceptionWhenNotFound(): void
166-
{
167-
$this->expectException(BehaviorNotFoundException::class);
168-
$this->registry->getBehavior('non-existent-type', TestBehavior::class);
169-
}
170-
171-
// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void
172-
// {
173-
// $this->expectException(QuantityFormatterNotFoundException::class);
174-
// $this->registry->createQuantityFormatter('non-existent-type', $this->createMock(FractionQuantityData::class));
175-
// }
17652
}

0 commit comments

Comments
 (0)