forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailerAssertionsTrait.php
More file actions
176 lines (162 loc) · 6.26 KB
/
MailerAssertionsTrait.php
File metadata and controls
176 lines (162 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
use Symfony\Component\Mime\Email;
trait MailerAssertionsTrait
{
/**
* Asserts that the expected number of emails was sent.
*
* ```php
* <?php
* $I->assertEmailCount(2, 'smtp');
* ```
*/
public function assertEmailCount(int $count, ?string $transport = null, string $message = ''): void
{
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
}
/**
* Asserts that the given mailer event is not queued.
* Use `getMailerEvent(int $index = 0, ?string $transport = null)` to retrieve a mailer event by index.
*
* ```php
* <?php
* $event = $I->getMailerEvent();
* $I->assertEmailIsNotQueued($event);
* ```
*/
public function assertEmailIsNotQueued(MessageEvent $event, string $message = ''): void
{
$this->assertThat($event, new LogicalNot(new MailerConstraint\EmailIsQueued()), $message);
}
/**
* Asserts that the given mailer event is queued.
* Use `getMailerEvent(int $index = 0, ?string $transport = null)` to retrieve a mailer event by index.
*
* ```php
* <?php
* $event = $I->getMailerEvent();
* $I->assertEmailIsQueued($event);
* ```
*/
public function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
{
$this->assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
}
/**
* Asserts that the expected number of emails was queued (e.g. using the Messenger component).
*
* ```php
* <?php
* $I->assertQueuedEmailCount(1, 'smtp');
* ```
*/
public function assertQueuedEmailCount(int $count, ?string $transport = null, string $message = ''): void
{
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport, true), $message);
}
/**
* Checks that no email was sent.
* The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first; otherwise this check will *always* pass.
*
* ```php
* <?php
* $I->dontSeeEmailIsSent();
* ```
*/
public function dontSeeEmailIsSent(): void
{
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount(0));
}
/**
* Returns the last sent email.
* The function is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
* See also: [grabSentEmails()](https://codeception.com/docs/modules/Symfony#grabSentEmails)
*
* ```php
* <?php
* $email = $I->grabLastSentEmail();
* $address = $email->getTo()[0];
* $I->assertSame('john_doe@example.com', $address->getAddress());
* ```
*/
public function grabLastSentEmail(): ?Email
{
/** @var Email[] $emails */
$emails = $this->getMessageMailerEvents()->getMessages();
$lastEmail = end($emails);
return $lastEmail ?: null;
}
/**
* Returns an array of all sent emails.
* The function is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
* See also: [grabLastSentEmail()](https://codeception.com/docs/modules/Symfony#grabLastSentEmail)
*
* ```php
* <?php
* $emails = $I->grabSentEmails();
* ```
*
* @return \Symfony\Component\Mime\RawMessage[]
*/
public function grabSentEmails(): array
{
return $this->getMessageMailerEvents()->getMessages();
}
/**
* Checks if the given number of emails was sent (default `$expectedCount`: 1).
* The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means:
* If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first.
*
* Limitation:
* If your mail is sent in a Symfony console command and you start that command in your test with [$I->runShellCommand()](https://codeception.com/docs/modules/Cli#runShellCommand),
* Codeception will not notice it.
* As a more professional alternative, we recommend Mailpit (see [Addons](https://codeception.com/addons)), which also lets you test the content of the mail.
*
* ```php
* <?php
* $I->seeEmailIsSent(2);
* ```
*
* @param int $expectedCount The expected number of emails sent
*/
public function seeEmailIsSent(int $expectedCount = 1): void
{
$this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($expectedCount));
}
/**
* Returns the mailer event at the specified index.
*
* ```php
* <?php
* $event = $I->getMailerEvent();
* ```
*/
public function getMailerEvent(int $index = 0, ?string $transport = null): ?MessageEvent
{
$mailerEvents = $this->getMessageMailerEvents();
$events = $mailerEvents->getEvents($transport);
return $events[$index] ?? null;
}
protected function getMessageMailerEvents(): MessageEvents
{
$services = ['mailer.message_logger_listener', 'mailer.logger_message_listener'];
foreach ($services as $serviceId) {
$mailer = $this->getService($serviceId);
if ($mailer instanceof MessageLoggerListener) {
return $mailer->getEvents();
}
}
Assert::fail("Emails can't be tested without Symfony Mailer service.");
}
}