160 lines
4.9 KiB
PHP
160 lines
4.9 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\Validator\Constraints\RoomSelection;
|
|
use App\Validator\Constraints\RoomSelectionValidator;
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
|
|
|
class RoomSelectionValidatorTest extends ConstraintValidatorTestCase
|
|
{
|
|
protected function createValidator(): RoomSelectionValidator
|
|
{
|
|
return new RoomSelectionValidator();
|
|
}
|
|
|
|
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->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;
|
|
}
|
|
}
|