wip: booking process, refactoring

This commit is contained in:
Björn Fromme
2025-07-16 19:37:17 +02:00
parent 47faa6b08e
commit eecea0abd1
43 changed files with 2269 additions and 366 deletions
@@ -4,22 +4,45 @@ namespace App\Tests\BusProNet\DataLoader;
use App\BusProNet\Model\Hotel;
use App\BusProNet\XmlLoader\HotelLoader;
use League\Flysystem\FilesystemOperator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
class HotelDataLoaderTest extends TestCase
{
public function testParse(): void
public function testLoadAll(): void
{
$loader = new HotelLoader(__DIR__.'/../../Resources');
$hotelId = 163113;
$xmlContent = '<?xml version="1.0" encoding="utf-8"?>
<!--Export für dreipunktnull büro für mediengestaltung-->
<hotels erstellt_am="14.07.2025 15:04:46">
<hotel id="1" idbuspro="163113" code="SBWOXA">
<name>L\'Oxalys</name>
<ort>Val Thorens</ort>
<land>F</land>
<strasse>Rue des Lacs</strasse>
<telefon></telefon>
<art>Hotel</art>
<internetbuchbar>True</internetbuchbar>
</hotel>
</hotels>';
$xml = $loader->loadById($hotelId, 'hotels_data.xml');
$this->assertInstanceOf(\SimpleXMLElement::class, $xml);
$filesystem = $this->createMock(FilesystemOperator::class);
$filesystem->expects($this->once())
->method('read')
->with('hotel.xml')
->willReturn($xmlContent);
$hotel = $loader->parseXml($xml);
$cache = new ArrayAdapter();
$loader = new HotelLoader($cache, $filesystem);
$hotels = $loader->loadAll();
$this->assertIsArray($hotels);
$this->assertArrayHasKey(163113, $hotels);
$hotel = $hotels[163113];
$this->assertInstanceOf(Hotel::class, $hotel);
$this->assertEquals($hotelId, $hotel->id);
$this->assertEquals(163113, $hotel->id);
$this->assertEquals('L\'Oxalys', $hotel->name);
$this->assertEquals('SBWOXA', $hotel->code);
$this->assertEquals('Val Thorens', $hotel->city);
@@ -4,22 +4,41 @@ namespace App\Tests\BusProNet\DataLoader;
use App\BusProNet\Model\Pickup;
use App\BusProNet\XmlLoader\PickupLoader;
use League\Flysystem\FilesystemOperator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
class PickupDataLoaderTest extends TestCase
{
public function testParse(): void
public function testLoadById(): void
{
$loader = new PickupLoader();
$pickupId = 1;
$xmlContent = '<?xml version="1.0" encoding="utf-8"?>
<!--Export für dreipunktnull büro für mediengestaltung-->
<zustiege erstellt_am="14.07.2025 15:15:09">
<zustieg id="1" idbuspro="1" code="MS-Hbf">
<ort>Münster</ort>
<plz>48143</plz>
<strasse>Hafenstr/Ecke Friedrich-Ebert-Str</strasse>
<art>BUS</art>
<hausabholung>False</hausabholung>
<crsbuchbar>True</crsbuchbar>
<internetbuchbar>True</internetbuchbar>
</zustieg>
</zustiege>';
$xml = $loader->loadById($pickupId, 'pickups_data.xml');
$this->assertInstanceOf(\SimpleXMLElement::class, $xml);
$filesystem = $this->createMock(FilesystemOperator::class);
$filesystem->expects($this->once())
->method('read')
->with('zustiege.xml')
->willReturn($xmlContent);
$pickup = $loader->parseXml($xml);
$cache = new ArrayAdapter();
$loader = new PickupLoader($cache, $filesystem);
$pickup = $loader->loadById(1);
$this->assertInstanceOf(Pickup::class, $pickup);
$this->assertEquals($pickupId, $pickup->id);
$this->assertEquals(1, $pickup->id);
$this->assertEquals('MS-Hbf', $pickup->code);
$this->assertEquals('Münster', $pickup->city);
$this->assertEquals('48143', $pickup->postalCode);
@@ -3,21 +3,56 @@
namespace App\Tests\BusProNet\DataLoader;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\HotelLoader;
use App\BusProNet\XmlLoader\TravelLoader;
use App\BusProNet\XmlParser\TravelParser;
use League\Flysystem\FilesystemOperator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
class TravelDataLoaderTest extends TestCase
{
public function testParse(): void
public function testLoadById(): void
{
$loader = new TravelLoader('');
$filename = __DIR__.'/../../Resources/travel_data.xml';
$travelId = 11478;
$xmlContent = '<?xml version="1.0" encoding="utf-8"?>
<!--Export für dreipunktnull büro für mediengestaltung-->
<reisen>
<reise id="1" idbuspro="1234" code="DPWMP" erstellt_am="14.07.2025 15:02:01">
<termin id="1" idbuspro="11478" idprodukt="1234" termin="06.01.2025" bis="25.01.2025" code="DPWMP060125" reiseart="F">
<text>Davos - Sportclub Waldschlössli</text>
<abpreis>534.2</abpreis>
<hotel idbuspro="123">
<text>Test Hotel</text>
</hotel>
</termin>
</reise>
</reisen>';
$xml = $loader->loadById($travelId, $filename);
$this->assertInstanceOf(\SimpleXMLElement::class, $xml);
$filesystem = $this->createMock(FilesystemOperator::class);
$filesystem->expects($this->once())
->method('read')
->with('test_file.xml')
->willReturn($xmlContent);
$travel = $loader->parseXml($xml);
$hotelLoader = $this->createMock(HotelLoader::class);
$travelParser = $this->createMock(TravelParser::class);
$expectedTravel = new Travel();
$expectedTravel->code = 'DPWMP060125';
$expectedTravel->type = 'F';
$expectedTravel->dateFrom = new \DateTimeImmutable('2025-01-06');
$expectedTravel->dateTo = new \DateTimeImmutable('2025-01-25');
$expectedTravel->label = 'Davos - Sportclub Waldschlössli';
$expectedTravel->priceFrom = 534.2;
$travelParser->expects($this->once())
->method('parse')
->willReturn($expectedTravel);
$cache = new ArrayAdapter();
$loader = new TravelLoader($hotelLoader, $travelParser, 'http://example.com', $cache, $filesystem);
$travel = $loader->loadById(11478, null, 'test_file.xml');
$this->assertInstanceOf(Travel::class, $travel);
$this->assertEquals('DPWMP060125', $travel->code);
@@ -26,11 +61,5 @@ class TravelDataLoaderTest extends TestCase
$this->assertInstanceOf(\DateTimeImmutable::class, $travel->dateTo);
$this->assertEquals('Davos - Sportclub Waldschlössli', $travel->label);
$this->assertEquals(534.2, $travel->priceFrom);
$this->assertCount(8, $travel->selectionGroups);
$this->assertCount(27, $travel->additionalServices);
$this->assertCount(5, $travel->transportationServices);
$this->assertCount(7, $travel->pickupsTo);
$this->assertCount(12, $travel->rooms);
}
}
@@ -0,0 +1,507 @@
<?php
declare(strict_types=1);
namespace App\Tests\BusProNet\DataProcessor;
use App\BusProNet\DataProcessor\BookingDataProcessor;
use App\BusProNet\Model\Address;
use App\BusProNet\Model\BankAccount;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\Communication;
use App\BusProNet\Model\PersonalData;
use App\BusProNet\Model\Pickup;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingEditDto;
use App\Form\Model\ParticipantDto;
use PHPUnit\Framework\TestCase;
/**
* Comprehensive test suite for BookingDataProcessor.
*
* Tests all aspects of booking data processing including service mappings,
* participant data updates, and API payload generation.
*/
class BookingDataProcessorTest extends TestCase
{
private BookingDataProcessor $processor;
protected function setUp(): void
{
$this->processor = new BookingDataProcessor();
}
public function testCreateUpdateRequestPayloadWithCompleteData(): void
{
$formData = $this->createCompleteFormData();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertIsArray($result);
$this->assertArrayHasKey('idbuchung', $result);
$this->assertArrayHasKey('teilnehmerliste', $result);
$this->assertArrayHasKey('zusatzleistungen', $result);
$this->assertArrayHasKey('beförderungen', $result);
$this->assertArrayHasKey('ferienzielunterbringungen', $result);
$this->assertEquals(123, $result['idbuchung']);
$this->assertEquals('ACTIVE', $result['status']);
$this->assertCount(2, $result['teilnehmerliste']['teilnehmer']);
}
public function testCanceledParticipantsAreSkipped(): void
{
$formData = $this->createFormDataWithCanceledParticipant();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertCount(2, $result['teilnehmerliste']['teilnehmer']);
$this->assertEquals(0, $result['teilnehmerliste']['teilnehmer'][0]['@id']);
$this->assertEquals(1, $result['teilnehmerliste']['teilnehmer'][1]['@id']);
}
public function testAdditionalServicesProcessing(): void
{
$formData = $this->createFormDataWithAdditionalServices();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertNotEmpty($result['zusatzleistungen']['zusatzleistung']);
$this->assertCount(2, $result['zusatzleistungen']['zusatzleistung']);
$service = $result['zusatzleistungen']['zusatzleistung'][0];
$this->assertEquals(1, $service['@idleistung']);
$this->assertEquals(1, $service['@anzahl']);
$this->assertEquals('0', $service['@zuordnung']);
}
public function testTransportationServicesProcessing(): void
{
$formData = $this->createFormDataWithTransportation();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertNotEmpty($result['beförderungen']['beförderung']);
$this->assertCount(2, $result['beförderungen']['beförderung']);
}
public function testBusPickupLocationsProcessing(): void
{
$formData = $this->createFormDataWithBusPickup();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertArrayHasKey('zustiege', $result);
$this->assertNotEmpty($result['zustiege']['zustieg']);
$this->assertEquals(1, $result['zustiege']['zustieg'][0]['@idzustieg']);
}
public function testNonBusTransportationSkipsPickup(): void
{
$formData = $this->createFormDataWithNonBusTransportation();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertArrayNotHasKey('zustiege', $result);
}
public function testUnusedServicesAreRemoved(): void
{
$formData = $this->createFormDataWithUnusedServices();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertEmpty($result['zusatzleistungen']['zusatzleistung']);
$this->assertEmpty($result['beförderungen']['beförderung']);
}
public function testParticipantPersonalDataUpdate(): void
{
$formData = $this->createFormDataWithUpdatedPersonalData();
$this->processor->createUpdateRequestPayload($formData);
$participant = $formData->booking->participants[0];
$this->assertEquals('Updated', $participant->firstName);
$this->assertEquals('Participant', $participant->name);
$this->assertEquals('[email protected]', $participant->communication->email);
$this->assertEquals('+49123456789', $participant->communication->mobile);
}
public function testInactiveParticipantsPersonalDataNotUpdated(): void
{
$formData = $this->createFormDataWithInactiveParticipant();
$this->processor->createUpdateRequestPayload($formData);
$participant = $formData->booking->participants[0];
$this->assertEquals('Original', $participant->firstName);
$this->assertEquals('Name', $participant->name);
}
public function testApplicantDataSyncWithFirstParticipant(): void
{
$formData = $this->createFormDataForApplicantSync();
$this->processor->createUpdateRequestPayload($formData);
$applicant = $formData->booking->applicant;
$firstParticipant = $formData->booking->participants[0];
$this->assertEquals($firstParticipant->height, $applicant->height);
$this->assertEquals($firstParticipant->weight, $applicant->weight);
$this->assertEquals($firstParticipant->shoeSize, $applicant->shoeSize);
}
public function testBankAccountIncludedInPayload(): void
{
$formData = $this->createFormDataWithBankAccount();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertArrayHasKey('bankverbindung', $result['zahlung']);
$this->assertEquals('Test Bank', $result['zahlung']['bankverbindung']['@kreditinstitut']);
$this->assertEquals('DE89370400440532013000', $result['zahlung']['bankverbindung']['@iban']);
}
public function testBankAccountNotIncludedWhenNull(): void
{
$formData = $this->createFormDataWithoutBankAccount();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertArrayNotHasKey('bankverbindung', $result['zahlung']);
}
public function testAccommodationRoomsProcessing(): void
{
$formData = $this->createFormDataWithRooms();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertNotEmpty($result['ferienzielunterbringungen']['ferienzielunterbringung']);
$room = $result['ferienzielunterbringungen']['ferienzielunterbringung'][0];
$this->assertEquals(1, $room['@idzimmer']);
$this->assertEquals('DOUBLE', $room['@kategorie']);
$this->assertEquals('01.01.2024', $room['@anreise']);
$this->assertEquals('07.01.2024', $room['@abreise']);
}
public function testCommunicationObjectCreation(): void
{
$formData = $this->createFormDataWithoutExistingCommunication();
$this->processor->createUpdateRequestPayload($formData);
$participant = $formData->booking->participants[0];
$this->assertInstanceOf(Communication::class, $participant->communication);
$this->assertEquals('[email protected]', $participant->communication->email);
}
public function testEmptyPickupsToDoesNotCreateZustiegeSection(): void
{
$formData = $this->createFormDataWithoutPickups();
$result = $this->processor->createUpdateRequestPayload($formData);
$this->assertArrayNotHasKey('zustiege', $result);
}
private function createCompleteFormData(): BookingEditDto
{
$formData = new BookingEditDto();
$formData->booking = $this->createMockBooking();
$formData->travel = $this->createMockTravel();
$formData->participants = [
$this->createMockParticipantDto(0, 'F'),
$this->createMockParticipantDto(1, 'F'),
];
return $formData;
}
private function createFormDataWithCanceledParticipant(): BookingEditDto
{
$formData = new BookingEditDto();
$formData->booking = $this->createMockBooking();
$formData->travel = $this->createMockTravel();
$formData->participants = [
$this->createMockParticipantDto(0, 'F'),
$this->createMockParticipantDto(1, 'S'), // Canceled
];
return $formData;
}
private function createFormDataWithAdditionalServices(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$participant->courses = [$this->createMockService(1)];
$participant->additionalServices = [$this->createMockService(2)];
return $formData;
}
private function createFormDataWithTransportation(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$participant->transportationServiceTo = $this->createMockService(1);
$participant->transportationServiceFro = $this->createMockService(2);
return $formData;
}
private function createFormDataWithBusPickup(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$busService = $this->createMockService(1);
$busService->subType = 'BUS';
$participant->transportationServiceTo = $busService;
$participant->pickup = $this->createMockPickup(1);
return $formData;
}
private function createFormDataWithNonBusTransportation(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$trainService = $this->createMockService(1);
$trainService->subType = 'TRAIN';
$participant->transportationServiceTo = $trainService;
return $formData;
}
private function createFormDataWithUnusedServices(): BookingEditDto
{
$formData = $this->createCompleteFormData();
// Remove all participants so services become unused
$formData->participants = [];
return $formData;
}
private function createFormDataWithUpdatedPersonalData(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$participant->firstName = 'Updated';
$participant->lastName = 'Participant';
$participant->email = '[email protected]';
$participant->mobile = '+49123456789';
return $formData;
}
private function createFormDataWithInactiveParticipant(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$participant->status = 'C'; // Inactive status
$participant->firstName = 'Updated';
$participant->lastName = 'Participant';
// Ensure original data remains unchanged
$formData->booking->participants[0]->firstName = 'Original';
$formData->booking->participants[0]->name = 'Name';
return $formData;
}
private function createFormDataForApplicantSync(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$firstParticipant = $formData->booking->participants[0];
$firstParticipant->height = '180';
$firstParticipant->weight = '75';
$firstParticipant->shoeSize = '42';
return $formData;
}
private function createFormDataWithBankAccount(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$bankAccount = new BankAccount();
$bankAccount->bankName = 'Test Bank';
$bankAccount->iban = 'DE89370400440532013000';
$bankAccount->bic = 'COBADEFFXXX';
$bankAccount->holder = 'Test Holder';
$formData->booking->bankAccount = $bankAccount;
return $formData;
}
private function createFormDataWithoutBankAccount(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$formData->booking->bankAccount = null;
return $formData;
}
private function createFormDataWithRooms(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$room = new Room();
$room->id = 1;
$room->category = 'DOUBLE';
$room->boardId = 1;
$room->dateFrom = new \DateTimeImmutable('2024-01-01');
$room->dateTo = new \DateTimeImmutable('2024-01-07');
$room->totalCount = 2;
$room->mapping = [0, 1];
$formData->booking->rooms = [$room];
return $formData;
}
private function createFormDataWithoutExistingCommunication(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$participant = $formData->participants[0];
$participant->email = '[email protected]';
$participant->mobile = '+49987654321';
$formData->booking->participants[0]->communication = new Communication();
return $formData;
}
private function createFormDataWithoutPickups(): BookingEditDto
{
$formData = $this->createCompleteFormData();
$formData->booking->pickupsTo = [];
return $formData;
}
private function createMockBooking(): Booking
{
$booking = new Booking();
$booking->id = 123;
$booking->status = 'ACTIVE';
$booking->agencyId = 1;
$booking->dateId = 1;
$booking->hotelId = 1;
$booking->paymentId = '1';
$booking->paymentLabel = 'Credit Card';
$booking->paymentType = 'CC';
$booking->additionalServices = [];
$booking->transportationServices = [];
$booking->pickupsTo = [];
$booking->pickupsFro = [];
$booking->participants = [
$this->createMockPersonalData('Participant0'),
$this->createMockPersonalData('Participant1'),
];
$booking->participantsStatus = ['F', 'F'];
$booking->applicant = $this->createMockPersonalData('Applicant');
$booking->bankAccount = null;
$booking->rooms = [];
return $booking;
}
private function createMockTravel(): Travel
{
$travel = new Travel();
$travel->additionalServices = [
1 => $this->createMockService(1),
2 => $this->createMockService(2),
];
$travel->transportationServices = [
1 => $this->createMockService(1),
2 => $this->createMockService(2),
];
return $travel;
}
private function createMockParticipantDto(int $index, string $status): ParticipantDto
{
$participant = new ParticipantDto();
$participant->index = $index;
$participant->status = $status;
$participant->firstName = "Participant{$index}";
$participant->lastName = 'LastName';
$participant->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$participant->gender = 'M';
$participant->nationality = 'DE';
$participant->height = '175';
$participant->weight = '70';
$participant->shoeSize = '40';
$participant->email = null;
$participant->mobile = null;
$participant->courses = [];
$participant->additionalServices = [];
$participant->skiPass = [];
$participant->board = [];
$participant->rentals = [];
$participant->transportationServiceTo = $this->createMockService(1);
$participant->transportationServiceFro = $this->createMockService(2);
$participant->pickup = null;
return $participant;
}
private function createMockPersonalData(string $firstName): PersonalData
{
$personalData = new PersonalData();
$personalData->firstName = $firstName;
$personalData->name = 'LastName';
$personalData->dateOfBirth = new \DateTimeImmutable('1990-01-01');
$personalData->gender = 'M';
$personalData->nationality = 'DE';
$personalData->height = '175';
$personalData->weight = '70';
$personalData->shoeSize = '40';
$personalData->address = new Address();
$personalData->communication = new Communication();
return $personalData;
}
private function createMockService(int $id): Service
{
$service = new Service();
$service->id = $id;
$service->price = 50.0;
$service->mapping = [];
$service->individualPrice = [];
$service->subType = 'STANDARD';
return $service;
}
private function createMockPickup(int $id): Pickup
{
$pickup = new Pickup();
$pickup->id = $id;
$pickup->mapping = [];
return $pickup;
}
}
@@ -2,14 +2,14 @@
namespace BusProNet\Utility;
use App\BusProNet\Utility\TravelCodeUtility;
use App\BusProNet\Utility\DateCodeUtility;
use PHPUnit\Framework\TestCase;
class TravelCodeUtilityTest extends TestCase
{
public function testSanitizeCode(): void
{
$utility = new TravelCodeUtility();
$utility = new DateCodeUtility();
$travelCode = 'ABCDEF/010125';
$sanitized = $utility->sanitize($travelCode);
@@ -26,7 +26,7 @@ class TravelCodeUtilityTest extends TestCase
public function testGetBaseCode(): void
{
$utility = new TravelCodeUtility();
$utility = new DateCodeUtility();
$travelCode = 'ABCDEF010125';
$baseCode = $utility->getBaseCode($travelCode);
@@ -39,7 +39,7 @@ class TravelCodeUtilityTest extends TestCase
public function testGetDate(): void
{
$utility = new TravelCodeUtility();
$utility = new DateCodeUtility();
$travelCode = 'ABCDEF010125';
$date = $utility->getDate($travelCode);