repository = $this->createMock(DispositionRepository::class); $this->mailer = $this->createMock(Mailer::class); $this->service = new QuestionnaireReminderService($this->repository, $this->mailer, new NullLogger(), '2026-11-10'); } public function testItAsksForTheGoLiveDateAndToday(): void { $this->repository ->expects($this->once()) ->method('findQuestionnaireReminders') ->with(new \DateTimeImmutable('2026-11-10'), new \DateTimeImmutable('2026-11-20')) ->willReturn([]) ; $this->assertSame('Sent 0 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20 01:00'))); } public function testOneMailPerTeamerListsEveryPlacement(): void { $kim = $this->teamer('kim@example.org'); $alex = $this->teamer('alex@example.org'); $this->repository->method('findQuestionnaireReminders')->willReturn([ $this->placement($kim, '2026-11-14'), $this->placement($kim, '2026-11-07'), $this->placement($alex, '2026-11-14'), ]); $sent = []; $this->mailer ->expects($this->exactly(2)) ->method('createAndSendText') ->willReturnCallback(function (EmailTextKey $key, array $placeholders, array $options) use (&$sent): void { $this->assertSame(EmailTextKey::REMINDER_QUESTIONNAIRE, $key); $sent[$options['to']] = $placeholders['destinations']; }) ; $this->assertSame('Sent 2 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20'))); $this->assertSame(['kim@example.org', 'alex@example.org'], array_keys($sent)); // the placement on its 13th night is not due by itself, but comes along with the one that // is; labelled as everywhere in the reminders, by Destination::__toString() $this->assertSame('07.11.26 - 14.11.26 Skireise, 31.10.26 - 07.11.26 Skireise', $sent['kim@example.org']); $this->assertSame('07.11.26 - 14.11.26 Skireise', $sent['alex@example.org']); } /** * Nightly for a week, then weekly: a teamer who never submits is not mailed every night for * the rest of the season. */ public function testItRemindsNightlyForAWeekThenWeekly(): void { $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement($this->teamer('kim@example.org'), '2026-11-14')]); $nights = []; for ($night = 1; $night <= 30; ++$night) { $today = new \DateTimeImmutable('2026-11-14')->modify(sprintf('+%d days', $night)); if ('Sent 1 questionnaire reminders to teamers' === $this->service->sendReminders($today)) { $nights[] = $night; } } $this->assertSame([1, 2, 3, 4, 5, 6, 7, 14, 21, 28], $nights); } /** * On its 13th night a placement waits for the weekly 14th. */ public function testAPlacementBetweenItsWeeklyNightsAloneSendsNothing(): void { $kim = $this->teamer('kim@example.org'); $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement($kim, '2026-11-07')]); $this->assertSame('Sent 0 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20'))); } public function testATeamerWithoutAnAddressIsSkipped(): void { $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement(new Teamer(), '2026-11-14')]); $this->mailer->expects($this->never())->method('createAndSendText'); $this->assertSame('Sent 0 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20'))); } public function testAMalformedGoLiveDateFailsLoudly(): void { $this->expectException(\InvalidArgumentException::class); new QuestionnaireReminderService($this->repository, $this->mailer, new NullLogger(), '10.11.2026'); } private function teamer(string $email): Teamer { return (new Teamer())->setCommunication((new Communication())->setEmail($email)); } private function placement(Teamer $teamer, string $dateTo): Disposition { $destination = (new Destination()) ->setProduct('Skireise') ->setDateFrom(new \DateTimeImmutable($dateTo.' -7 days')) ->setDateTo(new \DateTimeImmutable($dateTo)) ; return new Disposition(new Application((new Assignment())->setDestination($destination), $teamer)); } }