1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Codeception \Module \Symfony ;
6+
7+ use PHPUnit \Framework \Constraint \LogicalNot ;
8+ use Symfony \Component \Mime \Email ;
9+ use Symfony \Component \Mime \Test \Constraint as MimeConstraint ;
10+
11+ trait MimeAssertionsTrait
12+ {
13+ /**
14+ * Verify that an email contains addresses with a [header](https://datatracker.ietf.org/doc/html/rfc4021)
15+ * `$headerName` and its expected value `$expectedValue`.
16+ * If the Email object is not specified, the last email sent is used instead.
17+ *
18+ * ```php
19+ * <?php
20+ * $I->assertEmailAddressContains('To', 'jane_doe@example.com');
21+ * ```
22+ */
23+ public function assertEmailAddressContains (string $ headerName , string $ expectedValue , Email $ email = null ): void
24+ {
25+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
26+ $ this ->assertThat ($ email , new MimeConstraint \EmailAddressContains ($ headerName , $ expectedValue ));
27+ }
28+
29+ /**
30+ * Verify that an email has sent the specified number `$count` of attachments.
31+ * If the Email object is not specified, the last email sent is used instead.
32+ *
33+ * ```php
34+ * <?php
35+ * $I->assertEmailAttachmentCount(1);
36+ * ```
37+ */
38+ public function assertEmailAttachmentCount (int $ count , Email $ email = null ): void
39+ {
40+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
41+ $ this ->assertThat ($ email , new MimeConstraint \EmailAttachmentCount ($ count ));
42+ }
43+
44+ /**
45+ * Verify that an email has a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
46+ * If the Email object is not specified, the last email sent is used instead.
47+ *
48+ * ```php
49+ * <?php
50+ * $I->assertEmailHasHeader('Bcc');
51+ * ```
52+ */
53+ public function assertEmailHasHeader (string $ headerName , Email $ email = null ): void
54+ {
55+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
56+ $ this ->assertThat ($ email , new MimeConstraint \EmailHasHeader ($ headerName ));
57+ }
58+
59+ /**
60+ * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
61+ * `$headerName` of an email is not the expected one `$expectedValue`.
62+ * If the Email object is not specified, the last email sent is used instead.
63+ *
64+ * ```php
65+ * <?php
66+ * $I->assertEmailHeaderNotSame('To', 'john_doe@gmail.com');
67+ * ```
68+ */
69+ public function assertEmailHeaderNotSame (string $ headerName , string $ expectedValue , Email $ email = null ): void
70+ {
71+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
72+ $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue )));
73+ }
74+
75+ /**
76+ * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
77+ * `$headerName` of an email is the same as expected `$expectedValue`.
78+ * If the Email object is not specified, the last email sent is used instead.
79+ *
80+ * ```php
81+ * <?php
82+ * $I->assertEmailHeaderSame('To', 'jane_doe@gmail.com');
83+ * ```
84+ */
85+ public function assertEmailHeaderSame (string $ headerName , string $ expectedValue , Email $ email = null ): void
86+ {
87+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
88+ $ this ->assertThat ($ email , new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue ));
89+ }
90+
91+ /**
92+ * Verify that the HTML body of an email contains `$text`.
93+ * If the Email object is not specified, the last email sent is used instead.
94+ *
95+ * ```php
96+ * <?php
97+ * $I->assertEmailHtmlBodyContains('Successful registration');
98+ * ```
99+ */
100+ public function assertEmailHtmlBodyContains (string $ text , Email $ email = null ): void
101+ {
102+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
103+ $ this ->assertThat ($ email , new MimeConstraint \EmailHtmlBodyContains ($ text ));
104+ }
105+
106+ /**
107+ * Verify that the HTML body of an email does not contain a text `$text`.
108+ * If the Email object is not specified, the last email sent is used instead.
109+ *
110+ * ```php
111+ * <?php
112+ * $I->assertEmailHtmlBodyNotContains('userpassword');
113+ * ```
114+ */
115+ public function assertEmailHtmlBodyNotContains (string $ text , Email $ email = null ): void
116+ {
117+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
118+ $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHtmlBodyContains ($ text )));
119+ }
120+
121+ /**
122+ * Verify that an email does not have a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
123+ * If the Email object is not specified, the last email sent is used instead.
124+ *
125+ * ```php
126+ * <?php
127+ * $I->assertEmailNotHasHeader('Bcc');
128+ * ```
129+ */
130+ public function assertEmailNotHasHeader (string $ headerName , Email $ email = null ): void
131+ {
132+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
133+ $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHasHeader ($ headerName )));
134+ }
135+
136+ /**
137+ * Verify the text body of an email contains a `$text`.
138+ * If the Email object is not specified, the last email sent is used instead.
139+ *
140+ * ```php
141+ * <?php
142+ * $I->assertEmailTextBodyContains('Example text body');
143+ * ```
144+ */
145+ public function assertEmailTextBodyContains (string $ text , Email $ email = null ): void
146+ {
147+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
148+ $ this ->assertThat ($ email , new MimeConstraint \EmailTextBodyContains ($ text ));
149+ }
150+
151+ /**
152+ * Verify that the text body of an email does not contain a `$text`.
153+ * If the Email object is not specified, the last email sent is used instead.
154+ *
155+ * ```php
156+ * <?php
157+ * $I->assertEmailTextBodyNotContains('My secret text body');
158+ * ```
159+ */
160+ public function assertEmailTextBodyNotContains (string $ text , Email $ email = null ): void
161+ {
162+ $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
163+ $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailTextBodyContains ($ text )));
164+ }
165+
166+ /**
167+ * Returns the last email sent if $email is null. If no email has been sent it fails.
168+ */
169+ private function verifyEmailObject (?Email $ email , string $ function ): Email
170+ {
171+ $ email = $ email ?: $ this ->grabLastSentEmail ();
172+ $ errorMsgFormat = "There is no email to verify. An Email object was not specified when invoking '%s' and the application has not sent one. " ;
173+ return $ email ?: $ this ->fail (
174+ sprintf ($ errorMsgFormat , $ function )
175+ );
176+ }
177+ }
0 commit comments