Files
myep/tests/BusProNet/Service/ParticipantStatusRuleRegistryTest.php
T
Björn Fromme c1425f0562 feat: extensible participant status determination for booking creation
Adds a rule-based system to determine participant status in CREATE
payloads. Participants selecting a 'Begleitperson' service now receive
status 'O' (Option), all others default to 'F' (Final).

- Add ParticipantStatusRuleInterface for defining status rules
- Add ParticipantStatusRuleRegistry for priority-based rule evaluation
- Add ChaperonServiceStatusRule for Begleitperson detection
- Integrate status evaluation into BookingPayloadBuilder
2026-03-16 12:02:59 +01:00

130 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\BusProNet\Service;
use App\BusProNet\Service\Contract\ParticipantStatusRuleInterface;
use App\BusProNet\Service\ParticipantStatusRuleRegistry;
use App\Form\Model\ParticipantDto;
use PHPUnit\Framework\TestCase;
class ParticipantStatusRuleRegistryTest extends TestCase
{
public function testReturnsDefaultStatusWhenNoRulesMatch(): void
{
$rule = $this->createMock(ParticipantStatusRuleInterface::class);
$rule->method('evaluate')->willReturn(false);
$rule->method('getPriority')->willReturn(100);
$registry = new ParticipantStatusRuleRegistry([$rule]);
$participant = new ParticipantDto();
$this->assertSame('F', $registry->evaluateStatus($participant));
}
public function testReturnsMatchingRuleStatus(): void
{
$rule = $this->createMock(ParticipantStatusRuleInterface::class);
$rule->method('evaluate')->willReturn(true);
$rule->method('getStatus')->willReturn('O');
$rule->method('getPriority')->willReturn(100);
$registry = new ParticipantStatusRuleRegistry([$rule]);
$participant = new ParticipantDto();
$this->assertSame('O', $registry->evaluateStatus($participant));
}
public function testHigherPriorityRuleTakesPrecedence(): void
{
$lowPriorityRule = $this->createMock(ParticipantStatusRuleInterface::class);
$lowPriorityRule->method('evaluate')->willReturn(true);
$lowPriorityRule->method('getStatus')->willReturn('L');
$lowPriorityRule->method('getPriority')->willReturn(50);
$highPriorityRule = $this->createMock(ParticipantStatusRuleInterface::class);
$highPriorityRule->method('evaluate')->willReturn(true);
$highPriorityRule->method('getStatus')->willReturn('H');
$highPriorityRule->method('getPriority')->willReturn(100);
// Register low priority first, high priority second
$registry = new ParticipantStatusRuleRegistry([$lowPriorityRule, $highPriorityRule]);
$participant = new ParticipantDto();
// High priority rule should be evaluated first and match
$this->assertSame('H', $registry->evaluateStatus($participant));
}
public function testEmptyRulesArrayReturnsDefaultStatus(): void
{
$registry = new ParticipantStatusRuleRegistry([]);
$participant = new ParticipantDto();
$this->assertSame('F', $registry->evaluateStatus($participant));
}
public function testFirstMatchingRuleWins(): void
{
$firstMatchingRule = $this->createMock(ParticipantStatusRuleInterface::class);
$firstMatchingRule->method('evaluate')->willReturn(true);
$firstMatchingRule->method('getStatus')->willReturn('A');
$firstMatchingRule->method('getPriority')->willReturn(100);
$secondMatchingRule = $this->createMock(ParticipantStatusRuleInterface::class);
$secondMatchingRule->method('evaluate')->willReturn(true);
$secondMatchingRule->method('getStatus')->willReturn('B');
$secondMatchingRule->method('getPriority')->willReturn(100);
$registry = new ParticipantStatusRuleRegistry([$firstMatchingRule, $secondMatchingRule]);
$participant = new ParticipantDto();
// First rule with same priority registered first should win
$this->assertSame('A', $registry->evaluateStatus($participant));
}
public function testNonMatchingHighPriorityRuleDoesNotBlockLowerPriority(): void
{
$highPriorityRule = $this->createMock(ParticipantStatusRuleInterface::class);
$highPriorityRule->method('evaluate')->willReturn(false);
$highPriorityRule->method('getStatus')->willReturn('H');
$highPriorityRule->method('getPriority')->willReturn(100);
$lowPriorityRule = $this->createMock(ParticipantStatusRuleInterface::class);
$lowPriorityRule->method('evaluate')->willReturn(true);
$lowPriorityRule->method('getStatus')->willReturn('L');
$lowPriorityRule->method('getPriority')->willReturn(50);
$registry = new ParticipantStatusRuleRegistry([$highPriorityRule, $lowPriorityRule]);
$participant = new ParticipantDto();
// High priority doesn't match, so low priority rule should be used
$this->assertSame('L', $registry->evaluateStatus($participant));
}
public function testMultipleRulesWithVariousPrioritiesAreSortedCorrectly(): void
{
$lowRule = $this->createMock(ParticipantStatusRuleInterface::class);
$lowRule->method('evaluate')->willReturn(true);
$lowRule->method('getStatus')->willReturn('LOW');
$lowRule->method('getPriority')->willReturn(10);
$mediumRule = $this->createMock(ParticipantStatusRuleInterface::class);
$mediumRule->method('evaluate')->willReturn(true);
$mediumRule->method('getStatus')->willReturn('MED');
$mediumRule->method('getPriority')->willReturn(50);
$highRule = $this->createMock(ParticipantStatusRuleInterface::class);
$highRule->method('evaluate')->willReturn(true);
$highRule->method('getStatus')->willReturn('HIGH');
$highRule->method('getPriority')->willReturn(100);
// Register in random order
$registry = new ParticipantStatusRuleRegistry([$mediumRule, $lowRule, $highRule]);
$participant = new ParticipantDto();
// Highest priority rule should match first
$this->assertSame('HIGH', $registry->evaluateStatus($participant));
}
}