Skip to content

Commit

Permalink
[9.x] Add NotificationFake::assertNothingSentTo() (#41232)
Browse files Browse the repository at this point in the history
* Add NotificationFake::assertNothingSentTo()

* Update NotificationFake.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
axlon and taylorotwell authored Feb 24, 2022
1 parent eece14d commit 29f5cac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @method static mixed channel(string|null $name = null)
* @method static void assertNotSentTo(mixed $notifiable, string|\Closure $notification, callable $callback = null)
* @method static void assertNothingSent()
* @method static void assertNothingSentTo(mixed $notifiable)
* @method static void assertSentOnDemand(string|\Closure $notification, callable $callback = null)
* @method static void assertSentTo(mixed $notifiable, string|\Closure $notification, callable $callback = null)
* @method static void assertSentOnDemandTimes(string $notification, int $times = 1)
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ public function assertNothingSent()
PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.');
}

/**
* Assert that no notifications were sent to the given notifiable.
*
* @param mixed $notifiable
* @return void
*
* @throws \Exception
*/
public function assertNothingSentTo($notifiable)
{
if (is_array($notifiable) || $notifiable instanceof Collection) {
if (count($notifiable) === 0) {
throw new Exception('No notifiable given.');
}

foreach ($notifiable as $singleNotifiable) {
$this->assertNothingSentTo($singleNotifiable);
}

return;
}

PHPUnit::assertEmpty(
$this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
'Notifications were sent unexpectedly.',
);
}

/**
* Assert the total amount of times a notification was sent.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportTestingNotificationFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public function testAssertNotSentToClosure()
}
}

public function testAssertNothingSent()
{
$this->fake->assertNothingSent();
$this->fake->send($this->user, new NotificationStub);

try {
$this->fake->assertNothingSent();
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('Notifications were sent unexpectedly.'));
}
}

public function testAssertNothingSentTo()
{
$this->fake->assertNothingSentTo($this->user);
$this->fake->send($this->user, new NotificationStub);

try {
$this->fake->assertNothingSentTo($this->user);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('Notifications were sent unexpectedly.'));
}
}

public function testAssertSentToFailsForEmptyArray()
{
$this->expectException(Exception::class);
Expand Down

0 comments on commit 29f5cac

Please sign in to comment.