Skip to content

Commit 5474ddb

Browse files
authored
Improve Mailable assertion error messages with expected vs actual values (#56221)
* Improve mailable assertion error messages with expected vs actual values * wip * retry tests
1 parent 4c7e519 commit 5474ddb

File tree

2 files changed

+62
-26
lines changed

2 files changed

+62
-26
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,12 @@ public function assertFrom($address, $name = null)
12191219
{
12201220
$this->renderForAssertions();
12211221

1222-
$recipient = $this->formatAssertionRecipient($address, $name);
1222+
$expected = $this->formatAssertionRecipient($address, $name);
1223+
$actual = $this->formatActualRecipients($this->from);
12231224

12241225
PHPUnit::assertTrue(
12251226
$this->hasFrom($address, $name),
1226-
"Email was not from expected address [{$recipient}]."
1227+
"Email was not from expected address.\nExpected: [{$expected}]\nActual: [{$actual}]"
12271228
);
12281229

12291230
return $this;
@@ -1240,11 +1241,12 @@ public function assertTo($address, $name = null)
12401241
{
12411242
$this->renderForAssertions();
12421243

1243-
$recipient = $this->formatAssertionRecipient($address, $name);
1244+
$expected = $this->formatAssertionRecipient($address, $name);
1245+
$actual = $this->formatActualRecipients($this->to);
12441246

12451247
PHPUnit::assertTrue(
12461248
$this->hasTo($address, $name),
1247-
"Did not see expected recipient [{$recipient}] in email 'to' recipients."
1249+
"Did not see expected recipient in email 'to' recipients.\nExpected: [{$expected}]\nActual: [{$actual}]"
12481250
);
12491251

12501252
return $this;
@@ -1273,11 +1275,12 @@ public function assertHasCc($address, $name = null)
12731275
{
12741276
$this->renderForAssertions();
12751277

1276-
$recipient = $this->formatAssertionRecipient($address, $name);
1278+
$expected = $this->formatAssertionRecipient($address, $name);
1279+
$actual = $this->formatActualRecipients($this->cc);
12771280

12781281
PHPUnit::assertTrue(
12791282
$this->hasCc($address, $name),
1280-
"Did not see expected recipient [{$recipient}] in email 'cc' recipients."
1283+
"Did not see expected recipient in email 'cc' recipients.\nExpected: [{$expected}]\nActual: [{$actual}]"
12811284
);
12821285

12831286
return $this;
@@ -1294,11 +1297,12 @@ public function assertHasBcc($address, $name = null)
12941297
{
12951298
$this->renderForAssertions();
12961299

1297-
$recipient = $this->formatAssertionRecipient($address, $name);
1300+
$expected = $this->formatAssertionRecipient($address, $name);
1301+
$actual = $this->formatActualRecipients($this->bcc);
12981302

12991303
PHPUnit::assertTrue(
13001304
$this->hasBcc($address, $name),
1301-
"Did not see expected recipient [{$recipient}] in email 'bcc' recipients."
1305+
"Did not see expected recipient in email 'bcc' recipients.\nExpected: [{$expected}]\nActual: [{$actual}]"
13021306
);
13031307

13041308
return $this;
@@ -1315,11 +1319,12 @@ public function assertHasReplyTo($address, $name = null)
13151319
{
13161320
$this->renderForAssertions();
13171321

1318-
$replyTo = $this->formatAssertionRecipient($address, $name);
1322+
$expected = $this->formatAssertionRecipient($address, $name);
1323+
$actual = $this->formatActualRecipients($this->replyTo);
13191324

13201325
PHPUnit::assertTrue(
13211326
$this->hasReplyTo($address, $name),
1322-
"Did not see expected address [{$replyTo}] as email 'reply to' recipient."
1327+
"Did not see expected address as email 'reply to' recipient.\nExpected: [{$expected}]\nActual: [{$actual}]"
13231328
);
13241329

13251330
return $this;
@@ -1345,6 +1350,28 @@ private function formatAssertionRecipient($address, $name = null)
13451350
return $address;
13461351
}
13471352

1353+
/**
1354+
* Format actual recipients for display in assertion messages.
1355+
*
1356+
* @param array $recipients
1357+
* @return string
1358+
*/
1359+
private function formatActualRecipients($recipients)
1360+
{
1361+
if (empty($recipients)) {
1362+
return 'none';
1363+
}
1364+
1365+
return (new Collection($recipients))->map(function ($recipient) {
1366+
$formatted = $recipient['address'];
1367+
if (! empty($recipient['name'])) {
1368+
$formatted .= ' ('.$recipient['name'].')';
1369+
}
1370+
1371+
return $formatted;
1372+
})->implode(', ');
1373+
}
1374+
13481375
/**
13491376
* Assert that the mailable has the given subject.
13501377
*
@@ -1355,9 +1382,11 @@ public function assertHasSubject($subject)
13551382
{
13561383
$this->renderForAssertions();
13571384

1385+
$actualSubject = $this->subject ?: (method_exists($this, 'envelope') ? $this->envelope()->subject : null) ?: Str::title(Str::snake(class_basename($this), ' '));
1386+
13581387
PHPUnit::assertTrue(
13591388
$this->hasSubject($subject),
1360-
"Did not see expected text [{$subject}] in email subject."
1389+
"Email subject does not match expected value.\nExpected: [{$subject}]\nActual: [{$actualSubject}]"
13611390
);
13621391

13631392
return $this;
@@ -1570,9 +1599,12 @@ public function assertHasTag($tag)
15701599
{
15711600
$this->renderForAssertions();
15721601

1602+
$actualTags = method_exists($this, 'envelope') ? array_merge($this->tags, $this->envelope()->tags) : $this->tags;
1603+
$actualTagsString = empty($actualTags) ? 'none' : implode(', ', $actualTags);
1604+
15731605
PHPUnit::assertTrue(
15741606
$this->hasTag($tag),
1575-
"Did not see expected tag [{$tag}] in email tags."
1607+
"Did not see expected tag in email tags.\nExpected: [{$tag}]\nActual: [{$actualTagsString}]"
15761608
);
15771609

15781610
return $this;
@@ -1589,9 +1621,13 @@ public function assertHasMetadata($key, $value)
15891621
{
15901622
$this->renderForAssertions();
15911623

1624+
$actualMetadata = method_exists($this, 'envelope') ? array_merge($this->metadata, $this->envelope()->metadata) : $this->metadata;
1625+
$actualValue = $actualMetadata[$key] ?? null;
1626+
$actualString = $actualValue !== null ? "[{$key}] => [{$actualValue}]" : "key [{$key}] not found";
1627+
15921628
PHPUnit::assertTrue(
15931629
$this->hasMetadata($key, $value),
1594-
"Did not see expected key [{$key}] and value [{$value}] in email metadata."
1630+
"Email metadata does not match expected value.\nExpected: [{$key}] => [{$value}]\nActual: {$actualString}"
15951631
);
15961632

15971633
return $this;

tests/Mail/MailMailableTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testMailableSetsRecipientsCorrectly()
5555
$mailable->assertHasTo('[email protected]', 'Taylor Otwell');
5656
$this->fail();
5757
} catch (AssertionFailedError $e) {
58-
$this->assertSame("Did not see expected recipient [[email protected] (Taylor Otwell)] in email 'to' recipients.\nFailed asserting that false is true.", $e->getMessage());
58+
$this->assertSame("Did not see expected recipient in email 'to' recipients.\nExpected: [[email protected] (Taylor Otwell)]\nActual: [[email protected]]\nFailed asserting that false is true.", $e->getMessage());
5959
}
6060

6161
$mailable = new WelcomeMailableStub;
@@ -101,7 +101,7 @@ public function testMailableSetsRecipientsCorrectly()
101101
if (! is_string($address)) {
102102
$address = json_encode($address);
103103
}
104-
$this->assertSame("Did not see expected recipient [{$address}] in email 'to' recipients.\nFailed asserting that false is true.", $e->getMessage());
104+
$this->assertSame("Did not see expected recipient in email 'to' recipients.\nExpected: [{$address}]\nActual: [none]\nFailed asserting that false is true.", $e->getMessage());
105105
}
106106
}
107107
}
@@ -134,7 +134,7 @@ public function testMailableSetsCcRecipientsCorrectly()
134134
$mailable->assertHasCc('[email protected]', 'Taylor Otwell');
135135
$this->fail();
136136
} catch (AssertionFailedError $e) {
137-
$this->assertSame("Did not see expected recipient [[email protected] (Taylor Otwell)] in email 'cc' recipients.\nFailed asserting that false is true.", $e->getMessage());
137+
$this->assertSame("Did not see expected recipient in email 'cc' recipients.\nExpected: [[email protected] (Taylor Otwell)]\nActual: [[email protected]]\nFailed asserting that false is true.", $e->getMessage());
138138
}
139139

140140
$mailable = new WelcomeMailableStub;
@@ -192,7 +192,7 @@ public function testMailableSetsCcRecipientsCorrectly()
192192
if (! is_string($address)) {
193193
$address = json_encode($address);
194194
}
195-
$this->assertSame("Did not see expected recipient [{$address}] in email 'cc' recipients.\nFailed asserting that false is true.", $e->getMessage());
195+
$this->assertSame("Did not see expected recipient in email 'cc' recipients.\nExpected: [{$address}]\nActual: [none]\nFailed asserting that false is true.", $e->getMessage());
196196
}
197197
}
198198
}
@@ -225,7 +225,7 @@ public function testMailableSetsBccRecipientsCorrectly()
225225
$mailable->assertHasBcc('[email protected]', 'Taylor Otwell');
226226
$this->fail();
227227
} catch (AssertionFailedError $e) {
228-
$this->assertSame("Did not see expected recipient [[email protected] (Taylor Otwell)] in email 'bcc' recipients.\nFailed asserting that false is true.", $e->getMessage());
228+
$this->assertSame("Did not see expected recipient in email 'bcc' recipients.\nExpected: [[email protected] (Taylor Otwell)]\nActual: [[email protected]]\nFailed asserting that false is true.", $e->getMessage());
229229
}
230230

231231
$mailable = new WelcomeMailableStub;
@@ -283,7 +283,7 @@ public function testMailableSetsBccRecipientsCorrectly()
283283
if (! is_string($address)) {
284284
$address = json_encode($address);
285285
}
286-
$this->assertSame("Did not see expected recipient [{$address}] in email 'bcc' recipients.\nFailed asserting that false is true.", $e->getMessage());
286+
$this->assertSame("Did not see expected recipient in email 'bcc' recipients.\nExpected: [{$address}]\nActual: [none]\nFailed asserting that false is true.", $e->getMessage());
287287
}
288288
}
289289
}
@@ -316,7 +316,7 @@ public function testMailableSetsReplyToCorrectly()
316316
$mailable->assertHasReplyTo('[email protected]', 'Taylor Otwell');
317317
$this->fail();
318318
} catch (AssertionFailedError $e) {
319-
$this->assertSame("Did not see expected address [[email protected] (Taylor Otwell)] as email 'reply to' recipient.\nFailed asserting that false is true.", $e->getMessage());
319+
$this->assertSame("Did not see expected address as email 'reply to' recipient.\nExpected: [[email protected] (Taylor Otwell)]\nActual: [[email protected]]\nFailed asserting that false is true.", $e->getMessage());
320320
}
321321

322322
$mailable = new WelcomeMailableStub;
@@ -363,7 +363,7 @@ public function testMailableSetsReplyToCorrectly()
363363
if (! is_string($address)) {
364364
$address = json_encode($address);
365365
}
366-
$this->assertSame("Did not see expected address [{$address}] as email 'reply to' recipient.\nFailed asserting that false is true.", $e->getMessage());
366+
$this->assertSame("Did not see expected address as email 'reply to' recipient.\nExpected: [{$address}]\nActual: [none]\nFailed asserting that false is true.", $e->getMessage());
367367
}
368368
}
369369
}
@@ -396,7 +396,7 @@ public function testMailableSetsFromCorrectly()
396396
$mailable->assertFrom('[email protected]', 'Taylor Otwell');
397397
$this->fail();
398398
} catch (AssertionFailedError $e) {
399-
$this->assertSame("Email was not from expected address [[email protected] (Taylor Otwell)].\nFailed asserting that false is true.", $e->getMessage());
399+
$this->assertSame("Email was not from expected address.\nExpected: [[email protected] (Taylor Otwell)]\nActual: [[email protected]]\nFailed asserting that false is true.", $e->getMessage());
400400
}
401401

402402
$mailable = new WelcomeMailableStub;
@@ -443,7 +443,7 @@ public function testMailableSetsFromCorrectly()
443443
if (! is_string($address)) {
444444
$address = json_encode($address);
445445
}
446-
$this->assertSame("Email was not from expected address [{$address}].\nFailed asserting that false is true.", $e->getMessage());
446+
$this->assertSame("Email was not from expected address.\nExpected: [{$address}]\nActual: [none]\nFailed asserting that false is true.", $e->getMessage());
447447
}
448448
}
449449
}
@@ -629,7 +629,7 @@ public function testMailableMetadataGetsSent()
629629
$mailable->assertHasMetadata('test', 'test');
630630
$this->fail();
631631
} catch (AssertionFailedError $e) {
632-
$this->assertSame("Did not see expected key [test] and value [test] in email metadata.\nFailed asserting that false is true.", $e->getMessage());
632+
$this->assertSame("Email metadata does not match expected value.\nExpected: [test] => [test]\nActual: key [test] not found\nFailed asserting that false is true.", $e->getMessage());
633633
}
634634
}
635635

@@ -664,7 +664,7 @@ public function testMailableTagGetsSent()
664664
$mailable->assertHasTag('bar');
665665
$this->fail();
666666
} catch (AssertionFailedError $e) {
667-
$this->assertSame("Did not see expected tag [bar] in email tags.\nFailed asserting that false is true.", $e->getMessage());
667+
$this->assertSame("Did not see expected tag in email tags.\nExpected: [bar]\nActual: [test, foo]\nFailed asserting that false is true.", $e->getMessage());
668668
}
669669
}
670670

@@ -1088,7 +1088,7 @@ public function build()
10881088
$mailable->assertHasSubject('Foo Subject');
10891089
$this->fail();
10901090
} catch (AssertionFailedError $e) {
1091-
$this->assertSame("Did not see expected text [Foo Subject] in email subject.\nFailed asserting that false is true.", $e->getMessage());
1091+
$this->assertStringContainsString("Email subject does not match expected value.\nExpected: [Foo Subject]\nActual:", $e->getMessage());
10921092
}
10931093

10941094
$mailable = new class() extends Mailable

0 commit comments

Comments
 (0)