Files
myep-team/tests/Repository/StaffingCandidateQueryTest.php
T

106 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Repository;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Repository\AssignmentRepository;
use App\Repository\DestinationRepository;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Which dates the staffing matrix applies to, and which of them it must leave alone. Both are
* "does it return the right rows" questions, so they run against real rows.
*/
class StaffingCandidateQueryTest extends KernelTestCase
{
private const LATEST_IMPORT = '2026-10-15 03:20:00';
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$connection = self::getContainer()->get(Connection::class);
$connection->executeStatement('DELETE FROM assignment_fee');
$connection->executeStatement('DELETE FROM assignment');
$connection->executeStatement('DELETE FROM destination');
}
public function testOnlyLiveFutureDatesOfMatrixProductsFromTheLatestImport(): void
{
$match = $this->destination('DKIMP110127', 'DKIMP');
$this->destination('DKIMP040127', 'DKIMP', dateFrom: '2026-10-15'); // starts today
$this->destination('DKIMP180127', 'DKIMP', importedAt: '2026-10-14 03:20:00'); // cancelled upstream
$this->destination('DKIMP250127', 'DKIMP')->setDeleted();
$this->destination('DGSMP110127', 'DGSMP'); // not in the matrix
$this->destination('XYZ110127', null); // one-off date
$this->entityManager->flush();
$candidates = $this->destinationRepository()->getStaffingCandidates(['DKIMP'], new \DateTimeImmutable('2026-10-15'));
$this->assertSame([$match], $candidates);
}
public function testAnEmptyMatrixMatchesNothing(): void
{
$this->destination('DKIMP110127', 'DKIMP');
$this->entityManager->flush();
$this->assertSame([], $this->destinationRepository()->getStaffingCandidates([], new \DateTimeImmutable('2026-10-15')));
}
public function testADeletedAssignmentStillCounts(): void
{
$untouched = $this->destination('DKIMP110127', 'DKIMP');
$live = $this->destination('DKIMP180127', 'DKIMP');
$deleted = $this->destination('DKIMP250127', 'DKIMP');
$this->entityManager->persist((new Assignment())->setDestination($live));
$this->entityManager->persist((new Assignment())->setDestination($deleted)->setDeleted());
$this->entityManager->flush();
/** @var AssignmentRepository $repository */
$repository = self::getContainer()->get(AssignmentRepository::class);
$ids = $repository->getDestinationIdsWithAnyAssignment([$untouched, $live, $deleted]);
sort($ids);
$this->assertSame([$live->getId(), $deleted->getId()], $ids);
}
private function destination(
string $code,
?string $productCode,
string $dateFrom = '2027-01-11',
string $importedAt = self::LATEST_IMPORT,
): Destination {
$destination = (new Destination())
->setCode($code)
->setProductCode($productCode)
->setDateFrom(new \DateTimeImmutable($dateFrom))
->setDateTo((new \DateTimeImmutable($dateFrom))->modify('+5 days'))
->setProduct('Davos Klosters - Schwendi')
->setHotel('Schwendi')
->setHotelCode('DKI')
->setHotelBusProId(1)
->setImportedAt(new \DateTimeImmutable($importedAt))
;
$this->entityManager->persist($destination);
return $destination;
}
private function destinationRepository(): DestinationRepository
{
return self::getContainer()->get(DestinationRepository::class);
}
}