1616trait MimeAssertionsTrait
1717{
1818 /**
19- * Verify that an email contains addresses with a [header](https://datatracker.ietf.org/doc/html/rfc4021)
19+ * Verify that a message contains addresses with a [header](https://datatracker.ietf.org/doc/html/rfc4021)
2020 * `$headerName` and its expected value `$expectedValue`.
21- * If the Email object is not specified, the last email sent is used instead.
21+ * If no Message is specified, the last sent message is used instead.
2222 *
2323 * ```php
2424 * <?php
@@ -27,13 +27,12 @@ trait MimeAssertionsTrait
2727 */
2828 public function assertEmailAddressContains (string $ headerName , string $ expectedValue , ?Message $ email = null ): void
2929 {
30- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
31- $ this ->assertThat ($ email , new MimeConstraint \EmailAddressContains ($ headerName , $ expectedValue ));
30+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailAddressContains ($ headerName , $ expectedValue ));
3231 }
3332
3433 /**
35- * Verify that an email has sent the specified number `$count` of attachments.
36- * If the Email object is not specified, the last email sent is used instead.
34+ * Verify that an email has the specified number `$count` of attachments.
35+ * If no Email is specified, the last sent email is used instead.
3736 *
3837 * ```php
3938 * <?php
@@ -42,13 +41,12 @@ public function assertEmailAddressContains(string $headerName, string $expectedV
4241 */
4342 public function assertEmailAttachmentCount (int $ count , ?Email $ email = null ): void
4443 {
45- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
46- $ this ->assertThat ($ email , new MimeConstraint \EmailAttachmentCount ($ count ));
44+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailAttachmentCount ($ count ));
4745 }
4846
4947 /**
50- * Verify that an email has a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
51- * If the Email object is not specified, the last email sent is used instead.
48+ * Verify that a message has a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
49+ * If no Message is specified, the last sent message is used instead.
5250 *
5351 * ```php
5452 * <?php
@@ -57,14 +55,13 @@ public function assertEmailAttachmentCount(int $count, ?Email $email = null): vo
5755 */
5856 public function assertEmailHasHeader (string $ headerName , ?Message $ email = null ): void
5957 {
60- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
61- $ this ->assertThat ($ email , new MimeConstraint \EmailHasHeader ($ headerName ));
58+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailHasHeader ($ headerName ));
6259 }
6360
6461 /**
6562 * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
66- * `$headerName` of an email is not the expected one `$expectedValue`.
67- * If the Email object is not specified, the last email sent is used instead.
63+ * `$headerName` of a message is not the expected one `$expectedValue`.
64+ * If no Message is specified, the last sent message is used instead.
6865 *
6966 * ```php
7067 * <?php
@@ -73,14 +70,13 @@ public function assertEmailHasHeader(string $headerName, ?Message $email = null)
7370 */
7471 public function assertEmailHeaderNotSame (string $ headerName , string $ expectedValue , ?Message $ email = null ): void
7572 {
76- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
77- $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue )));
73+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new LogicalNot (new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue )));
7874 }
7975
8076 /**
8177 * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021)
82- * `$headerName` of an email is the same as expected `$expectedValue`.
83- * If the Email object is not specified, the last email sent is used instead.
78+ * `$headerName` of a message is the same as expected `$expectedValue`.
79+ * If no Message is specified, the last sent message is used instead.
8480 *
8581 * ```php
8682 * <?php
@@ -89,13 +85,12 @@ public function assertEmailHeaderNotSame(string $headerName, string $expectedVal
8985 */
9086 public function assertEmailHeaderSame (string $ headerName , string $ expectedValue , ?Message $ email = null ): void
9187 {
92- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
93- $ this ->assertThat ($ email , new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue ));
88+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailHeaderSame ($ headerName , $ expectedValue ));
9489 }
9590
9691 /**
9792 * Verify that the HTML body of an email contains `$text`.
98- * If the Email object is not specified, the last email sent is used instead.
93+ * If no Email is specified, the last sent email is used instead.
9994 *
10095 * ```php
10196 * <?php
@@ -104,13 +99,12 @@ public function assertEmailHeaderSame(string $headerName, string $expectedValue,
10499 */
105100 public function assertEmailHtmlBodyContains (string $ text , ?Email $ email = null ): void
106101 {
107- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
108- $ this ->assertThat ($ email , new MimeConstraint \EmailHtmlBodyContains ($ text ));
102+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailHtmlBodyContains ($ text ));
109103 }
110104
111105 /**
112106 * Verify that the HTML body of an email does not contain a text `$text`.
113- * If the Email object is not specified, the last email sent is used instead.
107+ * If no Email is specified, the last sent email is used instead.
114108 *
115109 * ```php
116110 * <?php
@@ -119,13 +113,12 @@ public function assertEmailHtmlBodyContains(string $text, ?Email $email = null):
119113 */
120114 public function assertEmailHtmlBodyNotContains (string $ text , ?Email $ email = null ): void
121115 {
122- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
123- $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHtmlBodyContains ($ text )));
116+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new LogicalNot (new MimeConstraint \EmailHtmlBodyContains ($ text )));
124117 }
125118
126119 /**
127- * Verify that an email does not have a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
128- * If the Email object is not specified, the last email sent is used instead.
120+ * Verify that a message does not have a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`.
121+ * If no Message is specified, the last sent message is used instead.
129122 *
130123 * ```php
131124 * <?php
@@ -134,13 +127,12 @@ public function assertEmailHtmlBodyNotContains(string $text, ?Email $email = nul
134127 */
135128 public function assertEmailNotHasHeader (string $ headerName , ?Message $ email = null ): void
136129 {
137- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
138- $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailHasHeader ($ headerName )));
130+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new LogicalNot (new MimeConstraint \EmailHasHeader ($ headerName )));
139131 }
140132
141133 /**
142134 * Verify the text body of an email contains a `$text`.
143- * If the Email object is not specified, the last email sent is used instead.
135+ * If no Email is specified, the last sent email is used instead.
144136 *
145137 * ```php
146138 * <?php
@@ -149,13 +141,12 @@ public function assertEmailNotHasHeader(string $headerName, ?Message $email = nu
149141 */
150142 public function assertEmailTextBodyContains (string $ text , ?Email $ email = null ): void
151143 {
152- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
153- $ this ->assertThat ($ email , new MimeConstraint \EmailTextBodyContains ($ text ));
144+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new MimeConstraint \EmailTextBodyContains ($ text ));
154145 }
155146
156147 /**
157148 * Verify that the text body of an email does not contain a `$text`.
158- * If the Email object is not specified, the last email sent is used instead.
149+ * If no Email is specified, the last sent email is used instead.
159150 *
160151 * ```php
161152 * <?php
@@ -164,19 +155,23 @@ public function assertEmailTextBodyContains(string $text, ?Email $email = null):
164155 */
165156 public function assertEmailTextBodyNotContains (string $ text , ?Email $ email = null ): void
166157 {
167- $ email = $ this ->verifyEmailObject ($ email , __FUNCTION__ );
168- $ this ->assertThat ($ email , new LogicalNot (new MimeConstraint \EmailTextBodyContains ($ text )));
158+ $ this ->assertThat ($ this ->getMessageOrFail ($ email , __FUNCTION__ ), new LogicalNot (new MimeConstraint \EmailTextBodyContains ($ text )));
169159 }
170160
171161 /**
172- * Returns the last email sent if $email is null. If no email has been sent it fails.
162+ * Resolves a Message for assertion or fails the test.
163+ *
164+ * Uses the provided `$message` or retrieves the last sent message.
165+ * Fails if no message is found, or if it is a plain RawMessage lacking the headers and structure required by Mime constraints.
173166 */
174- private function verifyEmailObject (? RawMessage $ email , string $ function ): RawMessage
167+ private function getMessageOrFail (? Message $ message , string $ caller ): Message
175168 {
176- $ email = $ email ?: $ this ->grabLastSentEmail ();
177- $ errorMsgTemplate = "There is no email to verify. An Email object was not specified when invoking '%s' and the application has not sent one. " ;
178- return $ email ?? Assert::fail (
179- sprintf ($ errorMsgTemplate , $ function )
180- );
169+ $ message ??= $ this ->grabLastSentRawMessage ();
170+
171+ if (!$ message instanceof Message) {
172+ Assert::fail (sprintf ("No message to verify for '%s'. None was provided or sent by the application. " , $ caller ));
173+ }
174+
175+ return $ message ;
181176 }
182177}
0 commit comments