feat: lock pooled rooms once the pool is used up
This commit is contained in:
1 parent
a1645784fe
commit
ac23baf625
5 files changed
+157
-11
No files matched your search
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Form;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Room;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\BookingCreateStep1Type;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
use App\Model\RoomContingentPool;
|
||||
use App\Service\RoomContingentPoolResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Form\Forms;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\PreloadedExtension;
|
||||
|
||||
class BookingCreateStep1TypeTest extends TestCase
|
||||
{
|
||||
public function testSelectingOneRoomOfAnExhaustedPoolLocksTheOthers(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366), 'the selected room keeps its quantity');
|
||||
self::assertSame(0, $this->maxValue($view, 2367));
|
||||
self::assertSame(3, $this->maxValue($view, 27), 'a room outside the pool is untouched');
|
||||
}
|
||||
|
||||
public function testRoomsOfAPoolWithRoomLeftShareWhatRemains(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 2, quantities: [2366 => 1, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(2, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
public function testNothingSelectedLeavesEachRoomAtThePoolCount(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 0, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
public function testAnOverSelectedPoolNeverCapsBelowTheSelection(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 1, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $quantities
|
||||
*/
|
||||
private function createView(int $poolAvailable, array $quantities): FormView
|
||||
{
|
||||
$eight = $this->createRoom(2366, $poolAvailable);
|
||||
$seven = $this->createRoom(2367, $poolAvailable);
|
||||
$double = $this->createRoom(27, 3);
|
||||
|
||||
$travel = new Travel();
|
||||
$travel->rooms = [$eight, $seven, $double];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 163113);
|
||||
foreach ([$eight, $seven, $double] as $room) {
|
||||
$selection = new RoomSelectionDto();
|
||||
$selection->id = $room->id;
|
||||
$selection->maxQuantity = (int) $room->available;
|
||||
$selection->quantity = $quantities[$room->id];
|
||||
$bookingDto->roomSelections[$room->id] = $selection;
|
||||
}
|
||||
|
||||
$poolResolver = $this->createStub(RoomContingentPoolResolver::class);
|
||||
$poolResolver->method('findSharedPools')->willReturn([new RoomContingentPool([2366 => $eight, 2367 => $seven])]);
|
||||
|
||||
return Forms::createFormFactoryBuilder()
|
||||
->addExtension(new PreloadedExtension([new BookingCreateStep1Type($poolResolver)], []))
|
||||
->getFormFactory()
|
||||
->create(BookingCreateStep1Type::class, $bookingDto)
|
||||
->createView();
|
||||
}
|
||||
|
||||
private function maxValue(FormView $view, int $roomId): int
|
||||
{
|
||||
return $view['roomSelections'][$roomId]['quantity']->vars['max_value'];
|
||||
}
|
||||
|
||||
private function createRoom(int $id, int $available): Room
|
||||
{
|
||||
$room = new Room();
|
||||
$room->id = $id;
|
||||
$room->label = 'Zimmer '.$id;
|
||||
$room->available = $available;
|
||||
$room->status = Constants::STATUS_AVAILABLE;
|
||||
|
||||
return $room;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user