Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertions for counting outgoing mailables #47655

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,56 @@ public function assertNothingQueued()
PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames);
}

/**
* Assert the total number of mailables that were sent.
*
* @param int $count
* @return void
*/
public function assertSentCount($count)
{
$total = collect($this->mailables)->count();

PHPUnit::assertSame(
$count, $total,
"The total number of mailables sent was {$total} instead of {$count}."
);
}

/**
* Assert the total number of mailables that were queued.
*
* @param int $count
* @return void
*/
public function assertQueuedCount($count)
{
$total = collect($this->queuedMailables)->count();

PHPUnit::assertSame(
$count, $total,
"The total number of mailables queued was {$total} instead of {$count}."
);
}

/**
* Assert the total number of mailables that were sent or queued.
*
* @param int $count
* @return void
*/
public function assertOutgoingCount($count)
{
$total = collect($this->mailables)
->concat($this->queuedMailables)
->count();

PHPUnit::assertSame(
$count, $total,
"The total number of outgoing mailables was {$total} instead of {$count}."
);
}

/**
* Get all of the mailables matching a truth-test callback.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ public function testAssertSentTimes()
$this->fake->assertSent(MailableStub::class, 2);
}

public function testAssertSentCount()
{
$this->fake->to('[email protected]')->send($this->mailable);
$this->fake->to('[email protected]')->send($this->mailable);

try {
$this->fake->assertSentCount(1);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The total number of mailables sent was 2 instead of 1.', $e->getMessage());
}

$this->fake->assertSentCount(2);
}

public function testAssertQueued()
{
try {
Expand Down Expand Up @@ -162,6 +177,21 @@ public function testAssertQueuedTimes()
$this->fake->assertQueued(MailableStub::class, 2);
}

public function testAssertQueuedCount()
{
$this->fake->to('[email protected]')->queue($this->mailable);
$this->fake->to('[email protected]')->queue($this->mailable);

try {
$this->fake->assertQueuedCount(1);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The total number of mailables queued was 2 instead of 1.', $e->getMessage());
}

$this->fake->assertQueuedCount(2);
}

public function testSendQueuesAMailableThatShouldBeQueued()
{
$this->fake->to('[email protected]')->send(new QueueableMailableStub);
Expand Down Expand Up @@ -204,6 +234,24 @@ public function testAssertNothingQueued()
}
}

public function testAssertOutgoingCount()
{
$this->fake->assertNothingOutgoing();

$this->fake->to('[email protected]')->queue($this->mailable);

try {
$this->fake->assertOutgoingCount(2);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The total number of outgoing mailables was 1 instead of 2.', $e->getMessage());
}

$this->fake->to('[email protected]')->send($this->mailable);

$this->fake->assertOutgoingCount(2);
}

public function testAssertQueuedWithClosure()
{
$this->fake->to($user = new LocalizedRecipientStub)->queue($this->mailable);
Expand Down