diff --git a/src/Illuminate/Support/Facades/Notification.php b/src/Illuminate/Support/Facades/Notification.php index e16a9cceaf7a..68f7201d27ee 100644 --- a/src/Illuminate/Support/Facades/Notification.php +++ b/src/Illuminate/Support/Facades/Notification.php @@ -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) diff --git a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php index c7b12f42d47c..4c089116ad3c 100644 --- a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php +++ b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php @@ -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. * diff --git a/tests/Support/SupportTestingNotificationFakeTest.php b/tests/Support/SupportTestingNotificationFakeTest.php index 06f49b6a1655..d28d386671b7 100644 --- a/tests/Support/SupportTestingNotificationFakeTest.php +++ b/tests/Support/SupportTestingNotificationFakeTest.php @@ -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);