Files
myep-team/tests/Service/Assignment/TimelineServiceTest.php
T

180 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service\Assignment;
use App\Config\HouseCatalog;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\Disposition;
use App\Entity\JobProfile;
use App\Entity\Teamer;
use App\Model\TimelineItem;
use App\Service\Assignment\TimelineService;
use Carbon\CarbonPeriodImmutable;
use PHPUnit\Framework\TestCase;
class TimelineServiceTest extends TestCase
{
public function testCalledOffDispositionsAreNotDrawnAndFreeTheirSlot(): void
{
$assignment = $this->assignment(2);
$this->dispose($assignment, 'Anna', Disposition::STATUS_CONFIRMED);
$this->dispose($assignment, 'Bert', Disposition::STATUS_CALLED_OFF);
$items = $this->sortByType($this->items($assignment));
$this->assertCount(2, $items);
$this->assertSame('Anna Probe', $items[0]->getTeamerName());
$this->assertSame(TimelineItem::TYPE_ASSIGNMENT, $items[1]->getType());
}
public function testVacancyIsOpenWithoutApplicationsEvenWhenPartlyStaffed(): void
{
$assignment = $this->assignment(2);
$this->dispose($assignment, 'Anna', Disposition::STATUS_CONFIRMED);
$assignment->setStaffingStatus(Assignment::STATUS_PARTLY_STAFFED);
$vacancy = $this->sortByType($this->items($assignment))[1];
$this->assertSame(TimelineItem::TYPE_ASSIGNMENT, $vacancy->getType());
$this->assertSame(TimelineItem::STATE_OPEN, $vacancy->getState());
}
public function testVacancyIsPendingWithAValidApplication(): void
{
$assignment = $this->assignment(1);
$assignment->addApplication(new Application($assignment, $this->teamer('Carla')));
$this->assertSame(TimelineItem::STATE_PENDING, $this->items($assignment)[0]->getState());
}
public function testVacancyIgnoresRejectedApplications(): void
{
$assignment = $this->assignment(1);
$application = new Application($assignment, $this->teamer('Carla'));
$application->setStatus(Application::STATUS_REJECTED);
$assignment->addApplication($application);
$this->assertSame(TimelineItem::STATE_OPEN, $this->items($assignment)[0]->getState());
}
/**
* Paid had no colour in the template and rendered like a vacancy.
*/
public function testDispositionStates(): void
{
$expected = [
Disposition::STATUS_NEW => TimelineItem::STATE_PENDING,
Disposition::STATUS_CHECKING_CONTRACT => TimelineItem::STATE_PENDING,
Disposition::STATUS_CONFIRMED => TimelineItem::STATE_STAFFED,
Disposition::STATUS_ENDED => TimelineItem::STATE_STAFFED,
Disposition::STATUS_CHECKING_INVOICE => TimelineItem::STATE_STAFFED,
Disposition::STATUS_PAID => TimelineItem::STATE_STAFFED,
Disposition::STATUS_COMPLETED => TimelineItem::STATE_STAFFED,
];
foreach ($expected as $status => $state) {
$assignment = $this->assignment(1);
$this->dispose($assignment, 'Anna', $status);
$this->assertSame($state, $this->items($assignment)[0]->getState(), $status);
}
}
/**
* Trips running into or out of the window are clipped to its columns.
*/
public function testItemsArePlacedOnTheDayColumnsAndClipped(): void
{
$inside = $this->assignment(1);
$startsBefore = $this->assignment(1, '2026-11-28', '2026-12-03');
$endsAfter = $this->assignment(1, '2027-01-28', '2027-02-03');
$columns = array_map(
fn (Assignment $assignment) => [$this->items($assignment)[0]->getColumn(), $this->items($assignment)[0]->getSpan()],
[$inside, $startsBefore, $endsAfter],
);
// window 2026-12-01 to 2027-01-31: 5 December is column 5, eight days
$this->assertSame([[5, 8], [1, 3], [59, 4]], $columns);
}
public function testHousesCarryNameRowsAndOpenSlots(): void
{
$first = $this->assignment(2);
$overlapping = $this->assignment(1, '2026-12-10', '2026-12-14');
$overlapping->addApplication(new Application($overlapping, $this->teamer('Carla')));
$other = $this->assignment(1);
$other->getDestination()->setHotelCode('AGR02');
$houses = $this->service()->preprocess([$first, $overlapping, $other], $this->period());
// sorted by name: Rotbach (AGR) before Spinabad (DGS)
$this->assertSame(['Rotbach', 'Spinabad'], array_map(fn ($house) => $house->getName(), $houses));
$this->assertCount(3, $houses[1]->getRows());
$this->assertSame(2, $houses[1]->getOpenSlots());
}
/**
* @return list<TimelineItem>
*/
private function items(Assignment $assignment): array
{
$houses = $this->service()->preprocess([$assignment], $this->period());
return array_merge(...$houses[0]->getRows());
}
private function service(): TimelineService
{
return new TimelineService(new HouseCatalog(['AGR' => 'Rotbach', 'DGS' => 'Spinabad']));
}
private function period(): CarbonPeriodImmutable
{
return CarbonPeriodImmutable::create('2026-12-01', '2027-01-31');
}
/**
* @param list<TimelineItem> $items
*
* @return list<TimelineItem> dispositions first
*/
private function sortByType(array $items): array
{
usort($items, fn (TimelineItem $a, TimelineItem $b) => strcmp($b->getType(), $a->getType()));
return $items;
}
private function assignment(int $slots, string $from = '2026-12-05', string $to = '2026-12-12'): Assignment
{
$destination = (new Destination())
->setHotelCode('DGS01')
->setDateFrom(new \DateTimeImmutable($from))
->setDateTo(new \DateTimeImmutable($to))
;
return (new Assignment())
->setDestination($destination)
->setJobProfile((new JobProfile())->setName('Reiseleitung'))
->setAvailableDispositions($slots)
;
}
private function dispose(Assignment $assignment, string $firstName, string $status): void
{
$disposition = new Disposition(new Application($assignment, $this->teamer($firstName)));
$disposition->setStatus($status);
$assignment->addDisposition($disposition);
}
private function teamer(string $firstName): Teamer
{
return (new Teamer())->setFirstName($firstName)->setLastName('Probe');
}
}