Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace hiqdev\php\billing\product\Application;

use Generator;
use hiqdev\php\billing\product\behavior\BehaviorInterface;
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;

interface BillingRegistryBehaviorServiceInterface
{
/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
* @throws InvalidBehaviorException
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;


/**
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<BehaviorInterface>
*/
public function getBehaviors(string $behaviorClassWrapper): Generator;

/**
* Find all PriceTypeDefinition in registry by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<PriceTypeDefinitionInterface>
*/
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;

}
15 changes: 15 additions & 0 deletions src/product/Application/BillingRegistryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ public function getRepresentationsByType(string $representationClass): array
return $representations;
}

public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
{
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
return $tariffTypeDefinition;
}
}
return null;
}
Comment on lines +52 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical bug: Missing method call parentheses

The comparison on line 55 is accessing name as a property instead of calling the name() method. Based on the TariffTypeInterface (line 7), name() is a method that should be called.

-            if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
+            if ($tariffName === $tariffTypeDefinition->tariffType()->name()) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
{
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
return $tariffTypeDefinition;
}
}
return null;
}
public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
{
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
if ($tariffName === $tariffTypeDefinition->tariffType()->name()) {
return $tariffTypeDefinition;
}
}
return null;
}
🤖 Prompt for AI Agents
In src/product/Application/BillingRegistryService.php lines 52 to 60, the code
incorrectly accesses the name as a property on line 55. Replace the property
access with a method call by changing
`$tariffTypeDefinition->tariffType()->name` to
`$tariffTypeDefinition->tariffType()->name()`. This ensures the name() method is
properly invoked as defined in the TariffTypeInterface.


public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool
{
return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper);
}

public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
$type = $this->convertStringTypeToType($type);

Expand Down
30 changes: 2 additions & 28 deletions src/product/Application/BillingRegistryServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;

interface BillingRegistryServiceInterface
interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in interface name.

There's a typo in the interface name: BillingRegistryTariffServiseInterface should be BillingRegistryTariffServiceInterface (missing 'c' in "Service").

-interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
+interface BillingRegistryServiceInterface extends BillingRegistryTariffServiceInterface, BillingRegistryBehaviorServiceInterface
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
interface BillingRegistryServiceInterface extends BillingRegistryTariffServiceInterface, BillingRegistryBehaviorServiceInterface
🤖 Prompt for AI Agents
In src/product/Application/BillingRegistryServiceInterface.php at line 16, fix
the typo in the interface name by changing BillingRegistryTariffServiseInterface
to BillingRegistryTariffServiceInterface, adding the missing 'c' in "Service".

{
/**
* @param string $representationClass
Expand All @@ -23,32 +23,6 @@ public function getRepresentationsByType(string $representationClass): array;

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

/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
* @throws InvalidBehaviorException
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;

/**
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<BehaviorInterface>
*/
public function getBehaviors(string $behaviorClassWrapper): Generator;

public function getAggregate(string $type): AggregateInterface;

public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;

/**
* Find all PriceTypeDefinition in registry by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<PriceTypeDefinitionInterface>
*/
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
}
}
18 changes: 18 additions & 0 deletions src/product/Application/BillingRegistryTariffServiseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace hiqdev\php\billing\product\Application;

use hiqdev\php\billing\product\behavior\BehaviorInterface;
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;

interface BillingRegistryTariffServiseInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Fix typo in interface name

The interface name contains a typo: "BillingRegistryTariffServiseInterface" should be "BillingRegistryTariffServiceInterface" (missing 'c' in "Service").

-interface BillingRegistryTariffServiseInterface
+interface BillingRegistryTariffServiceInterface

This typo will need to be corrected everywhere this interface is referenced.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
interface BillingRegistryTariffServiseInterface
-interface BillingRegistryTariffServiseInterface
+interface BillingRegistryTariffServiceInterface
🤖 Prompt for AI Agents
In src/product/Application/BillingRegistryTariffServiseInterface.php at line 10,
correct the typo in the interface name from
"BillingRegistryTariffServiseInterface" to
"BillingRegistryTariffServiceInterface" by adding the missing 'c'. Then, update
all references to this interface throughout the codebase to use the corrected
name to ensure consistency and avoid errors.

