112 lines
4.3 KiB
PHP
112 lines
4.3 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));
|
|
// 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]']);
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|