Skip to content

Commit ebe072a

Browse files
inxilprodriesvints
andauthored
[8.x] Allow for closure reflection on all MailFake assertions (#38328)
* [8.x] Allow for closure reflection on all MailFake assertions * Minor refactor * Update src/Illuminate/Support/Testing/Fakes/MailFake.php Co-authored-by: Dries Vints <[email protected]>
1 parent bc1188d commit ebe072a

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/Illuminate/Support/Testing/Fakes/MailFake.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class MailFake implements Factory, Mailer, MailQueue
4545
*/
4646
public function assertSent($mailable, $callback = null)
4747
{
48-
if ($mailable instanceof Closure) {
49-
[$mailable, $callback] = [$this->firstClosureParameterType($mailable), $mailable];
50-
}
48+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
5149

5250
if (is_numeric($callback)) {
5351
return $this->assertSentTimes($mailable, $callback);
@@ -85,12 +83,14 @@ protected function assertSentTimes($mailable, $times = 1)
8583
/**
8684
* Determine if a mailable was not sent based on a truth-test callback.
8785
*
88-
* @param string $mailable
86+
* @param string|\Closure $mailable
8987
* @param callable|null $callback
9088
* @return void
9189
*/
9290
public function assertNotSent($mailable, $callback = null)
9391
{
92+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
93+
9494
PHPUnit::assertCount(
9595
0, $this->sent($mailable, $callback),
9696
"The unexpected [{$mailable}] mailable was sent."
@@ -120,9 +120,7 @@ public function assertNothingSent()
120120
*/
121121
public function assertQueued($mailable, $callback = null)
122122
{
123-
if ($mailable instanceof Closure) {
124-
[$mailable, $callback] = [$this->firstClosureParameterType($mailable), $mailable];
125-
}
123+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
126124

127125
if (is_numeric($callback)) {
128126
return $this->assertQueuedTimes($mailable, $callback);
@@ -154,12 +152,14 @@ protected function assertQueuedTimes($mailable, $times = 1)
154152
/**
155153
* Determine if a mailable was not queued based on a truth-test callback.
156154
*
157-
* @param string $mailable
155+
* @param string|\Closure $mailable
158156
* @param callable|null $callback
159157
* @return void
160158
*/
161159
public function assertNotQueued($mailable, $callback = null)
162160
{
161+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
162+
163163
PHPUnit::assertCount(
164164
0, $this->queued($mailable, $callback),
165165
"The unexpected [{$mailable}] mailable was queued."
@@ -183,12 +183,14 @@ public function assertNothingQueued()
183183
/**
184184
* Get all of the mailables matching a truth-test callback.
185185
*
186-
* @param string $mailable
186+
* @param string|\Closure $mailable
187187
* @param callable|null $callback
188188
* @return \Illuminate\Support\Collection
189189
*/
190190
public function sent($mailable, $callback = null)
191191
{
192+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
193+
192194
if (! $this->hasSent($mailable)) {
193195
return collect();
194196
}
@@ -216,12 +218,14 @@ public function hasSent($mailable)
216218
/**
217219
* Get all of the queued mailables matching a truth-test callback.
218220
*
219-
* @param string $mailable
221+
* @param string|\Closure $mailable
220222
* @param callable|null $callback
221223
* @return \Illuminate\Support\Collection
222224
*/
223225
public function queued($mailable, $callback = null)
224226
{
227+
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
228+
225229
if (! $this->hasQueued($mailable)) {
226230
return collect();
227231
}
@@ -386,4 +390,20 @@ public function failures()
386390
{
387391
return [];
388392
}
393+
394+
/**
395+
* Infer mailable class using reflection if a typehinted closure is passed to assertion.
396+
*
397+
* @param string|\Closure $mailable
398+
* @param callable|null $callback
399+
* @return array
400+
*/
401+
protected function prepareMailableAndCallback($mailable, $callback)
402+
{
403+
if ($mailable instanceof Closure) {
404+
return [$this->firstClosureParameterType($mailable), $mailable];
405+
}
406+
407+
return [$mailable, $callback];
408+
}
389409
}

tests/Support/SupportTestingMailFakeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public function testAssertNotSent()
6969
}
7070
}
7171

72+
public function testAssertNotSentWithClosure()
73+
{
74+
$callback = function (MailableStub $mail) {
75+
return $mail->hasTo('[email protected]');
76+
};
77+
78+
$this->fake->assertNotSent($callback);
79+
80+
$this->fake->to('[email protected]')->send($this->mailable);
81+
82+
$this->expectException(ExpectationFailedException::class);
83+
$this->expectExceptionMessageMatches('/The unexpected \['.preg_quote(MailableStub::class, '/').'\] mailable was sent./m');
84+
85+
$this->fake->assertNotSent($callback);
86+
}
87+
7288
public function testAssertSentTimes()
7389
{
7490
$this->fake->to('[email protected]')->send($this->mailable);

0 commit comments

Comments
 (0)