wip: insurance booking in edit flow
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Tests\BusProNet\Model;
|
||||
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\Insurance;
|
||||
use App\BusProNet\Model\Service;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -69,4 +70,69 @@ class BookingTest extends TestCase
|
||||
$result = $booking->getSkiPassForParticipant(0);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testGetInsuranceForParticipantReturnsCorrectInsurance(): void
|
||||
{
|
||||
$booking = new Booking();
|
||||
|
||||
// Create insurance for participants 0 and 2
|
||||
$insurance1 = new Insurance();
|
||||
$insurance1->id = 100;
|
||||
$insurance1->label = 'Reiseschutz Platin';
|
||||
$insurance1->mapping = [0, 2];
|
||||
|
||||
// Create insurance for participant 1
|
||||
$insurance2 = new Insurance();
|
||||
$insurance2->id = 200;
|
||||
$insurance2->label = 'Reiseschutz Gold';
|
||||
$insurance2->mapping = [1];
|
||||
|
||||
$booking->insurances = [
|
||||
100 => $insurance1,
|
||||
200 => $insurance2,
|
||||
];
|
||||
|
||||
// Test: participant 0 should get insurance1
|
||||
$result = $booking->getInsuranceForParticipant(0);
|
||||
$this->assertSame($insurance1, $result);
|
||||
$this->assertEquals(100, $result->id);
|
||||
|
||||
// Test: participant 1 should get insurance2
|
||||
$result = $booking->getInsuranceForParticipant(1);
|
||||
$this->assertSame($insurance2, $result);
|
||||
$this->assertEquals(200, $result->id);
|
||||
|
||||
// Test: participant 2 should get insurance1
|
||||
$result = $booking->getInsuranceForParticipant(2);
|
||||
$this->assertSame($insurance1, $result);
|
||||
|
||||
// Test: participant 3 should get null (no insurance assigned)
|
||||
$result = $booking->getInsuranceForParticipant(3);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testGetInsuranceForParticipantReturnsNullWhenNoInsurances(): void
|
||||
{
|
||||
$booking = new Booking();
|
||||
$booking->insurances = [];
|
||||
|
||||
$result = $booking->getInsuranceForParticipant(0);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testGetInsuranceForParticipantHandlesEmptyMapping(): void
|
||||
{
|
||||
$booking = new Booking();
|
||||
|
||||
// Create insurance with empty mapping
|
||||
$insurance = new Insurance();
|
||||
$insurance->id = 100;
|
||||
$insurance->label = 'Reiseschutz';
|
||||
$insurance->mapping = [];
|
||||
|
||||
$booking->insurances = [100 => $insurance];
|
||||
|
||||
$result = $booking->getInsuranceForParticipant(0);
|
||||
$this->assertNull($result, 'Should return null when mapping is empty');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\BusProNet\XmlParser;
|
||||
|
||||
use App\BusProNet\XmlParser\BookingInsurancesParser;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class BookingInsurancesParserTest extends TestCase
|
||||
{
|
||||
public function testParseInsurancesFromBookingXml(): void
|
||||
{
|
||||
$xml = <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ergebnis>
|
||||
<versicherungen>
|
||||
<versicherung idversicherung="177240" bezeichnung="Reiseschutz Platin Auto/Bahn/Bus (Europa)" anzahl="1"
|
||||
zuordnung="1" gesamtpreis="45,00" einzelpreis="45,00"></versicherung>
|
||||
</versicherungen>
|
||||
</ergebnis>
|
||||
XML;
|
||||
|
||||
$crawler = new Crawler($xml);
|
||||
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
|
||||
|
||||
$parser = new BookingInsurancesParser();
|
||||
$insurances = $parser->parse($insurancesData);
|
||||
|
||||
$this->assertCount(1, $insurances);
|
||||
$this->assertArrayHasKey(177240, $insurances);
|
||||
|
||||
$insurance = $insurances[177240];
|
||||
$this->assertEquals(177240, $insurance->id);
|
||||
$this->assertEquals('Reiseschutz Platin Auto/Bahn/Bus (Europa)', $insurance->label);
|
||||
$this->assertEquals(45.0, $insurance->price);
|
||||
$this->assertEquals([0], $insurance->mapping); // zuordnung="1" maps to participant index 0
|
||||
$this->assertEquals([0 => 45.0], $insurance->individualPrice);
|
||||
}
|
||||
|
||||
public function testParseMultipleInsurances(): void
|
||||
{
|
||||
$xml = <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ergebnis>
|
||||
<versicherungen>
|
||||
<versicherung idversicherung="100" bezeichnung="Insurance A" anzahl="1"
|
||||
zuordnung="1" gesamtpreis="45,00" einzelpreis="45,00"></versicherung>
|
||||
<versicherung idversicherung="200" bezeichnung="Insurance B" anzahl="1"
|
||||
zuordnung="2" gesamtpreis="30,00" einzelpreis="30,00"></versicherung>
|
||||
</versicherungen>
|
||||
</ergebnis>
|
||||
XML;
|
||||
|
||||
$crawler = new Crawler($xml);
|
||||
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
|
||||
|
||||
$parser = new BookingInsurancesParser();
|
||||
$insurances = $parser->parse($insurancesData);
|
||||
|
||||
$this->assertCount(2, $insurances);
|
||||
|
||||
// First insurance
|
||||
$insurance1 = $insurances[100];
|
||||
$this->assertEquals(100, $insurance1->id);
|
||||
$this->assertEquals('Insurance A', $insurance1->label);
|
||||
$this->assertEquals(45.0, $insurance1->price);
|
||||
$this->assertEquals([0], $insurance1->mapping); // zuordnung="1" maps to index 0
|
||||
$this->assertEquals([0 => 45.0], $insurance1->individualPrice);
|
||||
|
||||
// Second insurance
|
||||
$insurance2 = $insurances[200];
|
||||
$this->assertEquals(200, $insurance2->id);
|
||||
$this->assertEquals('Insurance B', $insurance2->label);
|
||||
$this->assertEquals(30.0, $insurance2->price);
|
||||
$this->assertEquals([1], $insurance2->mapping); // zuordnung="2" maps to index 1
|
||||
$this->assertEquals([1 => 30.0], $insurance2->individualPrice);
|
||||
}
|
||||
|
||||
public function testParseEmptyInsurancesReturnsEmptyArray(): void
|
||||
{
|
||||
$xml = <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ergebnis>
|
||||
<versicherungen>
|
||||
</versicherungen>
|
||||
</ergebnis>
|
||||
XML;
|
||||
|
||||
$crawler = new Crawler($xml);
|
||||
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
|
||||
|
||||
$parser = new BookingInsurancesParser();
|
||||
$insurances = $parser->parse($insurancesData);
|
||||
|
||||
$this->assertCount(0, $insurances);
|
||||
$this->assertIsArray($insurances);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user