{

public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;

public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface;

public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool;
}
129 changes: 129 additions & 0 deletions tests/unit/product/Application/BillingRegistryBehaviorServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\tests\unit\product\Application;

use hiqdev\php\billing\product\Application\BillingRegistryService;
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
use hiqdev\php\billing\product\BillingRegistry;
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
use hiqdev\php\billing\product\TariffTypeDefinition;
use hiqdev\php\billing\tests\unit\product\behavior\FakeBehavior;
use hiqdev\php\billing\tests\unit\product\behavior\TestBehavior;
use hiqdev\php\billing\tests\unit\product\Domain\Model\DummyTariffType;
use hiqdev\php\billing\tests\unit\product\Domain\Model\FakeTariffType;
use hiqdev\php\billing\type\Type;
use PHPUnit\Framework\TestCase;

class BillingRegistryBehaviorServiceTest extends TestCase
{
private BillingRegistry $registry;

private BillingRegistryService $registryService;

protected function setUp(): void
{
$this->registry = new BillingRegistry();
$this->registryService = new BillingRegistryService($this->registry);
}

public function testGetBehavior(): void
{
$tariffType = new DummyTariffType();
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
$dummyBehavior = new TestBehavior('dummy');
$type = Type::anyId('dummy');
$tariffTypeDefinition
->withPrices()
->priceType($type)
->withBehaviors()
->attach($dummyBehavior);

$this->registry->addTariffType($tariffTypeDefinition);

$behavior = $this->registryService->getBehavior($type->getName(), TestBehavior::class);

$this->assertSame($dummyBehavior->getContext(), $behavior->getContext());
}

public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void
{
$tariffType = new DummyTariffType();
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
$type1 = Type::anyId('type,dummy1');
$type2 = Type::anyId('type,dummy2');
$dummyBehavior1 = new TestBehavior('dummy 1');
$dummyBehavior2 = new TestBehavior('dummy 2');
$dummyBehavior3 = new FakeBehavior('dummy 3');

$tariffTypeDefinition
->withPrices()
->priceType($type1)
->withBehaviors()
->attach($dummyBehavior1)
->end()
->end()
->priceType($type2)
->withBehaviors()
->attach($dummyBehavior2)
->attach($dummyBehavior3)
->end()
->end()
->end();

$this->registry->addTariffType($tariffTypeDefinition);

$behavior = $this->registryService->getBehavior($type1->getName(), TestBehavior::class);
$this->assertSame($dummyBehavior1->getContext(), $behavior->getContext());

$behavior = $this->registryService->getBehavior($type2->getName(), TestBehavior::class);
$this->assertSame($dummyBehavior2->getContext(), $behavior->getContext());

$behavior = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class);
$this->assertSame($dummyBehavior3->getContext(), $behavior->getContext());
}

public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void
{
$tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType());
$testBehavior = new TestBehavior('dummy');
$type1 = Type::anyId('type,dummy1');
$tariffTypeDefinition1
->withPrices()
->priceType($type1)
->withBehaviors()
->attach($testBehavior)
->end()
->end()
->end();

$tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType());
$fakeBehavior = new FakeBehavior('dummy');
$type2 = Type::anyId('type,dummy2');
$tariffTypeDefinition2
->withPrices()
->priceType($type2)
->withBehaviors()
->attach($fakeBehavior)
->end()
->end()
->end();

$this->registry->addTariffType($tariffTypeDefinition1);
$this->registry->addTariffType($tariffTypeDefinition2);

/** @var TestBehavior $testBehaviorActual */
$testBehaviorActual = $this->registryService->getBehavior($type1->getName(), TestBehavior::class);
$this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext());

/** @var FakeBehavior $fakeBehaviorActual */
$fakeBehaviorActual = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class);
$this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext());
}

