forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMimeAssertionsTrait.php
177 lines (163 loc) · 6.37 KB
/
MimeAssertionsTrait.php
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
177
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
trait MimeAssertionsTrait
{
/**
* Verify that an email contains addresses with a [header](https://datatracker.ietf.org/doc/html/rfc4021)
* `$headerName` and its expected value `$expectedValue`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailAddressContains('To', '[email protected]');
* ```
*/
public function assertEmailAddressContains(string $headerName, string $expectedValue, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue));
}
/**
* Verify that an email has sent the specified number `$count` of attachments.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailAttachmentCount(1);
* ```
*/
public function assertEmailAttachmentCount(int $count, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailAttachmentCount($count));
}
/**
* Verify that an email has a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailHasHeader('Bcc');
* ```
*/
public function assertEmailHasHeader(string $headerName, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailHasHeader($headerName));
}
/**
* Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
* `$headerName` of an email is not the expected one `$expectedValue`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailHeaderNotSame('To', '[email protected]');
* ```
*/
public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)));
}
/**
* Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
* `$headerName` of an email is the same as expected `$expectedValue`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailHeaderSame('To', '[email protected]');
* ```
*/
public function assertEmailHeaderSame(string $headerName, string $expectedValue, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue));
}
/**
* Verify that the HTML body of an email contains `$text`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailHtmlBodyContains('Successful registration');
* ```
*/
public function assertEmailHtmlBodyContains(string $text, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text));
}
/**
* Verify that the HTML body of an email does not contain a text `$text`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailHtmlBodyNotContains('userpassword');
* ```
*/
public function assertEmailHtmlBodyNotContains(string $text, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)));
}
/**
* Verify that an email does not have a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailNotHasHeader('Bcc');
* ```
*/
public function assertEmailNotHasHeader(string $headerName, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)));
}
/**
* Verify the text body of an email contains a `$text`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailTextBodyContains('Example text body');
* ```
*/
public function assertEmailTextBodyContains(string $text, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailTextBodyContains($text));
}
/**
* Verify that the text body of an email does not contain a `$text`.
* If the Email object is not specified, the last email sent is used instead.
*
* ```php
* <?php
* $I->assertEmailTextBodyNotContains('My secret text body');
* ```
*/
public function assertEmailTextBodyNotContains(string $text, ?Email $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)));
}
/**
* Returns the last email sent if $email is null. If no email has been sent it fails.
*/
private function verifyEmailObject(?Email $email, string $function): Email
{
$email = $email ?: $this->grabLastSentEmail();
$errorMsgTemplate = "There is no email to verify. An Email object was not specified when invoking '%s' and the application has not sent one.";
return $email ?: $this->fail(
sprintf($errorMsgTemplate, $function)
);
}
}