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 testAnEscortWithBothQuestionnairesSubmittedIsNoLongerPending(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true); $this->respond($disposition, submit: true, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK); $this->assertSame([], $this->pending()); } /** * A bus escort owes both questionnaires; the teamer feedback alone does not settle it. */ public function testAnEscortWithoutTheBusFeedbackIsStillPending(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true); $this->assertSame([['disposition' => $disposition, 'draft' => false]], $this->pending()); } public function testAnEscortWithABusFeedbackDraftSaysDraft(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true); $this->respond($disposition, submit: false, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK); $this->assertSame([['disposition' => $disposition, 'draft' => true]], $this->pending()); } /** * Each questionnaire key belongs in its own EXISTS: the response to one questionnaire must not * stand in for the other's. */ public function testTheBusFeedbackDoesNotStandInForTheTeamerFeedback(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK); $this->assertSame([['disposition' => $disposition, 'draft' => false]], $this->pending()); } public function testAPlacementWithoutBusEscortOnlyOwesTheTeamerFeedback(): void { $disposition = $this->escort(); $disposition->getAssignment()->setPickup(0); $this->respond($disposition, submit: true); $this->assertSame([], $this->pending()); } public function testAPlacementWithoutBusEscortIsPendingToo(): void { $disposition = $this->escort(); $disposition->getAssignment()->setPickup(0); $this->assertSame([['disposition' => $disposition, 'draft' => false]], $this->pending()); } /** * Only submitting ends it, however long ago the trip ended. */ public function testAnUnfinishedQuestionnaireStaysPendingLongAfterTheTrip(): void { $this->escort(dateTo: self::GO_LIVE); $this->assertCount(1, $this->pending()); } /** * Two leg drafts are one placement on the dashboard, not two rows. */ public function testBothLegDraftsListThePlacementOnce(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true); $this->respond($disposition, submit: false, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK, leg: BusLeg::OUTBOUND); $this->respond($disposition, submit: false, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK, leg: BusLeg::RETURN); $this->assertSame([['disposition' => $disposition, 'draft' => true]], $this->pending()); } /** * An escort who rode only the return bus has a single leg response; it settles the bus * feedback. */ public function testASubmittedReturnLegSettlesTheBusFeedback(): void { $disposition = $this->escort(); $this->respond($disposition, submit: true); $this->respond($disposition, submit: true, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK, leg: BusLeg::RETURN); $this->assertSame([], $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 */ public static function notPending(): array { return [ '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()); } /** * The mail starts the day after the trip: on its last day the teamer is only just back. */ public function testTheReminderStartsTheDayAfterTheTrip(): void { $this->withUser($this->teamer); $yesterday = $this->escort(dateTo: 'yesterday'); $this->escort(dateTo: 'today'); $this->assertSame([$yesterday], $this->reminded()); } public function testTheReminderCoversEveryTeamerAndKeepsGoingUntilSubmitted(): void { $this->withUser($this->teamer); $other = (new Teamer())->setFirstName('Alex')->setLastName('Beispiel')->setGender('d'); $this->entityManager->persist($other); $this->withUser($other); $old = $this->escort(dateTo: self::GO_LIVE); $draft = $this->escort(teamer: $other); $this->respond($draft, submit: false); $done = $this->escort(); $this->respond($done, submit: true); $this->respond($done, submit: true, questionnaire: QuestionnaireCatalog::BUS_FEEDBACK); $reminded = $this->reminded(); $this->assertContains($old, $reminded); $this->assertContains($draft, $reminded); $this->assertNotContains($done, $reminded); } /** * A teamer who cannot log in cannot submit, and would be mailed every day forever. * * @param \Closure(Teamer, ?\App\Entity\User): mixed $change */ #[\PHPUnit\Framework\Attributes\DataProvider('notReminded')] public function testTeamersWhoCannotLogInAreNotReminded(bool $withUser, \Closure $change): void { $user = $withUser ? $this->withUser($this->teamer) : null; $change($this->teamer, $user); $this->escort(); $this->assertSame([], $this->reminded()); } /** * @return array */ public static function notReminded(): array { return [ 'no user account' => [false, static fn () => null], 'user blocked' => [true, static fn (Teamer $t, $user) => $user->setDisabledAt(new \DateTimeImmutable('yesterday'))], 'teamer deleted' => [true, static fn (Teamer $t) => $t->setDeleted()], ]; } /** * @return list */ private function pending(): array { $this->entityManager->flush(); return $this->repository->findPendingQuestionnaireByTeamer($this->teamer, new \DateTimeImmutable(self::GO_LIVE)); } /** * @return list */ private function reminded(): array { $this->entityManager->flush(); return $this->repository->findQuestionnaireReminders(new \DateTimeImmutable(self::GO_LIVE), new \DateTimeImmutable('today')); } }