227 lines
7.4 KiB
PHP
227 lines
7.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Validator\Constraints;
|
|
|
|
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
|
|
{
|
|
$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
|
|
{
|
|
$bookingDto = $this->createBookingDto([]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->buildViolation('Bitte mindestens ein Zimmer/Bett auswählen')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testRegularRoomSelectedPassesValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testMultipleRegularRoomsSelectedPassesValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => 1],
|
|
['id' => 2, 'code' => '4erDW', 'quantity' => 2],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testOnlyBabyRoomSelectedFailsValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => 'Baby', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->buildViolation('Baby-Zimmer können nur in Kombination mit regulären Zimmern gebucht werden.')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testMultipleBabyRoomsOnlySelectedFailsValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => 'Baby', 'quantity' => 2],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->buildViolation('Baby-Zimmer können nur in Kombination mit regulären Zimmern gebucht werden.')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testBabyRoomWithRegularRoomPassesValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => 1],
|
|
['id' => 2, 'code' => 'Baby', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testBabyRoomWithMultipleRegularRoomsPassesValidation(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => 1],
|
|
['id' => 2, 'code' => '4erDW', 'quantity' => 2],
|
|
['id' => 3, 'code' => 'Baby', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testRoomWithZeroQuantityIsIgnored(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => 0],
|
|
['id' => 2, 'code' => 'Baby', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->buildViolation('Baby-Zimmer können nur in Kombination mit regulären Zimmern gebucht werden.')
|
|
->assertRaised();
|
|
}
|
|
|
|
public function testRoomWithNullQuantityIsIgnored(): void
|
|
{
|
|
$bookingDto = $this->createBookingDto([
|
|
['id' => 1, 'code' => '2erDW', 'quantity' => null],
|
|
['id' => 2, 'code' => 'Baby', 'quantity' => 1],
|
|
]);
|
|
|
|
$this->validator->validate($bookingDto, new RoomSelection());
|
|
|
|
$this->buildViolation('Baby-Zimmer können nur in Kombination mit regulären Zimmern gebucht werden.')
|
|
->assertRaised();
|
|
}
|
|
|
|
/**
|
|
* @param array<array{id: int, code: string, quantity: int|null}> $roomData
|
|
*/
|
|
private function createBookingDto(array $roomData): BookingDto
|
|
{
|
|
$travel = new Travel();
|
|
$travel->rooms = [];
|
|
|
|
$roomSelections = [];
|
|
|
|
foreach ($roomData as $data) {
|
|
$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;
|
|
|
|
$roomSelection = new RoomSelectionDto();
|
|
$roomSelection->id = $data['id'];
|
|
$roomSelection->quantity = $data['quantity'];
|
|
$roomSelections[] = $roomSelection;
|
|
}
|
|
|
|
$bookingDto = new BookingDto($travel, 1);
|
|
$bookingDto->roomSelections = $roomSelections;
|
|
|
|
return $bookingDto;
|
|
}
|
|
}
|