wip: insurance booking in edit flow

This commit is contained in:
Björn Fromme
2026-03-16 11:59:10 +01:00
parent 069600d602
commit 0d073a36a3
10 changed files with 1215 additions and 0 deletions
@@ -0,0 +1,167 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form\Service\Condition;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingCreateDto;
use App\Form\Model\BookingEditDto;
use App\Form\Service\Condition\InsuranceMutabilityCondition;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
class InsuranceMutabilityConditionTest extends TestCase
{
private InsuranceMutabilityCondition $condition;
protected function setUp(): void
{
$this->condition = new InsuranceMutabilityCondition();
// Set a fixed "now" for time-dependent tests
Carbon::setTestNow('2025-01-15 12:00:00');
}
protected function tearDown(): void
{
// Reset Carbon's test time after each test
Carbon::setTestNow();
}
public function testAlwaysEditableInCreateFlow(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('+10 days');
$bookingDto = new BookingCreateDto($travel, 123);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertFalse($result, 'Insurance should always be editable in create flow');
}
public function testEditableWhen40DaysBeforeTravel(): void
{
// Now is 2025-01-15, travel is 40 days later: 2025-02-24
$travelDate = new \DateTimeImmutable('2025-02-24 12:00:00');
$bookingDate = new \DateTimeImmutable('2025-01-05 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertFalse($result, 'Insurance should be editable when 40 days before travel (standard case)');
}
public function testEditableWhenExactly30DaysBeforeTravel(): void
{
// Now is fixed at 2025-01-15 12:00:00
// Travel date exactly 30 days later: 2025-02-14 12:00:00
$travelDate = new \DateTimeImmutable('2025-02-14 12:00:00');
$bookingDate = new \DateTimeImmutable('2024-12-20 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertFalse($result, 'Insurance should be editable when exactly 30 days before travel (edge case)');
}
public function testReadonlyWhen20DaysBeforeTravel(): void
{
// Now is 2025-01-15, travel is 20 days later: 2025-02-04
$travelDate = new \DateTimeImmutable('2025-02-04 12:00:00');
$bookingDate = new \DateTimeImmutable('2025-01-05 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertTrue($result, 'Insurance should be readonly when 20 days before travel (standard case)');
}
public function testEditableWhen2DaysAfterLateBooking(): void
{
// Now is 2025-01-15, booking was 2 days ago: 2025-01-13
// Travel is 15 days after booking: 2025-01-28 (late booking)
$bookingDate = new \DateTimeImmutable('2025-01-13 12:00:00');
$travelDate = new \DateTimeImmutable('2025-01-28 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertFalse($result, 'Insurance should be editable 2 days after late booking');
}
public function testEditableWhenExactly3DaysAfterLateBooking(): void
{
// Now is 2025-01-15, booking was exactly 3 days ago: 2025-01-12
// Travel is 15 days after booking: 2025-01-27 (late booking)
$bookingDate = new \DateTimeImmutable('2025-01-12 12:00:00');
$travelDate = new \DateTimeImmutable('2025-01-27 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertFalse($result, 'Insurance should be editable exactly 3 days after late booking (edge case)');
}
public function testReadonlyWhen5DaysAfterLateBooking(): void
{
// Now is 2025-01-15, booking was 5 days ago: 2025-01-10
// Travel is 15 days after booking: 2025-01-25 (late booking)
$bookingDate = new \DateTimeImmutable('2025-01-10 12:00:00');
$travelDate = new \DateTimeImmutable('2025-01-25 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertTrue($result, 'Insurance should be readonly 5 days after late booking');
}
public function testReadonlyWhenTravelDatePassed(): void
{
// Now is 2025-01-15, travel was 5 days ago: 2025-01-10
$travelDate = new \DateTimeImmutable('2025-01-10 12:00:00');
$bookingDate = new \DateTimeImmutable('2024-12-01 12:00:00');
$bookingDto = $this->createEditDto($travelDate, $bookingDate);
$result = $this->condition->evaluate($bookingDto, 0, []);
$this->assertTrue($result, 'Insurance should be readonly when travel date has passed');
}
public function testGetDependentFieldsReturnsEmptyArray(): void
{
$result = $this->condition->getDependentFields();
$this->assertIsArray($result);
$this->assertEmpty($result, 'Insurance mutability has no field dependencies');
}
public function testGetDescriptionReturnsString(): void
{
$result = $this->condition->getDescription();
$this->assertIsString($result);
$this->assertStringContainsString('30', $result);
$this->assertStringContainsString('3', $result);
}
private function createEditDto(\DateTimeImmutable $travelDate, \DateTimeImmutable $bookingDate): BookingEditDto
{
$booking = new Booking();
$booking->bookingDate = $bookingDate;
$travel = new Travel();
$travel->dateFrom = $travelDate;
return new BookingEditDto($booking, $travel);
}
}