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

[9.x] Add NotificationFake::assertNothingSentTo() #41232

Merged
merged 2 commits into from
Feb 24, 2022
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
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