|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace QueueTest\App\Message; |
| 6 | + |
| 7 | +use Core\User\Repository\UserRepository; |
| 8 | +use Dot\Log\Logger; |
| 9 | +use Dot\Mail\Email; |
| 10 | +use Dot\Mail\Exception\MailException; |
| 11 | +use Dot\Mail\Service\MailService; |
| 12 | +use Exception; |
| 13 | +use Mezzio\Template\TemplateRendererInterface; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Queue\App\Message\ExampleMessage; |
| 17 | +use Queue\App\Message\ExampleMessageHandler; |
| 18 | +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 19 | + |
| 20 | +use function json_encode; |
| 21 | + |
| 22 | +class ExampleMessageHandlerTest extends TestCase |
| 23 | +{ |
| 24 | + protected MailService|MockObject $mailService; |
| 25 | + protected TemplateRendererInterface|MockObject $renderer; |
| 26 | + protected UserRepository|MockObject $userRepository; |
| 27 | + protected Logger $logger; |
| 28 | + protected array $config; |
| 29 | + private ExampleMessageHandler $handler; |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws Exception|\PHPUnit\Framework\MockObject\Exception |
| 33 | + */ |
| 34 | + public function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->mailService = $this->createMock(MailService::class); |
| 39 | + $this->renderer = $this->createMock(TemplateRendererInterface::class); |
| 40 | + $this->userRepository = $this->createMock(UserRepository::class); |
| 41 | + $this->logger = new Logger([ |
| 42 | + 'writers' => [ |
| 43 | + 'FileWriter' => [ |
| 44 | + 'name' => 'null', |
| 45 | + 'priority' => Logger::ALERT, |
| 46 | + ], |
| 47 | + ], |
| 48 | + ]); |
| 49 | + $this->config = [ |
| 50 | + 'notification' => [ |
| 51 | + 'server' => [ |
| 52 | + 'protocol' => 'tcp', |
| 53 | + 'host' => 'localhost', |
| 54 | + 'port' => '8556', |
| 55 | + 'eof' => "\n", |
| 56 | + ], |
| 57 | + ], |
| 58 | + 'application' => [ |
| 59 | + 'name' => 'dotkernel', |
| 60 | + ], |
| 61 | + ]; |
| 62 | + |
| 63 | + $this->handler = new ExampleMessageHandler( |
| 64 | + $this->mailService, |
| 65 | + $this->renderer, |
| 66 | + $this->userRepository, |
| 67 | + $this->logger, |
| 68 | + $this->config |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 74 | + */ |
| 75 | + public function testInvokeHandlesExceptionThrownByPerform(): void |
| 76 | + { |
| 77 | + $uuid = '1234'; |
| 78 | + |
| 79 | + $message = $this->createMock(ExampleMessage::class); |
| 80 | + $message->method('getPayload')->willReturn([ |
| 81 | + 'foo' => json_encode(['userUuid' => $uuid]), |
| 82 | + ]); |
| 83 | + |
| 84 | + $handlerMock = $this->getMockBuilder(ExampleMessageHandler::class) |
| 85 | + ->setConstructorArgs([ |
| 86 | + $this->mailService, |
| 87 | + $this->renderer, |
| 88 | + $this->userRepository, |
| 89 | + $this->logger, |
| 90 | + $this->config, |
| 91 | + ]) |
| 92 | + ->onlyMethods(['perform']) |
| 93 | + ->getMock(); |
| 94 | + |
| 95 | + $handlerMock |
| 96 | + ->expects($this->once()) |
| 97 | + ->method('perform') |
| 98 | + ->willThrowException(new \RuntimeException('Test exception')); |
| 99 | + |
| 100 | + $handlerMock->__invoke($message); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 105 | + */ |
| 106 | + public function testInvokeWithValidPayload(): void |
| 107 | + { |
| 108 | + $uuid = '1245'; |
| 109 | + |
| 110 | + $name = 'dotkernel'; |
| 111 | + |
| 112 | + $message = $this->createMock(ExampleMessage::class); |
| 113 | + $message->method('getPayload')->willReturn([ |
| 114 | + 'foo' => json_encode(['userUuid' => $uuid]), |
| 115 | + ]); |
| 116 | + |
| 117 | + $user = $this->getMockBuilder(\stdClass::class) |
| 118 | + ->addMethods(['getEmail', 'getName']) |
| 119 | + ->getMock(); |
| 120 | + $user->method('getEmail')->willReturn($email); |
| 121 | + $user->method('getName')->willReturn($name); |
| 122 | + |
| 123 | + $this->userRepository |
| 124 | + ->expects($this->once()) |
| 125 | + ->method('find') |
| 126 | + ->with($uuid) |
| 127 | + ->willReturn($user); |
| 128 | + |
| 129 | + $mailMessage = $this->createMock(Email::class); |
| 130 | + $mailMessage->expects($this->once()) |
| 131 | + ->method('addTo') |
| 132 | + ->with($email, $name); |
| 133 | + |
| 134 | + $this |
| 135 | + ->mailService |
| 136 | + ->method('getMessage') |
| 137 | + ->willReturn($mailMessage); |
| 138 | + |
| 139 | + $this |
| 140 | + ->mailService |
| 141 | + ->expects($this->once()) |
| 142 | + ->method('setSubject') |
| 143 | + ->with('Welcome to dotkernel'); |
| 144 | + |
| 145 | + $this->renderer->method('render')->willReturn('Rendered email body'); |
| 146 | + |
| 147 | + $this->mailService->expects($this->once()) |
| 148 | + ->method('setBody') |
| 149 | + ->with('Rendered email body'); |
| 150 | + |
| 151 | + $this->handler->__invoke($message); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @throws MailException |
| 156 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 157 | + */ |
| 158 | + public function testSendWelcomeMailHandlesMailException(): void |
| 159 | + { |
| 160 | + $uuid = '1234'; |
| 161 | + |
| 162 | + $reflection = new \ReflectionClass($this->handler); |
| 163 | + $argsProperty = $reflection->getProperty('args'); |
| 164 | + $argsProperty->setValue($this->handler, ['userUuid' => $uuid]); |
| 165 | + |
| 166 | + $user = $this->getMockBuilder(\stdClass::class) |
| 167 | + ->addMethods(['getEmail', 'getName']) |
| 168 | + ->getMock(); |
| 169 | + $user-> method( 'getEmail')-> willReturn( '[email protected]'); |
| 170 | + $user->method('getName')->willReturn('dotkernel'); |
| 171 | + |
| 172 | + $this->userRepository->method('find')->willReturn($user); |
| 173 | + $this->mailService->method('getMessage')->willReturn($this->createMock(Email::class)); |
| 174 | + $this->mailService->method('setSubject'); |
| 175 | + $this->renderer->method('render')->willReturn('Rendered content'); |
| 176 | + $this->mailService->method('setBody'); |
| 177 | + |
| 178 | + $this->mailService->method('send') |
| 179 | + ->willThrowException($this->createMock(TransportExceptionInterface::class)); |
| 180 | + |
| 181 | + $result = $this->handler->sendWelcomeMail(); |
| 182 | + $this->assertFalse($result); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 187 | + */ |
| 188 | + public function testInvokeWithInvalidJsonSkipsPerform(): void |
| 189 | + { |
| 190 | + $message = $this->createMock(ExampleMessage::class); |
| 191 | + $message->method('getPayload')->willReturn([ |
| 192 | + 'foo' => '{"userUuid":', |
| 193 | + ]); |
| 194 | + |
| 195 | + $this->userRepository->expects($this->never())->method('find'); |
| 196 | + $this->mailService->expects($this->never())->method('send'); |
| 197 | + |
| 198 | + $this->handler->__invoke($message); |
| 199 | + } |
| 200 | +} |
0 commit comments