fix: restore room occupancy lost to case-sensitive xml attribute lookup

This commit is contained in:
Björn Fromme
2026-08-31 14:15:21 +02:00
parent 24df764efb
commit e14d29f7d4
10 changed files with 223 additions and 16 deletions
@@ -6,6 +6,7 @@ namespace App\Tests\BusProNet\XmlParser;
use App\BusProNet\XmlCrawlerFactory;
use App\BusProNet\XmlParser\TravelParser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class TravelParserTest extends TestCase
@@ -426,4 +427,35 @@ class TravelParserTest extends TestCase
$this->assertSame('2029-12-30 18:00', $pickups[1]->time->format('Y-m-d H:i'));
$this->assertSame('2029-12-30 01:00', $pickups[2]->time->format('Y-m-d H:i'));
}
/**
* The travel export writes MinPax/MaxPax capitalized while booking responses write them lowercase.
* XmlCrawlerFactory uses addXmlContent(), which makes attribute lookups case-sensitive, so both
* spellings have to resolve.
*/
#[DataProvider('roomOccupancyAttributeProvider')]
public function testGetRoomsReadsOccupancyRegardlessOfAttributeCasing(string $attributes, ?int $expectedPax): void
{
$xml = sprintf('<?xml version="1.0" encoding="utf-8"?>
<zimmer>
<preis zimmercode="4erDW" idbuspro_zimmer="95" zimmertext="4er Zimmer Dusche/WC" %s naechte="5" idbuspro_vp="2" preis="689,00" status="Frei" verfuegbar="8" />
</zimmer>', $attributes);
$crawler = XmlCrawlerFactory::create($xml);
$rooms = $this->parser->getRooms($crawler->filterXPath('//zimmer')->first());
$this->assertArrayHasKey(95, $rooms);
$this->assertSame($expectedPax, $rooms[95]->minPax);
$this->assertSame($expectedPax, $rooms[95]->maxPax);
}
/**
* @return iterable<string, array{string, int|null}>
*/
public static function roomOccupancyAttributeProvider(): iterable
{
yield 'export casing' => ['MinPax="4" MaxPax="4"', 4];
yield 'lowercase casing' => ['minpax="4" maxpax="4"', 4];
yield 'missing stays null' => ['', null];
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Tests\Form;
use App\Form\Model\RoomSelectionDto;
use App\Form\RoomSelectType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Forms;
class RoomSelectTypeTest extends TestCase
{
/**
* @param array<string, int> $expectedChoices
*/
#[DataProvider('quantityChoiceProvider')]
public function testQuantityChoicesFollowAvailability(int $maxQuantity, array $expectedChoices): void
{
$data = new RoomSelectionDto();
$data->id = 95;
$data->maxQuantity = $maxQuantity;
$form = Forms::createFormFactory()->create(RoomSelectType::class, $data);
$this->assertSame($expectedChoices, $form->get('quantity')->getConfig()->getOption('choices'));
}
/**
* @return iterable<string, array{int, array<string, int>}>
*/
public static function quantityChoiceProvider(): iterable
{
yield 'available rooms are selectable' => [2, ['-' => 0, '1' => 1, '2' => 2]];
// range(1, 0) would return [1, 0], offering a bookable "1" for a room with no availability.
yield 'no availability offers only the placeholder' => [0, ['-' => 0]];
}
}
+18 -1
View File
@@ -70,6 +70,23 @@ class BookingSummaryAssemblerTest extends TestCase
$this->assertSame(2, $summary->participantCount);
}
public function testCreateStep1CountsRoomsWithUnknownOccupancyAsZero(): void
{
$service = $this->createService();
$bookingDto = $this->createBookingDto();
$bookingDto->travel->rooms[12] = $this->createRoom(12, null);
$bookingDto->currentStep = 1;
$bookingDto->roomSelections = [
$this->createRoomSelection(10, 1),
$this->createRoomSelection(12, 2),
];
$summary = $service->getSummaryData($bookingDto);
// 1×2 for the known room; the room without occupancy contributes nothing.
$this->assertSame(2, $summary->participantCount);
}
private function createService(): BookingSummaryAssembler
{
$priceCalculator = $this->createStub(BookingPriceCalculator::class);
@@ -107,7 +124,7 @@ class BookingSummaryAssemblerTest extends TestCase
return new BookingDto($travel, 157047);
}
private function createRoom(int $id, int $maxPax): Room
private function createRoom(int $id, ?int $maxPax): Room
{
$room = new Room();
$room->id = $id;
+26
View File
@@ -272,6 +272,32 @@ class RoomAssignerTest extends TestCase
self::assertNull($bookingDto->participants[1]->assignedRoomId);
}
#[Test]
public function assignParticipantsToRoomsFallsBackToOneBedWhenOccupancyIsUnknown(): void
{
$bookingDto = $this->createBookingDtoWithRoomSelections([
['roomId' => 100, 'quantity' => 2, 'capacity' => 1],
]);
$bookingDto->participants = [
new ParticipantDto(),
new ParticipantDto(),
];
$room = new Room();
$room->id = 100;
$room->minPax = null;
$room->available = 10;
$room->status = Constants::STATUS_AVAILABLE;
$bookingDto->travel->rooms = [$room];
$this->service->assignParticipantsToRooms($bookingDto);
self::assertSame(100, $bookingDto->participants[0]->assignedRoomId);
self::assertSame(100, $bookingDto->participants[1]->assignedRoomId);
}
private function createBookingDtoWithRoomSelections(array $selections): BookingDto
{
$travel = new Travel();
@@ -247,6 +247,31 @@ class RoomPricingCalculatorTest extends TestCase
$this->assertEquals(458.0, $result[0]['totalPrice']);
}
public function testCalculateRoomPricingFallsBackToOneParticipantWhenOccupancyIsUnknown(): void
{
$room = new Room();
$room->id = 4;
$room->price = 419.0;
$room->minPax = null;
$room->label = 'Bett im Mehrbettzimmer';
$travel = new Travel();
$travel->rooms = [$room];
$roomSelection = new RoomSelectionDto();
$roomSelection->id = 4;
$roomSelection->quantity = 2;
$bookingDto = new BookingDto($travel, 1);
$bookingDto->roomSelections = [$roomSelection];
$result = $this->calculator->calculateRoomPricing($bookingDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
$this->assertCount(1, $result);
$this->assertEquals(2, $result[0]['participantCount']);
$this->assertEquals(838.0, $result[0]['totalPrice']);
}
public function testCalculateRoomPricingInEditModeUsesStoredIndividualPrices(): void
{
$travel = new Travel();