Skip to content

Commit 07c7850

Browse files
Assertions for counting outgoing mailables (#47655)
* Assertions for counting outgoing mailables * Update MailFake.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9130f8f commit 07c7850

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,56 @@ public function assertNothingQueued()
224224
PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames);
225225
}
226226

227+
/**
228+
* Assert the total number of mailables that were sent.
229+
*
230+
* @param int $count
231+
* @return void
232+
*/
233+
public function assertSentCount($count)
234+
{
235+
$total = collect($this->mailables)->count();
236+
237+
PHPUnit::assertSame(
238+
$count, $total,
239+
"The total number of mailables sent was {$total} instead of {$count}."
240+
);
241+
}
242+
243+
/**
244+
* Assert the total number of mailables that were queued.
245+
*
246+
* @param int $count
247+
* @return void
248+
*/
249+
public function assertQueuedCount($count)
250+
{
251+
$total = collect($this->queuedMailables)->count();
252+
253+
PHPUnit::assertSame(
254+
$count, $total,
255+
"The total number of mailables queued was {$total} instead of {$count}."
256+
);
257+
}
258+
259+
/**
260+
* Assert the total number of mailables that were sent or queued.
261+
*
262+
* @param int $count
263+
* @return void
264+
*/
265+
public function assertOutgoingCount($count)
266+
{
267+
$total = collect($this->mailables)
268+
->concat($this->queuedMailables)
269+
->count();
270+
271+
PHPUnit::assertSame(
272+
$count, $total,
273+
"The total number of outgoing mailables was {$total} instead of {$count}."
274+
);
275+
}
276+
227277
/**
228278
* Get all of the mailables matching a truth-test callback.
229279
*

tests/Support/SupportTestingMailFakeTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ public function testAssertSentTimes()
133133
$this->fake->assertSent(MailableStub::class, 2);
134134
}
135135

136+
public function testAssertSentCount()
137+
{
138+
$this->fake->to('[email protected]')->send($this->mailable);
139+
$this->fake->to('[email protected]')->send($this->mailable);
140+
141+
try {
142+
$this->fake->assertSentCount(1);
143+
$this->fail();
144+
} catch (ExpectationFailedException $e) {
145+
$this->assertStringContainsString('The total number of mailables sent was 2 instead of 1.', $e->getMessage());
146+
}
147+
148+
$this->fake->assertSentCount(2);
149+
}
150+
136151
public function testAssertQueued()
137152
{
138153
try {
@@ -162,6 +177,21 @@ public function testAssertQueuedTimes()
162177
$this->fake->assertQueued(MailableStub::class, 2);
163178
}
164179

180+
public function testAssertQueuedCount()
181+
{
182+
$this->fake->to('[email protected]')->queue($this->mailable);
183+
$this->fake->to('[email protected]')->queue($this->mailable);
184+
185+
try {
186+
$this->fake->assertQueuedCount(1);
187+
$this->fail();
188+
} catch (ExpectationFailedException $e) {
189+
$this->assertStringContainsString('The total number of mailables queued was 2 instead of 1.', $e->getMessage());
190+
}
191+
192+
$this->fake->assertQueuedCount(2);
193+
}
194+
165195
public function testSendQueuesAMailableThatShouldBeQueued()
166196
{
167197
$this->fake->to('[email protected]')->send(new QueueableMailableStub);
@@ -204,6 +234,24 @@ public function testAssertNothingQueued()
204234
}
205235
}
206236

237+
public function testAssertOutgoingCount()
238+
{
239+
$this->fake->assertNothingOutgoing();
240+
241+
$this->fake->to('[email protected]')->queue($this->mailable);
242+
243+
try {
244+
$this->fake->assertOutgoingCount(2);
245+
$this->fail();
246+
} catch (ExpectationFailedException $e) {
247+
$this->assertStringContainsString('The total number of outgoing mailables was 1 instead of 2.', $e->getMessage());
248+
}
249+
250+
$this->fake->to('[email protected]')->send($this->mailable);
251+
252+
$this->fake->assertOutgoingCount(2);
253+
}
254+
207255
public function testAssertQueuedWithClosure()
208256
{
209257
$this->fake->to($user = new LocalizedRecipientStub)->queue($this->mailable);

0 commit comments

Comments
 (0)