public function testGetBehaviorThrowsExceptionWhenNotFound(): void
{
$this->expectException(BehaviorNotFoundException::class);
$this->registryService->getBehavior('non-existent-type', TestBehavior::class);
}
}
100 changes: 0 additions & 100 deletions tests/unit/product/Application/BillingRegistryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,106 +39,6 @@ public function testGetAggregateThrowsExceptionWhenNotFound(): void
$this->registryService->getAggregate('non-existent-type');
}

public function testGetBehavior(): void
{
$tariffType = new DummyTariffType();
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
$dummyBehavior = new TestBehavior('dummy');
$type = Type::anyId('dummy');
$tariffTypeDefinition
->withPrices()
->priceType($type)
->withBehaviors()
->attach($dummyBehavior);

$this->registry->addTariffType($tariffTypeDefinition);

$behavior = $this->registryService->getBehavior($type->getName(), TestBehavior::class);

$this->assertSame($dummyBehavior->getContext(), $behavior->getContext());
}

public function testGetBehavior_WithMultipleTariffTypeDefinitions(): void
{
$tariffType = new DummyTariffType();
$tariffTypeDefinition = new TariffTypeDefinition($tariffType);
$type1 = Type::anyId('type,dummy1');
$type2 = Type::anyId('type,dummy2');
$dummyBehavior1 = new TestBehavior('dummy 1');
$dummyBehavior2 = new TestBehavior('dummy 2');
$dummyBehavior3 = new FakeBehavior('dummy 3');

$tariffTypeDefinition
->withPrices()
->priceType($type1)
->withBehaviors()
->attach($dummyBehavior1)
->end()
->end()
->priceType($type2)
->withBehaviors()
->attach($dummyBehavior2)
->attach($dummyBehavior3)
->end()
->end()
->end();

$this->registry->addTariffType($tariffTypeDefinition);

$behavior = $this->registryService->getBehavior($type1->getName(), TestBehavior::class);
$this->assertSame($dummyBehavior1->getContext(), $behavior->getContext());

$behavior = $this->registryService->getBehavior($type2->getName(), TestBehavior::class);
$this->assertSame($dummyBehavior2->getContext(), $behavior->getContext());

$behavior = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class);
$this->assertSame($dummyBehavior3->getContext(), $behavior->getContext());
}

public function testGetBehavior_WithMultiplePriceTypeDefinitions(): void
{
$tariffTypeDefinition1 = new TariffTypeDefinition(new DummyTariffType());
$testBehavior = new TestBehavior('dummy');
$type1 = Type::anyId('type,dummy1');
$tariffTypeDefinition1
->withPrices()
->priceType($type1)
->withBehaviors()
->attach($testBehavior)
->end()
->end()
->end();

$tariffTypeDefinition2 = new TariffTypeDefinition(new FakeTariffType());
$fakeBehavior = new FakeBehavior('dummy');
$type2 = Type::anyId('type,dummy2');
$tariffTypeDefinition2
->withPrices()
->priceType($type2)
->withBehaviors()
->attach($fakeBehavior)
->end()
->end()
->end();

$this->registry->addTariffType($tariffTypeDefinition1);
$this->registry->addTariffType($tariffTypeDefinition2);

/** @var TestBehavior $testBehaviorActual */
$testBehaviorActual = $this->registryService->getBehavior($type1->getName(), TestBehavior::class);
$this->assertSame($testBehavior->getContext(), $testBehaviorActual->getContext());

/** @var FakeBehavior $fakeBehaviorActual */
$fakeBehaviorActual = $this->registryService->getBehavior($type2->getName(), FakeBehavior::class);
$this->assertSame($fakeBehavior->getContext(), $fakeBehaviorActual->getContext());
}

public function testGetBehaviorThrowsExceptionWhenNotFound(): void
{
$this->expectException(BehaviorNotFoundException::class);
$this->registryService->getBehavior('non-existent-type', TestBehavior::class);
}

// public function testCreateQuantityFormatterThrowsExceptionWhenNotFound(): void
// {
// $this->expectException(QuantityFormatterNotFoundException::class);
Expand Down
Loading
Loading