|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\EventListener; |
| 6 | + |
| 7 | +use App\Entity\Email as EmailEntity; |
| 8 | +use App\Entity\EmailDeliveryEvent as EmailDeliveryEventEntity; |
| 9 | +use App\Entity\EmailKind; |
| 10 | +use App\Entity\User as UserEntity; |
| 11 | +use App\EventSubscriber\EmailCreatedSubscriber; |
| 12 | +use App\Repository\UserRepository; |
| 13 | +use Doctrine\ORM\EntityManagerInterface; |
| 14 | +use Monolog\Logger; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 17 | +use Symfony\Component\Mailer\Envelope; |
| 18 | +use Symfony\Component\Mailer\Event\MessageEvent; |
| 19 | +use Symfony\Component\Mime\Email; |
| 20 | + |
| 21 | +class EmailCreatedSubscriberTest extends TestCase |
| 22 | +{ |
| 23 | + public function testTransactionalEmail(): void |
| 24 | + { |
| 25 | + $logger = new Logger('test'); |
| 26 | + |
| 27 | + $message = (new Email()) |
| 28 | + ->subject('subject') |
| 29 | + ->html('<div>body</div') |
| 30 | + ->from('demo-dbplay@example.com') |
| 31 | + ->to('test@example.com'); |
| 32 | + |
| 33 | + $headers = $message->getHeaders(); |
| 34 | + $headers = EmailKind::Test->addToEmailHeader($headers); |
| 35 | + $message->setHeaders($headers); |
| 36 | + |
| 37 | + $envelope = Envelope::create($message); |
| 38 | + |
| 39 | + $userRepository = self::createMock(UserRepository::class); |
| 40 | + $userRepository |
| 41 | + ->expects(self::once()) |
| 42 | + ->method('findOneBy') |
| 43 | + ->with(['email' => 'test@example.com']) |
| 44 | + ->willReturn(new UserEntity()); |
| 45 | + |
| 46 | + $invokedCount = self::exactly(2); |
| 47 | + /** |
| 48 | + * @var Email|null $emailInstance |
| 49 | + */ |
| 50 | + $emailInstance = null; |
| 51 | + $entityManager = self::createMock(EntityManagerInterface::class); |
| 52 | + $entityManager |
| 53 | + ->expects(self::exactly(2)) |
| 54 | + ->method('persist') |
| 55 | + ->willReturnCallback(function (mixed ...$parameters) use ($invokedCount, &$emailInstance): void { |
| 56 | + switch ($invokedCount->numberOfInvocations()) { |
| 57 | + case 1: |
| 58 | + $email = $parameters[0]; |
| 59 | + \assert($email instanceof EmailEntity); |
| 60 | + |
| 61 | + self::assertEquals('subject', $email->getSubject()); |
| 62 | + self::assertEquals('<div>body</div>', $email->getContent()); |
| 63 | + self::assertEquals(EmailKind::Test, $email->getKind()); |
| 64 | + |
| 65 | + $emailInstance = $email; |
| 66 | + break; |
| 67 | + case 2: |
| 68 | + $event = $parameters[0]; |
| 69 | + \assert($event instanceof EmailDeliveryEventEntity); |
| 70 | + |
| 71 | + self::assertEquals('test@example.com', $event->getToAddress()); |
| 72 | + self::assertEquals($emailInstance, $event->getEmail()); |
| 73 | + break; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + $subscriber = new EmailCreatedSubscriber($logger, $userRepository, $entityManager); |
| 78 | + $dispatcher = new EventDispatcher(); |
| 79 | + $event = new MessageEvent($message, $envelope, ''); |
| 80 | + |
| 81 | + $dispatcher->addSubscriber($subscriber); |
| 82 | + $dispatcher->dispatch($event); |
| 83 | + } |
| 84 | +} |
0 commit comments