Skip to content

Commit 277995a

Browse files
ValeriyShnurovoyDrahmaSilverFire
authored
HP-2482/Close_prices_and_notices_for_client_and_reseller (#100)
Co-authored-by: Drahma <[email protected]> Co-authored-by: Dmytro Naumenko <[email protected]>
1 parent b0b60e8 commit 277995a

8 files changed

+270
-30
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hiqdev\php\billing\product\Application;
6+
7+
use Generator;
8+
use hiqdev\php\billing\product\behavior\BehaviorInterface;
9+
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
10+
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
11+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
12+
13+
interface BillingRegistryBehaviorServiceInterface
14+
{
15+
/**
16+
* @param string $type - full type like 'overuse,lb_capacity_unit'
17+
* @param string $behaviorClassWrapper
18+
* @return BehaviorInterface
19+
* @throws BehaviorNotFoundException
20+
* @throws InvalidBehaviorException
21+
*/
22+
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
23+
24+
25+
/**
26+
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
27+
*
28+
* @param string $behaviorClassWrapper
29+
* @return Generator<BehaviorInterface>
30+
*/
31+
public function getBehaviors(string $behaviorClassWrapper): Generator;
32+
33+
/**
34+
* Find all PriceTypeDefinition in registry by specified Behavior class.
35+
*
36+
* @param string $behaviorClassWrapper
37+
* @return Generator<PriceTypeDefinitionInterface>
38+
*/
39+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
40+
41+
}

src/product/Application/BillingRegistryService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public function getRepresentationsByType(string $representationClass): array
4949
return $representations;
5050
}
5151

52+
public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
53+
{
54+
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
55+
if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
56+
return $tariffTypeDefinition;
57+
}
58+
}
59+
return null;
60+
}
61+
62+
public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool
63+
{
64+
return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper);
65+
}
66+
5267
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
5368
$type = $this->convertStringTypeToType($type);
5469

src/product/Application/BillingRegistryServiceInterface.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
1414
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
1515

16-
interface BillingRegistryServiceInterface
16+
interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
1717
{
1818
/**
1919
* @param string $representationClass
@@ -23,32 +23,6 @@ public function getRepresentationsByType(string $representationClass): array;
2323

2424
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface;
2525

26-
/**
27-
* @param string $type - full type like 'overuse,lb_capacity_unit'
28-
* @param string $behaviorClassWrapper
29-
* @return BehaviorInterface
30-
* @throws BehaviorNotFoundException
31-
* @throws InvalidBehaviorException
32-
*/
33-
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
34-
35-
/**
36-
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
37-
*
38-
* @param string $behaviorClassWrapper
39-
* @return Generator<BehaviorInterface>
40-
*/
41-
public function getBehaviors(string $behaviorClassWrapper): Generator;
42-
4326
public function getAggregate(string $type): AggregateInterface;
4427

45-
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;
46-
47-
/**
48-
* Find all PriceTypeDefinition in registry by specified Behavior class.
49-
*
50-
* @param string $behaviorClassWrapper
51-
* @return Generator<PriceTypeDefinitionInterface>
52-
*/
53-
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
54-
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hiqdev\php\billing\product\Application;
6+
7+
use hiqdev\php\billing\product\behavior\BehaviorInterface;
8+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
9+
10+
interface BillingRegistryTariffServiseInterface
11+
{
12+
13+
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;
14+
15+
public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface;
16+
17+
public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool;
18+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

tests/unit/product/Application/BillingRegistryServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function testGetBehaviorThrowsExceptionWhenNotFound(): void
164164
$this->registryService->getBehavior('non-existent-type', TestBehavior::class);
165165
}
166166

167-
// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void
167+
// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void
168168
// {
169169
// $this->expectException(QuantityFormatterNotFoundException::class);
170170
// $this->registryService->createQuantityFormatter('non-existent-type', $this->createMock(FractionQuantityData::class));
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 BillingRegistryTariffServiceTest 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 testGetTariffDefinitionByName(): void
31+
{
32+
$tariffType = new DummyTariffType();
33+
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
34+
$dummyBehavior = new TestBehavior('dummy');
35+
$tariffTypeDefinition
36+
->withBehaviors()
37+
->attach($dummyBehavior);
38+
39+
$this->registry->addTariffType($tariffTypeDefinition);
40+
41+
$tariff = $this->registryService->getTariffDefinitionByName('dummy');
42+
43+
$this->assertSame($tariffType->name(), $tariff->tariffType()->name());
44+
}
45+
46+
public function testCheckBehaviorInTariff(): void
47+
{
48+
$tariffType = new DummyTariffType();
49+
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
50+
$dummyBehavior = new TestBehavior('dummy');
51+
$tariffTypeDefinition
52+
->withBehaviors()
53+
->attach($dummyBehavior);
54+
55+
$this->registry->addTariffType($tariffTypeDefinition);
56+
57+
$tariff = $this->registryService->getTariffDefinitionByName('dummy');
58+
$this->assertTrue($this->registryService->hasBehaviour($tariff, TestBehavior::class));
59+
}
60+
61+
}

tests/unit/product/Domain/Model/DummyTariffType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
class DummyTariffType extends TariffType
66
{
7+
public string $name = 'dummy';
8+
79
public function name(): string
810
{
9-
return 'dummy';
11+
return $this->name;
1012
}
1113

1214
public function label(): string

0 commit comments

Comments
 (0)