Skip to content

Commit 29f5cac

Browse files
axlontaylorotwell
andauthored
[9.x] Add NotificationFake::assertNothingSentTo() (#41232)
* Add NotificationFake::assertNothingSentTo() * Update NotificationFake.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent eece14d commit 29f5cac

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/Illuminate/Support/Facades/Notification.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @method static mixed channel(string|null $name = null)
1414
* @method static void assertNotSentTo(mixed $notifiable, string|\Closure $notification, callable $callback = null)
1515
* @method static void assertNothingSent()
16+
* @method static void assertNothingSentTo(mixed $notifiable)
1617
* @method static void assertSentOnDemand(string|\Closure $notification, callable $callback = null)
1718
* @method static void assertSentTo(mixed $notifiable, string|\Closure $notification, callable $callback = null)
1819
* @method static void assertSentOnDemandTimes(string $notification, int $times = 1)

src/Illuminate/Support/Testing/Fakes/NotificationFake.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,34 @@ public function assertNothingSent()
158158
PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.');
159159
}
160160

161+
/**
162+
* Assert that no notifications were sent to the given notifiable.
163+
*
164+
* @param mixed $notifiable
165+
* @return void
166+
*
167+
* @throws \Exception
168+
*/
169+
public function assertNothingSentTo($notifiable)
170+
{
171+
if (is_array($notifiable) || $notifiable instanceof Collection) {
172+
if (count($notifiable) === 0) {
173+
throw new Exception('No notifiable given.');
174+
}
175+
176+
foreach ($notifiable as $singleNotifiable) {
177+
$this->assertNothingSentTo($singleNotifiable);
178+
}
179+
180+
return;
181+
}
182+
183+
PHPUnit::assertEmpty(
184+
$this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
185+
'Notifications were sent unexpectedly.',
186+
);
187+
}
188+
161189
/**
162190
* Assert the total amount of times a notification was sent.
163191
*

tests/Support/SupportTestingNotificationFakeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ public function testAssertNotSentToClosure()
106106
}
107107
}
108108

109+
public function testAssertNothingSent()
110+
{
111+
$this->fake->assertNothingSent();
112+
$this->fake->send($this->user, new NotificationStub);
113+
114+
try {
115+
$this->fake->assertNothingSent();
116+
$this->fail();
117+
} catch (ExpectationFailedException $e) {
118+
$this->assertThat($e, new ExceptionMessage('Notifications were sent unexpectedly.'));
119+
}
120+
}
121+
122+
public function testAssertNothingSentTo()
123+
{
124+
$this->fake->assertNothingSentTo($this->user);
125+
$this->fake->send($this->user, new NotificationStub);
126+
127+
try {
128+
$this->fake->assertNothingSentTo($this->user);
129+
$this->fail();
130+
} catch (ExpectationFailedException $e) {
131+
$this->assertThat($e, new ExceptionMessage('Notifications were sent unexpectedly.'));
132+
}
133+
}
134+
109135
public function testAssertSentToFailsForEmptyArray()
110136
{
111137
$this->expectException(Exception::class);

0 commit comments

Comments
 (0)