feat: cap rooms sharing a contingent per booking

This commit is contained in:
fromme committed 2026-11-11 16:35:49 +01:00
1 parent 5942ecf340
commit 3d94505e8d
18 files changed
+671 -19

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Tests\BpnConnect;
use App\BpnConnect\Exception\BpnConnectException;
use App\BpnConnect\RoomsClient;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
class RoomsClientTest extends TestCase
{
public function testGetContingentPoolsQueriesTheHotelAndMapsEachRoomTypeToItsPool(): void
{
$requests = [];
$http = new MockHttpClient(function (string $method, string $url) use (&$requests): MockResponse {
$requests[] = [$method, $url];
return $this->jsonResponse([
'meta' => ['hotel_id' => 163113, 'count' => 3, 'execution_time_ms' => 3],
'data' => [
['room_type_id' => 27, 'pool_room_type_id' => 27],
['room_type_id' => 2366, 'pool_room_type_id' => 2366],
['room_type_id' => 2367, 'pool_room_type_id' => 2366],
],
]);
});
$pools = $this->client($http)->getContingentPools(163113);
self::assertSame([['GET', 'https://bpn-connect.test/api/v1/rooms/contingent-pools?hotelId=163113']], $requests);
self::assertSame([27 => 27, 2366 => 2366, 2367 => 2366], $pools);
}
public function testAnUnknownHotelYieldsNoPools(): void
{
$http = new MockHttpClient($this->jsonResponse(['meta' => ['hotel_id' => 1, 'count' => 0], 'data' => []]));
self::assertSame([], $this->client($http)->getContingentPools(1));
}
public function testAFailedRequestThrows(): void
{
$http = new MockHttpClient(new MockResponse('', ['http_code' => 500]));
$this->expectException(BpnConnectException::class);
$this->client($http)->getContingentPools(163113);
}
/**
* @param array<string, mixed> $payload
*/
private function jsonResponse(array $payload): MockResponse
{
return new MockResponse(json_encode($payload, JSON_THROW_ON_ERROR), [
'response_headers' => ['content-type' => 'application/json'],
]);
}
private function client(MockHttpClient $http): RoomsClient
{
return new RoomsClient($http, new NullLogger(), 'https://bpn-connect.test', 'secret');
}
}
@@ -14,6 +14,7 @@ use App\Form\Model\ParticipantDto;
use App\Service\BookingConfigurator;
use App\Service\BookingSessionManager;
use App\Service\ParticipantEligibilityChecker;
use App\Service\RoomContingentPoolResolver;
use App\Service\TravelDataProvider;
use PHPUnit\Framework\TestCase;
@@ -38,6 +39,7 @@ class BookingConfiguratorBabyTest extends TestCase
$this->participantEligibilityService,
$bookingStatusRuleRegistry,
$this->createStub(AgencyLoader::class),
$this->createStub(RoomContingentPoolResolver::class),
'F' // default booking status
);
}
@@ -11,9 +11,11 @@ use App\BusProNet\Service\BookingStatusRuleRegistry;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Form\Model\BookingDto;
use App\Form\Model\RoomSelectionDto;
use App\Model\RoomContingentPool;
use App\Service\BookingConfigurator;
use App\Service\BookingSessionManager;
use App\Service\ParticipantEligibilityChecker;
use App\Service\RoomContingentPoolResolver;
use App\Service\TravelDataProvider;
use PHPUnit\Framework\TestCase;
@@ -162,14 +164,61 @@ class BookingConfiguratorRoomReconciliationTest extends TestCase
$this->assertSame([], $bookingDto->roomSelections);
}
private function bookingService(): BookingConfigurator
public function testCapsRoomsSharingAContingentTogetherInSelectionOrder(): void
{
$eight = $this->createRoom(2366, '8er App mit 8 Personen', available: 1, minPax: 8);
$seven = $this->createRoom(2367, '8er App mit 7 Personen', available: 1, minPax: 7);
$travel = $this->createTravel([$eight, $seven]);
$bookingDto = new BookingDto($travel, 1);
$bookingDto->roomSelections = [
2366 => $this->createRoomSelection(2366, quantity: 1),
2367 => $this->createRoomSelection(2367, quantity: 1),
];
$adjustments = $this->bookingService([new RoomContingentPool([2366 => $eight, 2367 => $seven])])
->reconcileRoomSelections($bookingDto);
$this->assertCount(1, $adjustments);
$this->assertSame(2367, $adjustments[0]->roomId);
$this->assertTrue($adjustments[0]->isDropped());
$this->assertSame(1, $bookingDto->roomSelections[2366]->quantity);
$this->assertSame(0, $bookingDto->roomSelections[2367]->quantity);
}
public function testLeavesRoomsSharingAContingentAloneWhileTheyFit(): void
{
$eight = $this->createRoom(2366, '8er App mit 8 Personen', available: 2, minPax: 8);
$seven = $this->createRoom(2367, '8er App mit 7 Personen', available: 2, minPax: 7);
$travel = $this->createTravel([$eight, $seven]);
$bookingDto = new BookingDto($travel, 1);
$bookingDto->roomSelections = [
2366 => $this->createRoomSelection(2366, quantity: 1),
2367 => $this->createRoomSelection(2367, quantity: 1),
];
$adjustments = $this->bookingService([new RoomContingentPool([2366 => $eight, 2367 => $seven])])
->reconcileRoomSelections($bookingDto);
$this->assertSame([], $adjustments);
}
/**
* @param list<RoomContingentPool> $sharedPools
*/
private function bookingService(array $sharedPools = []): BookingConfigurator
{
$poolResolver = $this->createStub(RoomContingentPoolResolver::class);
$poolResolver->method('findSharedPools')->willReturn($sharedPools);
return new BookingConfigurator(
$this->createStub(BookingSessionManager::class),
$this->createStub(TravelDataProvider::class),
$this->createStub(ParticipantEligibilityChecker::class),
$this->createStub(BookingStatusRuleRegistry::class),
$this->createStub(AgencyLoader::class),
$poolResolver,
'F'
);
}
@@ -19,6 +19,7 @@ use App\Form\Model\ParticipantDto;
use App\Service\BookingConfigurator;
use App\Service\BookingSessionManager;
use App\Service\ParticipantEligibilityChecker;
use App\Service\RoomContingentPoolResolver;
use App\Service\TravelDataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
@@ -45,6 +46,7 @@ class BookingConfiguratorStatusTest extends TestCase
$participantEligibility,
$bookingStatusRuleRegistry,
$agencyLoader,
$this->createStub(RoomContingentPoolResolver::class),
'F' // default booking status
);
}
@@ -220,6 +222,7 @@ class BookingConfiguratorStatusTest extends TestCase
new Selection1473StatusRule(),
]),
$this->createStub(AgencyLoader::class),
$this->createStub(RoomContingentPoolResolver::class),
'F'
);
@@ -313,6 +316,7 @@ class BookingConfiguratorStatusTest extends TestCase
$this->createStub(ParticipantEligibilityChecker::class),
$bookingStatusRuleRegistry,
$this->createStub(AgencyLoader::class),
$this->createStub(RoomContingentPoolResolver::class),
'F'
);
@@ -344,6 +348,7 @@ class BookingConfiguratorStatusTest extends TestCase
$this->createStub(ParticipantEligibilityChecker::class),
$bookingStatusRuleRegistry,
$this->createStub(AgencyLoader::class),
$this->createStub(RoomContingentPoolResolver::class),
'F'
);
@@ -11,11 +11,13 @@ use App\Form\Model\BookingDto;
use App\Form\Model\BookingSummaryDto;
use App\Form\Model\ParticipantCardDataDto;
use App\Form\Model\ParticipantCardPriceDto;
use App\Model\RoomContingentPool;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingPriceCalculator;
use App\Service\BookingSummaryAssembler;
use App\Service\FamilyInsuranceAvailabilityChecker;
use App\Service\ParticipantCardAssembler;
use App\Service\RoomContingentPoolResolver;
use App\Service\RoomPricingCalculator;
use PHPUnit\Framework\TestCase;
@@ -71,6 +73,7 @@ class BookingCreateContextFactoryTest extends TestCase
$summaryDataService,
$priceCalculator,
$familyInsuranceService,
$this->createStub(RoomContingentPoolResolver::class),
);
$context = $service->create($bookingDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
@@ -134,6 +137,7 @@ class BookingCreateContextFactoryTest extends TestCase
$summaryDataService,
$priceCalculator,
$familyInsuranceService,
$this->createStub(RoomContingentPoolResolver::class),
);
$context = $service->createWithParticipantCards($bookingDto, true);
@@ -188,6 +192,7 @@ class BookingCreateContextFactoryTest extends TestCase
$summaryDataService,
$priceCalculator,
$familyInsuranceService,
$this->createStub(RoomContingentPoolResolver::class),
);
$context = $service->createWithParticipantPrices($bookingDto);
@@ -198,4 +203,47 @@ class BookingCreateContextFactoryTest extends TestCase
$this->assertNull($context->cardsData);
$this->assertSame([10 => $room], $context->groupedRooms->byRoom);
}
public function testCreateForRoomSelectionIndexesSharedPoolsByMemberRoom(): void
{
$travel = new Travel();
$travel->dateFrom = new \DateTimeImmutable('2030-01-01');
$travel->dateTo = new \DateTimeImmutable('2030-01-06');
$eight = new Room();
$eight->id = 2366;
$eight->label = '8er App mit 8 Personen';
$eight->maxPax = 8;
$eight->available = 1;
$eight->status = 'Frei';
$seven = new Room();
$seven->id = 2367;
$seven->label = '8er App mit 7 Personen';
$seven->maxPax = 7;
$seven->available = 1;
$seven->status = 'Frei';
$travel->rooms = [$eight, $seven];
$bookingDto = new BookingDto($travel, 163113);
$pool = new RoomContingentPool([2366 => $eight, 2367 => $seven]);
$poolResolver = $this->createStub(RoomContingentPoolResolver::class);
$poolResolver->method('findSharedPools')->willReturn([$pool]);
$summaryDataService = $this->createStub(BookingSummaryAssembler::class);
$summaryDataService->method('getSummaryData')->willReturn($this->createStub(BookingSummaryDto::class));
$service = new BookingCreateContextFactory(
$this->createStub(ParticipantCardAssembler::class),
$summaryDataService,
$this->createStub(BookingPriceCalculator::class),
$this->createStub(FamilyInsuranceAvailabilityChecker::class),
$poolResolver,
);
$context = $service->createForRoomSelection($bookingDto);
$this->assertSame([2366 => $pool, 2367 => $pool], $context->roomContingentPools);
}
}
@@ -0,0 +1,151 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BpnConnect\RoomsClient;
use App\BusProNet\Constants;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Travel;
use App\Service\RoomContingentPoolResolver;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
class RoomContingentPoolResolverTest extends TestCase
{
private const array POOLS_163113 = [
['room_type_id' => 27, 'pool_room_type_id' => 27],
['room_type_id' => 2366, 'pool_room_type_id' => 2366],
['room_type_id' => 2367, 'pool_room_type_id' => 2366],
['room_type_id' => 2368, 'pool_room_type_id' => 2366],
];
public function testGroupsOfferedRoomsThatShareAContingent(): void
{
$resolver = $this->resolver(new MockHttpClient($this->poolsResponse(self::POOLS_163113)));
$pools = $resolver->findSharedPools($this->createTravel([
$this->createRoom(27, 'Doppelzimmer', available: 3),
$this->createRoom(2366, '8er App mit 8 Personen', available: 1),
$this->createRoom(2367, '8er App mit 7 Personen', available: 1),
$this->createRoom(2368, '8er App mit 6 Personen', available: 1),
]));
self::assertCount(1, $pools);
self::assertSame([2366, 2367, 2368], array_keys($pools[0]->rooms));
self::assertSame(1, $pools[0]->getAvailable());
}
public function testLeavesOutPoolsWithASingleOfferedRoom(): void
{
$resolver = $this->resolver(new MockHttpClient($this->poolsResponse(self::POOLS_163113)));
$pools = $resolver->findSharedPools($this->createTravel([
$this->createRoom(2366, '8er App mit 8 Personen', available: 1),
$this->createRoom(2367, '8er App mit 7 Personen', available: 0),
]));
self::assertSame([], $pools);
}
public function testAsksBpnConnectOncePerHotel(): void
{
$calls = 0;
$http = new MockHttpClient(function () use (&$calls): MockResponse {
++$calls;
return $this->poolsResponse(self::POOLS_163113);
});
$resolver = $this->resolver($http);
$travel = $this->sharedPoolTravel();
$resolver->findSharedPools($travel);
$resolver->findSharedPools($travel);
self::assertSame(1, $calls);
}
public function testTreatsEveryRoomAsItsOwnPoolWhenBpnConnectFailsAndAsksAgainNextTime(): void
{
$calls = 0;
$http = new MockHttpClient(function () use (&$calls): MockResponse {
++$calls;
return 1 === $calls
? new MockResponse('', ['http_code' => 503])
: $this->poolsResponse(self::POOLS_163113);
});
$resolver = $this->resolver($http);
$travel = $this->sharedPoolTravel();
self::assertSame([], $resolver->findSharedPools($travel));
self::assertCount(1, $resolver->findSharedPools($travel));
self::assertSame(2, $calls);
}
public function testATravelWithoutHotelHasNoPools(): void
{
$http = new MockHttpClient(function (): MockResponse {
self::fail('bpn-connect must not be asked without a hotel');
});
$travel = $this->sharedPoolTravel();
$travel->hotelId = null;
self::assertSame([], $this->resolver($http)->findSharedPools($travel));
}
private function sharedPoolTravel(): Travel
{
return $this->createTravel([
$this->createRoom(2366, '8er App mit 8 Personen', available: 1),
$this->createRoom(2367, '8er App mit 7 Personen', available: 1),
]);
}
/**
* @param list<array{room_type_id: int, pool_room_type_id: int}> $data
*/
private function poolsResponse(array $data): MockResponse
{
return new MockResponse(json_encode(['meta' => ['hotel_id' => 163113], 'data' => $data], JSON_THROW_ON_ERROR), [
'response_headers' => ['content-type' => 'application/json'],
]);
}
private function resolver(MockHttpClient $http): RoomContingentPoolResolver
{
return new RoomContingentPoolResolver(
new RoomsClient($http, new NullLogger(), 'https://bpn-connect.test', 'secret'),
new ArrayAdapter(),
new NullLogger(),
);
}
/**
* @param list<Room> $rooms
*/
private function createTravel(array $rooms): Travel
{
$travel = new Travel();
$travel->hotelId = 163113;
$travel->rooms = $rooms;
return $travel;
}
private function createRoom(int $id, string $label, int $available): Room
{
$room = new Room();
$room->id = $id;
$room->label = $label;
$room->available = $available;
$room->status = Constants::STATUS_AVAILABLE;
return $room;
}
}
@@ -8,15 +8,81 @@ use App\BusProNet\Model\Room;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\RoomSelectionDto;
use App\Model\RoomContingentPool;
use App\Service\RoomContingentPoolResolver;
use App\Validator\Constraints\RoomSelection;
use App\Validator\Constraints\RoomSelectionValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
class RoomSelectionValidatorTest extends ConstraintValidatorTestCase
{
/** @var list<RoomContingentPool> */
private array $sharedPools = [];
protected function createValidator(): RoomSelectionValidator
{
return new RoomSelectionValidator();
$poolResolver = $this->createStub(RoomContingentPoolResolver::class);
$poolResolver->method('findSharedPools')->willReturnCallback(fn (): array => $this->sharedPools);
return new RoomSelectionValidator($poolResolver);
}
public function testRoomsSharingAContingentMayNotTogetherExceedIt(): void
{
$bookingDto = $this->createBookingDto([
['id' => 2366, 'code' => '8erSudel', 'quantity' => 1],
['id' => 2367, 'code' => '8erSudel', 'quantity' => 1],
]);
$this->shareContingent($bookingDto, [2366, 2367], available: 1);
$this->validator->validate($bookingDto, new RoomSelection());
$this->buildViolation('Diese Zimmer teilen sich ein Kontingent, von dem nur noch {{ available }} verfügbar ist: {{ rooms }}. Bitte reduziere deine Auswahl.')
->setParameter('{{ available }}', '1')
->setParameter('{{ rooms }}', 'Zimmer 2366, Zimmer 2367')
->assertRaised();
}
public function testRoomsSharingAContingentPassWhileTheyFit(): void
{
$bookingDto = $this->createBookingDto([
['id' => 2366, 'code' => '8erSudel', 'quantity' => 1],
['id' => 2367, 'code' => '8erSudel', 'quantity' => 1],
]);
$this->shareContingent($bookingDto, [2366, 2367], available: 2);
$this->validator->validate($bookingDto, new RoomSelection());
$this->assertNoViolation();
}
public function testAnUnselectedRoomOfASharedContingentDoesNotCount(): void
{
$bookingDto = $this->createBookingDto([
['id' => 2366, 'code' => '8erSudel', 'quantity' => 1],
['id' => 2367, 'code' => '8erSudel', 'quantity' => 0],
]);
$this->shareContingent($bookingDto, [2366, 2367], available: 1);
$this->validator->validate($bookingDto, new RoomSelection());
$this->assertNoViolation();
}
/**
* @param list<int> $roomIds
*/
private function shareContingent(BookingDto $bookingDto, array $roomIds, int $available): void
{
$rooms = [];
foreach ($bookingDto->travel->rooms as $room) {
if (in_array($room->id, $roomIds, true)) {
$room->available = $available;
$rooms[$room->id] = $room;
}
}
$this->sharedPools = [new RoomContingentPool($rooms)];
}
public function testNoRoomSelectedFailsValidation(): void
@@ -141,6 +207,7 @@ class RoomSelectionValidatorTest extends ConstraintValidatorTestCase
$room = new Room();
$room->id = $data['id'];
$room->code = $data['code'];
$room->label = 'Zimmer '.$data['id'];
$room->available = 10;
$room->status = 'Frei';
$travel->rooms[] = $room;