Skip to content

Commit

Permalink
Add assertSentTo shorthand (laravel#52083)
Browse files Browse the repository at this point in the history
* Add `assertSentTo` shorthand

* Add multiple example to test

* Overload existing method

* Add negative assertions
  • Loading branch information
jasonmccreary authored Jul 15, 2024
1 parent 435098a commit ddda68e
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 8 deletions.
65 changes: 57 additions & 8 deletions src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert as PHPUnit;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function __construct(MailManager $manager)
* Assert if a mailable was sent based on a truth-test callback.
*
* @param string|\Closure $mailable
* @param callable|int|null $callback
* @param callable|int|string|array|null $callback
* @return void
*/
public function assertSent($mailable, $callback = null)
Expand All @@ -71,15 +72,24 @@ public function assertSent($mailable, $callback = null)
return $this->assertSentTimes($mailable, $callback);
}

$message = "The expected [{$mailable}] mailable was not sent.";
$suggestion = count($this->queuedMailables) ? ' Did you mean to use assertQueued() instead?' : '';

if (count($this->queuedMailables) > 0) {
$message .= ' Did you mean to use assertQueued() instead?';
if (is_array($callback) || is_string($callback)) {
foreach (Arr::wrap($callback) as $address) {
$callback = fn ($mail) => $mail->hasTo($address);

PHPUnit::assertTrue(
$this->sent($mailable, $callback)->count() > 0,
"The expected [{$mailable}] mailable was not sent to address [{$address}].".$suggestion
);
}

return;
}

PHPUnit::assertTrue(
$this->sent($mailable, $callback)->count() > 0,
$message
"The expected [{$mailable}] mailable was not sent.".$suggestion
);
}

Expand Down Expand Up @@ -117,11 +127,24 @@ public function assertNotOutgoing($mailable, $callback = null)
* Determine if a mailable was not sent based on a truth-test callback.
*
* @param string|\Closure $mailable
* @param callable|null $callback
* @param callable|string|array|null $callback
* @return void
*/
public function assertNotSent($mailable, $callback = null)
{
if (is_string($callback) || is_array($callback)) {
foreach (Arr::wrap($callback) as $address) {
$callback = fn ($mail) => $mail->hasTo($address);

PHPUnit::assertCount(
0, $this->sent($mailable, $callback),
"The unexpected [{$mailable}] mailable was sent to address [{$address}]."
);
}

return;
}

[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

PHPUnit::assertCount(
Expand Down Expand Up @@ -159,7 +182,7 @@ public function assertNothingSent()
* Assert if a mailable was queued based on a truth-test callback.
*
* @param string|\Closure $mailable
* @param callable|int|null $callback
* @param callable|int|string|array|null $callback
* @return void
*/
public function assertQueued($mailable, $callback = null)
Expand All @@ -170,6 +193,19 @@ public function assertQueued($mailable, $callback = null)
return $this->assertQueuedTimes($mailable, $callback);
}

if (is_string($callback) || is_array($callback)) {
foreach (Arr::wrap($callback) as $address) {
$callback = fn ($mail) => $mail->hasTo($address);

PHPUnit::assertTrue(
$this->queued($mailable, $callback)->count() > 0,
"The expected [{$mailable}] mailable was not queued to address [{$address}]."
);
}

return;
}

PHPUnit::assertTrue(
$this->queued($mailable, $callback)->count() > 0,
"The expected [{$mailable}] mailable was not queued."
Expand Down Expand Up @@ -197,11 +233,24 @@ protected function assertQueuedTimes($mailable, $times = 1)
* Determine if a mailable was not queued based on a truth-test callback.
*
* @param string|\Closure $mailable
* @param callable|null $callback
* @param callable|string|array|null $callback
* @return void
*/
public function assertNotQueued($mailable, $callback = null)
{
if (is_string($callback) || is_array($callback)) {
foreach (Arr::wrap($callback) as $address) {
$callback = fn ($mail) => $mail->hasTo($address);

PHPUnit::assertCount(
0, $this->queued($mailable, $callback),
"The unexpected [{$mailable}] mailable was queued to address [{$address}]."
);
}

return;
}

[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

PHPUnit::assertCount(
Expand Down
104 changes: 104 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ public function testAssertSent()
$this->fake->assertSent(MailableStub::class);
}

public function testAssertSentTo()
{
try {
$this->fake->assertSent(MailableStub::class, '[email protected]');
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\MailableStub] mailable was not sent to address [[email protected]].', $e->getMessage());
}

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

$this->fake->assertSent(MailableStub::class, '[email protected]');
}

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

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

$this->fake->assertSent(MailableStub::class, 3);
$this->fake->assertSent(
MailableStub::class,
['[email protected]', '[email protected]', '[email protected]', '[email protected]']
);
}

public function testAssertSentWhenRecipientHasPreferredLocale()
{
$user = new LocalizedRecipientStub;
Expand Down Expand Up @@ -118,6 +146,30 @@ public function testAssertNotSentWithClosure()
$this->fake->assertNotSent($callback);
}

public function testAssertNotSentWithString()
{
$this->fake->assertNotSent(MailableStub::class, '[email protected]');

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

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The unexpected ['.MailableStub::class.'] mailable was sent to address [[email protected]].');

$this->fake->assertNotSent(MailableStub::class, '[email protected]');
}

public function testAssertNotSentWithArray()
{
$this->fake->assertNotSent(MailableStub::class, ['[email protected]', '[email protected]']);

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

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The unexpected ['.MailableStub::class.'] mailable was sent to address [[email protected]].');

$this->fake->assertNotSent(MailableStub::class, ['[email protected]', '[email protected]']);
}

public function testAssertSentTimes()
{
$this->fake->to('[email protected]')->send($this->mailable);
Expand Down Expand Up @@ -162,6 +214,34 @@ public function testAssertQueued()
$this->fake->assertQueued(MailableStub::class);
}

public function testAssertQueuedTo()
{
try {
$this->fake->assertQueued(MailableStub::class, '[email protected]');
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\MailableStub] mailable was not queued to address [[email protected]].', $e->getMessage());
}

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

$this->fake->assertQueued(MailableStub::class, '[email protected]');
}

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

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

$this->fake->assertQueued(MailableStub::class, 3);
$this->fake->assertQueued(
MailableStub::class,
['[email protected]', '[email protected]', '[email protected]', '[email protected]']
);
}

public function testAssertQueuedTimes()
{
$this->fake->to('[email protected]')->queue($this->mailable);
Expand All @@ -177,6 +257,30 @@ public function testAssertQueuedTimes()
$this->fake->assertQueued(MailableStub::class, 2);
}

public function testAssertNotQueuedWithString()
{
$this->fake->assertNotQueued(MailableStub::class, '[email protected]');

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

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The unexpected ['.MailableStub::class.'] mailable was queued to address [[email protected]].');

$this->fake->assertNotQueued(MailableStub::class, '[email protected]');
}

public function testAssertNotQueuedWithArray()
{
$this->fake->assertNotQueued(MailableStub::class, ['[email protected]', '[email protected]']);

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

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('The unexpected ['.MailableStub::class.'] mailable was queued to address [[email protected]].');

$this->fake->assertNotQueued(MailableStub::class, ['[email protected]', '[email protected]']);
}

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

0 comments on commit ddda68e

Please sign in to comment.