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
136 lines (121 loc) · 5.38 KB
/
MailerAssertionsTrait.php
File metadata and controls
136 lines (121 loc) · 5.38 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
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
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.
*/
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.
*/
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.
*/
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).
*/
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.
*/
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\Email[]
*/
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, 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));
}
protected function getMessageMailerEvents(): MessageEvents
{
if ($messageLogger = $this->getService('mailer.message_logger_listener')) {
/** @var MessageLoggerListener $messageLogger */
return $messageLogger->getEvents();
}
if ($messageLogger = $this->getService('mailer.logger_message_listener')) {
/** @var MessageLoggerListener $messageLogger */
return $messageLogger->getEvents();
}
$this->fail("Emails can't be tested without Symfony Mailer service.");
}
}