mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-08 09:56:14 +00:00
Add testing methods for new mailer functions
This commit is contained in:
parent
48bc019932
commit
8cd6a3261c
@ -17,13 +17,18 @@ use Chill\MainBundle\Entity\User;
|
|||||||
use Chill\MainBundle\Notification\Email\NotificationMailer;
|
use Chill\MainBundle\Notification\Email\NotificationMailer;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Event\PostPersistEventArgs;
|
use Doctrine\ORM\Event\PostPersistEventArgs;
|
||||||
|
use PHPUnit\Framework\MockObject\Exception;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
use Symfony\Component\Mailer\MailerInterface;
|
use Symfony\Component\Mailer\MailerInterface;
|
||||||
|
use Symfony\Component\Messenger\Envelope;
|
||||||
|
use Symfony\Component\Messenger\MessageBusInterface;
|
||||||
use Symfony\Component\Mime\Email;
|
use Symfony\Component\Mime\Email;
|
||||||
use Symfony\Component\Translation\Translator;
|
use Symfony\Component\Translation\Translator;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
use Chill\MainBundle\Notification\Email\NotificationEmailMessages\SendImmediateNotificationEmailMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -112,13 +117,139 @@ class NotificationMailerTest extends TestCase
|
|||||||
$mailer->postPersistComment($comment, new PostPersistEventArgs($comment, $objectManager->reveal()));
|
$mailer->postPersistComment($comment, new PostPersistEventArgs($comment, $objectManager->reveal()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \ReflectionException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function testProcessNotificationForAddresseeWithImmediateEmailPreference(): void
|
||||||
|
{
|
||||||
|
// Create a mock notification
|
||||||
|
$notification = $this->prophesize(Notification::class);
|
||||||
|
$notification->getType()->willReturn('test_notification_type');
|
||||||
|
$notification->getId()->willReturn(123);
|
||||||
|
|
||||||
|
// Create a mock user
|
||||||
|
$user = $this->prophesize(User::class);
|
||||||
|
$user->getEmail()->willReturn('user@example.com');
|
||||||
|
$user->getId()->willReturn(456);
|
||||||
|
$user->getNotificationFlags()->willReturn(['test_notification_type' => 'immediate-email']);
|
||||||
|
|
||||||
|
$messageBus = $this->createMock(MessageBusInterface::class);
|
||||||
|
$messageBus->expects($this->once())
|
||||||
|
->method('dispatch')
|
||||||
|
->with($this->callback(function (SendImmediateNotificationEmailMessage $message) {
|
||||||
|
return 123 === $message->getNotificationId()
|
||||||
|
&& 456 === $message->getAddresseeId();
|
||||||
|
}))
|
||||||
|
->willReturn(new Envelope(new \stdClass()));
|
||||||
|
|
||||||
|
$mailer = $this->buildNotificationMailer(null, $messageBus);
|
||||||
|
|
||||||
|
// Call the method that processes notifications
|
||||||
|
$reflection = new \ReflectionClass(NotificationMailer::class);
|
||||||
|
$method = $reflection->getMethod('processNotificationForAddressee');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
$method->invoke($mailer, $notification->reveal(), $user->reveal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSendDailyDigest(): void
|
||||||
|
{
|
||||||
|
// Create a user
|
||||||
|
$user = new User();
|
||||||
|
$user->setEmail('user@example.com');
|
||||||
|
|
||||||
|
// Create some notifications
|
||||||
|
$notification = $this->prophesize(Notification::class);
|
||||||
|
$notification->getTitle()->willReturn('Notification 1');
|
||||||
|
$notification->getMessage()->willReturn('Message 1');
|
||||||
|
$notification->getId()->willReturn(123);
|
||||||
|
|
||||||
|
$notification2 = $this->prophesize(Notification::class);
|
||||||
|
$notification2->getTitle()->willReturn('Notification 2');
|
||||||
|
$notification2->getMessage()->willReturn('Message 2');
|
||||||
|
$notification2->getId()->willReturn(456);
|
||||||
|
|
||||||
|
$notifications = [$notification, $notification2];
|
||||||
|
|
||||||
|
// Mock the mailer to verify that an email is sent with the correct parameters
|
||||||
|
$mailer = $this->prophesize(MailerInterface::class);
|
||||||
|
$mailer->send(Argument::that(function ($email) use ($user) {
|
||||||
|
// Verify that the email is sent to the correct user
|
||||||
|
foreach ($email->getTo() as $address) {
|
||||||
|
if ($user->getEmail() === $address->getAddress()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}))->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
// Create a translator that returns a fixed string for the subject
|
||||||
|
$translator = $this->prophesize(TranslatorInterface::class);
|
||||||
|
$translator->trans('notification.Daily Notification Digest')->willReturn('Daily Digest');
|
||||||
|
|
||||||
|
// Create the notification mailer with the mocked mailer and translator
|
||||||
|
$notificationMailer = $this->buildNotificationMailer($mailer->reveal(), null, $translator->reveal());
|
||||||
|
|
||||||
|
// Call the sendDailyDigest method
|
||||||
|
$notificationMailer->sendDailyDigest($user, $notifications);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSendDailyDigestWithNoNotifications(): void
|
||||||
|
{
|
||||||
|
// Create a user
|
||||||
|
$user = new User();
|
||||||
|
$user->setEmail('user@example.com');
|
||||||
|
|
||||||
|
// Empty notifications array
|
||||||
|
$notifications = [];
|
||||||
|
|
||||||
|
// Mock the mailer to verify that no email is sent
|
||||||
|
$mailer = $this->prophesize(MailerInterface::class);
|
||||||
|
$mailer->send(Argument::any())->shouldNotBeCalled();
|
||||||
|
|
||||||
|
// Create the notification mailer with the mocked mailer
|
||||||
|
$notificationMailer = $this->buildNotificationMailer($mailer->reveal());
|
||||||
|
|
||||||
|
// Call the sendDailyDigest method
|
||||||
|
$notificationMailer->sendDailyDigest($user, $notifications);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSendDailyDigestWithUserHavingNoEmail(): void
|
||||||
|
{
|
||||||
|
// Create a user with no email
|
||||||
|
$user = new User();
|
||||||
|
$user->setEmail(null);
|
||||||
|
|
||||||
|
// Create some notifications
|
||||||
|
$notification = $this->prophesize(Notification::class);
|
||||||
|
$notification->getTitle()->willReturn('Notification 1');
|
||||||
|
$notification->getMessage()->willReturn('Message 1');
|
||||||
|
$notification->getId()->willReturn(123);
|
||||||
|
|
||||||
|
$notifications = [$notification];
|
||||||
|
|
||||||
|
// Mock the mailer to verify that no email is sent
|
||||||
|
$mailer = $this->prophesize(MailerInterface::class);
|
||||||
|
$mailer->send(Argument::any())->shouldNotBeCalled();
|
||||||
|
|
||||||
|
// Create the notification mailer with the mocked mailer
|
||||||
|
$notificationMailer = $this->buildNotificationMailer($mailer->reveal());
|
||||||
|
|
||||||
|
// Call the sendDailyDigest method
|
||||||
|
$notificationMailer->sendDailyDigest($user, $notifications);
|
||||||
|
}
|
||||||
|
|
||||||
private function buildNotificationMailer(
|
private function buildNotificationMailer(
|
||||||
?MailerInterface $mailer = null,
|
?MailerInterface $mailer = null,
|
||||||
|
?MessageBusInterface $messageBus = null,
|
||||||
|
?TranslatorInterface $translator = null,
|
||||||
): NotificationMailer {
|
): NotificationMailer {
|
||||||
return new NotificationMailer(
|
return new NotificationMailer(
|
||||||
$mailer,
|
$mailer ?? $this->prophesize(MailerInterface::class)->reveal(),
|
||||||
new NullLogger(),
|
new NullLogger(),
|
||||||
new Translator('fr')
|
$messageBus ?? $this->prophesize(MessageBusInterface::class)->reveal(),
|
||||||
|
$translator ?? new Translator('fr')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user