|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Application\DBAL\Types\MessageTypeType; |
|
8
|
|
|
use Application\Model\Message; |
|
9
|
|
|
use Application\Model\User; |
|
10
|
|
|
use Application\Service\Mailer; |
|
11
|
|
|
use Doctrine\ORM\EntityManager; |
|
12
|
|
|
use Prophecy\Argument; |
|
13
|
|
|
use Zend\Mail\Transport\InMemory; |
|
14
|
|
|
use Zend\View\Renderer\RendererInterface; |
|
15
|
|
|
|
|
16
|
|
|
class MailerTest extends \PHPUnit\Framework\TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private function createMockMailer(): Mailer |
|
19
|
|
|
{ |
|
20
|
|
|
global $container; |
|
21
|
|
|
|
|
22
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
|
23
|
|
|
$renderer = $container->get(RendererInterface::class); |
|
24
|
|
|
$transport = new InMemory(); |
|
25
|
|
|
$config = $container->get('config'); |
|
26
|
|
|
|
|
27
|
|
|
$mailer = new Mailer($entityManager, $transport, $renderer, $config['hostname'], $config['emailOverride'] ?? null); |
|
28
|
|
|
|
|
29
|
|
|
return $mailer; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testQueueResetPassword(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$user = $this->createMockUser(); |
|
35
|
|
|
$mailer = $this->createMockMailer(); |
|
36
|
|
|
$message = $mailer->queueResetPassword($user, '[email protected]'); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertMessage($message, $user, '[email protected]', MessageTypeType::RESET_PASSWORD, 'Demande de modification de mot de passe'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testSendMessage(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$mailer = $this->createMockMailer(); |
|
44
|
|
|
$message = new Message(); |
|
45
|
|
|
$message->setEmail('[email protected]'); |
|
46
|
|
|
|
|
47
|
|
|
$this->expectOutputRegex('~email sent to: john\.doe@example\.com~'); |
|
48
|
|
|
$mailer->sendMessage($message); |
|
49
|
|
|
self::assertNotNull($message->getDateSent()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function createMockUser(): User |
|
53
|
|
|
{ |
|
54
|
|
|
$prophecy = $this->prophesize(User::class); |
|
55
|
|
|
$prophecy->getLogin()->willReturn('john.doe'); |
|
56
|
|
|
$prophecy->getFirstName()->willReturn('John'); |
|
57
|
|
|
$prophecy->getLastName()->willReturn('Doe'); |
|
58
|
|
|
$prophecy->getEmail()->willReturn('[email protected]'); |
|
59
|
|
|
$prophecy->createToken()->willReturn(str_repeat('X', 32)); |
|
60
|
|
|
$prophecy->messageAdded(Argument::type(Message::class)); |
|
61
|
|
|
$user = $prophecy->reveal(); |
|
62
|
|
|
|
|
63
|
|
|
return $user; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function assertMessage(Message $message, ?User $user, string $email, string $type, string $subject): void |
|
67
|
|
|
{ |
|
68
|
|
|
self::assertSame($type, $message->getType()); |
|
69
|
|
|
self::assertSame($email, $message->getEmail()); |
|
70
|
|
|
self::assertSame($user, $message->getRecipient()); |
|
71
|
|
|
self::assertNull($message->getDateSent()); |
|
72
|
|
|
self::assertSame($subject, $message->getSubject()); |
|
73
|
|
|
|
|
74
|
|
|
$expectedBody = 'tests/data/emails/' . str_replace('_', '-', $type) . '.html'; |
|
75
|
|
|
$this->assertFile($expectedBody, $message->getBody()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Custom assert that will not produce gigantic diff |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $file |
|
82
|
|
|
* @param string $actual |
|
83
|
|
|
*/ |
|
84
|
|
|
private function assertFile(string $file, string $actual): void |
|
85
|
|
|
{ |
|
86
|
|
|
// Log actual result for easier comparison with external diff tools |
|
87
|
|
|
$logFile = 'logs/' . $file; |
|
88
|
|
|
$dir = dirname($logFile); |
|
89
|
|
|
@mkdir($dir, 0777, true); |
|
|
|
|
|
|
90
|
|
|
file_put_contents($logFile, $actual); |
|
91
|
|
|
|
|
92
|
|
|
self::assertFileExists($file, 'Expected file must exist on disk, fix it with: cp ' . $logFile . ' ' . $file); |
|
93
|
|
|
$expected = file_get_contents($file); |
|
94
|
|
|
|
|
95
|
|
|
self::assertTrue($expected === $actual, 'File content does not match, compare with: meld ' . $file . ' ' . $logFile); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: