145 lines
5.6 KiB
PHP
145 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service\Cron;
|
|
|
|
use App\Config\EmailTextKey;
|
|
use App\Email\Mailer;
|
|
use App\Entity\Application;
|
|
use App\Entity\Assignment;
|
|
use App\Entity\Destination;
|
|
use App\Entity\Disposition;
|
|
use App\Entity\Embeddable\Communication;
|
|
use App\Entity\Teamer;
|
|
use App\Repository\DispositionRepository;
|
|
use App\Service\Cron\QuestionnaireReminderService;
|
|
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
|
|
/**
|
|
* One mail per teamer, however many placements they owe a questionnaire for. Which placements
|
|
* those are is the repository's rule, pinned in DispositionRepositoryQuestionnaireTest.
|
|
*/
|
|
#[AllowMockObjectsWithoutExpectations]
|
|
class QuestionnaireReminderServiceTest extends TestCase
|
|
{
|
|
private DispositionRepository&MockObject $repository;
|
|
private Mailer&MockObject $mailer;
|
|
private QuestionnaireReminderService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->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('[email protected]');
|
|
$alex = $this->teamer('[email protected]');
|
|
|
|
$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(['[email protected]', '[email protected]'], 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['[email protected]']);
|
|
$this->assertSame('07.11.26 - 14.11.26 Skireise', $sent['[email protected]']);
|
|
}
|
|
|
|
/**
|
|
* 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('[email protected]'), '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('[email protected]');
|
|
$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));
|
|
}
|
|
}
|