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

183 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Repository;
use App\Config\QuestionnaireCatalog;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\Disposition;
use App\Entity\QuestionnaireResponse;
use App\Entity\Teamer;
use App\Model\Questionnaire\Definition;
use App\Repository\DispositionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Which questionnaires the dashboard reminds a teamer of. Run against db_test with real rows,
* because the question is which rows come back: the response is joined as an arbitrary entity,
* and a wrong JOIN condition would quietly let a draft, or a response to another questionnaire,
* decide the outcome.
*
* Every test runs inside a transaction that is rolled back, so db_test - shared between
* branches - keeps nothing.
*/
class DispositionRepositoryQuestionnaireTest extends KernelTestCase
{
private const string GO_LIVE = '2026-01-01';
private EntityManagerInterface $entityManager;
private DispositionRepository $repository;
private Teamer $teamer;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->repository = $this->entityManager->getRepository(Disposition::class);
$this->entityManager->getConnection()->beginTransaction();
$this->teamer = (new Teamer())->setFirstName('Kim')->setLastName('Muster')->setGender('d');
$this->entityManager->persist($this->teamer);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->rollBack();
parent::tearDown();
}
public function testAnEscortWithoutAResponseIsPending(): void
{
$disposition = $this->escort();
$this->assertSame([['disposition' => $disposition, 'draft' => false]], $this->pending());
}
public function testADraftIsStillPendingAndSaysSo(): void
{
$disposition = $this->escort();
$this->respond($disposition, submit: false);
$this->assertSame([['disposition' => $disposition, 'draft' => true]], $this->pending());
}
public function testASubmittedQuestionnaireIsNoLongerPending(): void
{
$this->respond($this->escort(), submit: true);
$this->assertSame([], $this->pending());
}
/**
* The questionnaire key belongs in the JOIN condition: in the WHERE clause, a submitted
* response to some other questionnaire would hide this one.
*/
public function testAResponseToAnotherQuestionnaireDoesNotCount(): void
{
$disposition = $this->escort();
$this->respond($disposition, submit: true, questionnaire: 'other');
$this->assertSame([['disposition' => $disposition, 'draft' => false]], $this->pending());
}
public function testAnAssignmentEndingTodayIsPending(): void
{
$this->escort(dateTo: 'today');
$this->assertCount(1, $this->pending());
}
/**
* @param \Closure(Disposition): mixed $change
*/
#[\PHPUnit\Framework\Attributes\DataProvider('notPending')]
public function testIsNotPending(\Closure $change): void
{
$change($this->escort());
$this->entityManager->flush();
$this->assertSame([], $this->pending());
}
/**
* @return array<string, array{\Closure(Disposition): mixed}>
*/
public static function notPending(): array
{
return [
'no bus escort' => [static fn (Disposition $d) => $d->getAssignment()->setPickup(0)],
'not over yet' => [static fn (Disposition $d) => $d->getAssignment()->getDestination()->setDateTo(new \DateTimeImmutable('tomorrow'))],
'ended before go-live' => [static fn (Disposition $d) => $d->getAssignment()->getDestination()->setDateTo(new \DateTimeImmutable('2025-12-31'))],
'skip formalities' => [static fn (Disposition $d) => $d->getAssignment()->setSkipFormalities(true)],
'called off' => [static fn (Disposition $d) => $d->setStatus(Disposition::STATUS_CALLED_OFF)],
'assignment called off' => [static fn (Disposition $d) => $d->getAssignment()->setStatus(Assignment::STATUS_CALLED_OFF)],
];
}
public function testAnotherTeamersEscortIsNotListed(): void
{
$other = (new Teamer())->setFirstName('Alex')->setLastName('Beispiel')->setGender('d');
$this->entityManager->persist($other);
$this->escort(teamer: $other);
$this->assertSame([], $this->pending());
}
/**
* @return list<array{disposition: Disposition, draft: bool}>
*/
private function pending(): array
{
$this->entityManager->flush();
return $this->repository->findPendingQuestionnaireByTeamer($this->teamer, new \DateTimeImmutable(self::GO_LIVE));
}
private function escort(string $dateTo = 'yesterday', ?Teamer $teamer = null): Disposition
{
$destination = (new Destination())
->setProduct('Davos Klosters - Schwendi')
->setHotel('Schwendi')
->setHotelCode('DKI')
->setHotelBusProId(1)
->setDateFrom(new \DateTimeImmutable($dateTo.' -7 days'))
->setDateTo(new \DateTimeImmutable($dateTo))
->setPickups([['busProId' => 4711, 'city' => 'Stuttgart', 'street' => 'Busbahnhof', 'time' => '21:30']])
;
$assignment = (new Assignment())
->setDestination($destination)
->setPickup(4711)
->setStatus(Assignment::STATUS_STAFFED)
;
$disposition = (new Disposition(new Application($assignment, $teamer ?? $this->teamer)))
->setStatus(Disposition::STATUS_ENDED)
;
$this->entityManager->persist($destination);
$this->entityManager->persist($assignment);
$this->entityManager->persist($disposition);
return $disposition;
}
private function respond(Disposition $disposition, bool $submit, string $questionnaire = QuestionnaireCatalog::TEAMER_ESCORT): void
{
$definition = new Definition($questionnaire, 1, 'Beispiel', null, []);
$response = QuestionnaireResponse::forDisposition($definition, $disposition);
if ($submit) {
$response->submit($definition, [], [], new \DateTimeImmutable());
} else {
$response->saveDraft([], []);
}
$this->entityManager->persist($response);
}
}