NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages

Notifications::fake() swaps the manager for a NotificationsFake behind the facade and in the container, and returns it:

use RoundlyConsulting\Notifications\Facades\Notifications;

it('asks the approvers', function (): void {
    $notifications = Notifications::fake();            // record-only: nothing is written, nothing is mailed
    $request = PurchaseRequest::factory()->create();

    $this->actingAs($request->author)->post("/requests/{$request->id}/submit")->assertRedirect();

    $notifications->assertSent(RequestAwaitingApproval::class, to: $approver, about: $request);   // key or class
    $notifications->assertNotSent('request.awaiting_approval', to: $request->author);
    $notifications->assertNotSent('security.alert');
});
  • It is record-only: every mutating call — sends (also through type()->…->send(), for()->send() and sendNotification()), inbox mutations, preference writes, unsubscribe redeems, reminder schedule / sync / remind / cancel / snooze / acknowledge, revoke, sweeps, prunes, the inbox channel, the subject-deleted inbox cleanup and calls through the model traits — is recorded and not executed. A faked send returns a SendResult with the type key and no outcomes, like the real send.
  • A send still runs the real send’s up-front checks and throws the same exceptions, recording nothing: an unknown type, a subject of the wrong class or an escalation without one, data that is not plain JSON, bad keys, contexts or unstorable escalation recipients — a green test never hides a production exception.
  • Reads (inbox()->paginate(), types(), preferences()->matrix()) pass through to the real store, and type registration passes through.
  • Recipient resolution still runs, so assertSent(to: $user) works with Recipients::role(). Access checks, preferences, quiet hours and de-dup do not: an assertion proves the intent to notify, not a delivery.
  • NotificationsFake extends the manager, so an injected NotificationsManager is faked and still type-checks. Calling an action directly is not recorded.
  • The interop listener is never recorded or blocked by the fake.
  • ->passthrough() records and also runs every operation.
  • ->withReminders() (since 1.3.0) runs the reminder operations for real — schedule, sync, remind(), cancel, snooze, acknowledge, backfill, fire — so reminder rows exist and fire, while the sends they fire stay recorded. Reminder and escalation sends go through the manager, so the fake records them like any other send.
Notifications::fake();

// The real send's up-front checks still run — and throw the same exceptions, recording nothing
Notifications::send('request.awaitng_approval', $request, $approver);   // UnknownNotificationTypeException
Notifications::send('security.alert', $request, $user);                 // InvalidNotificationSubjectException: no subject expected
Notifications::send('comment.posted', $post, $user, ['post' => $post]); // InvalidNotificationDataException: not plain JSON

Assertions

Every assertion is on the fake and forwarded by the facade, and each failure names what was expected. Every assertion with a type takes a type key or its class, and the fake records every operation’s type by key, as the real operations store it — so remind(Foo::class, …) passes assertReminderScheduled($subject, 'foo.key') and vice versa:

AssertionPasses when
assertSent($type, to: ?Model|Closure, about: ?Model, times: ?int)A send of the type (to the recipient, about the subject; exactly times) was recorded. A closure to receives each resolved recipient.
assertNotSent($type, to: ?Model|Closure, about: ?Model)No such send — to that recipient, about that subject.
assertNothingSent()No send at all.
assertMarkedRead(?Model $recipient) · assertNothingMarkedRead()markRead() or markAllRead() / none.
assertMarkedUnread(?Model $recipient)markUnread().
assertArchived(?Model $recipient) · assertNothingArchived()archive() / none.
assertUnarchived(?Model $recipient)unarchive().
assertDeleted(?Model $recipient)delete().
assertInboxMessage(Model $recipient, ?string $type)The Laravel inbox channel wrote a message (of the type).
assertSubjectForgotten(Model $subject)A deleted subject’s inbox cleanup ran (notifications.inbox.on_subject_deleted is not keep).
assertPreferenceSet($user, $type, Channel $channel, bool $enabled)set() or mute() of that channel to that value.
assertMuted($user, $type)A mute (an off write) of the type.
assertCategoryPreferenceSet($user, $category, Channel $channel, ?bool $enabled)muteCategory() (false) or unmuteCategory() (null).
assertPreferencesUpdated($user, ?Closure $where)preferences($user)->update(); $where(PreferencesUpdate): bool narrows it.
assertPreferencesReset($user)reset().
assertDigestSet($user, DigestFrequency $frequency, ?string $time)digest(); a null time matches any time.
assertQuietHoursSet($user, ?string $start, ?string $end)quietHours(); both null: cleared.
assertTimezoneSet($user, ?string $timezone)timezone(); null: cleared.
assertUnsubscribed(?Model $recipient, ?string $type) · assertNothingUnsubscribed()unsubscribe()->redeem($token) — the link’s recipient and type are recorded; a bad token throws as in production / none.
assertNoPreferenceChanged()No preference write of any kind — type, category, reset, update, digest, quiet hours, timezone.
assertRemindersSynced($subject)A sync, including the one HasNotificationReminders runs on save.
assertReminderScheduled($subject, $type, ?bool $restart = null)schedule() or remind(); restart: true matches only a schedule(…, restart: true), false only one without it.
assertNoRemindersScheduled()No schedule(), sync, remind() or backfill.
assertRemindersCancelled($subject, ?Model $for = null)cancel(), or the cancel a deleted subject triggers; with for: only a cancel(for: $recipient) of that recipient.
assertRemindersSnoozed($subject, ?Model $recipient, ?$type, ?CarbonInterface $until)reminders()->snooze() (since 1.4.0).
assertAcknowledged($subject, ?$type)acknowledge().
fireDueReminders(): FiredRemindersNot an assertion: runs the sweep’s reminder step now (also without withReminders()); the sends it fires are recorded.
assertReminded(Model $recipient, string $type, ?int $times, ?Model $about) · assertNotReminded($recipient, $type, ?$about)A fired reminder sent the type to the recipient (escalations excluded) / none did.
assertEscalated(string $type, ?Model $to, ?int $times, ?Model $about)An escalation fired.
assertRevoked($subject, ?Model $recipient)revoke().
assertSwept() · assertPruned()sweep() or notifications:sweep / prune() or notifications:prune.
assertRecorded(NotificationOperation $operation, ?Closure $where, ?int $times)Any operation — also those without a named assertion (SendDigests, DispatchDeliveries, FireReminders, BackfillReminders); $where(RecordedOperation): bool narrows it.
assertNotRecorded(NotificationOperation $operation, ?Closure $where)No such operation.
$fake = Notifications::fake();

$fake->assertSent('comment.posted', to: fn (User $user): bool => $user->is_admin, times: 2);
$fake->assertNotSent('comment.posted', about: $draft);
$fake->assertMarkedRead($user);
$fake->assertDeleted($user);
$fake->assertInboxMessage($user, 'comment.posted');
$fake->assertDigestSet($user, DigestFrequency::Daily);          // any time
$fake->assertQuietHoursSet($user, '22:00', '07:00');
$fake->assertPreferencesUpdated($user, fn (PreferencesUpdate $update): bool => isset($update->channels['comment.posted']));
$fake->assertRemindersSynced($contract);                        // also the sync HasNotificationReminders runs on save
$fake->assertAcknowledged($contract, 'contract.expiring');
$fake->assertRevoked($request, $formerMember);
$fake->assertSubjectForgotten($post);                           // the on_subject_deleted cleanup of a deleted subject
$fake->assertUnsubscribed($user, 'comment.posted');             // a redeemed one-click unsubscribe link
$fake->assertSwept();

// Operations without a named assertion
$fake->assertRecorded(NotificationOperation::SendDigests);
$fake->assertRecorded(NotificationOperation::Send, fn (RecordedOperation $op): bool => $op->subject?->is($request) ?? false, times: 1);
$fake->assertNotRecorded(NotificationOperation::BackfillReminders);

// The facade forwards every assertion too
Notifications::assertNothingSent();
Notifications::assertNoPreferenceChanged();

// Record and still run
Notifications::fake()->passthrough();

Testing repeating reminders

Combine withReminders() with time travel and fireDueReminders(), then assert per recipient:

$fake = Notifications::fake()->withReminders();
Notifications::for($policy)->reminders()->schedule('policy.unacknowledged', to: [$jane, $john]);

$this->travel(7)->days();
$fake->fireDueReminders();
Notifications::for($policy)->acknowledge('policy.unacknowledged', by: $jane);
$this->travel(7)->days();
$fake->fireDueReminders();

$fake->assertReminded($jane, 'policy.unacknowledged', times: 1);
$fake->assertReminded($john, 'policy.unacknowledged', times: 2);

Recorded operations

$fake->recorded(?NotificationOperation $operation = null) returns the RecordedOperation records in call order — operation, context (the call’s arguments), the resolved recipients of a send, type and subject:

use RoundlyConsulting\Notifications\Enums\NotificationOperation;

foreach ($fake->recorded(NotificationOperation::Send) as $send) {
    $send->operation;     // NotificationOperation::Send
    $send->type;          // 'request.awaiting_approval'
    $send->subject;       // the subject model
    $send->recipients;    // list<Model> — the resolved recipients
    $send->context;       // the call's arguments
}

Pest expectations

Registered automatically through the package’s Pest plugin; they read the database:

ExpectationPasses when
toHaveUnreadInboxItems(int $count)Exactly this many unread, not archived items.
toHaveInboxItem(string $type, ?Model $subject = null)An item of the type (about the subject).
toHaveMuted(string $type)The type’s mail is off for the user.
expect($user)->toHaveUnreadInboxItems(1);
expect($user)->toHaveInboxItem('request.awaiting_approval', $request);
expect($user)->toHaveMuted('comment.posted');

// Where Pest plugin discovery is off
RoundlyConsulting\Notifications\Testing\NotificationExpectations::register();

The real pipeline with Notification::fake()

To assert a delivery, run the real pipeline and fake Laravel’s notifications. Digests arrive as DigestMailNotification. Freeze time for quiet hours, digests and reminders, and run Notifications::sweep() to release what is due:

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
use RoundlyConsulting\Notifications\Mail\TypedMailNotification;

Notification::fake();

Notifications::send('security.alert', null, $user);

Notification::assertSentTo($user, TypedMailNotification::class, fn ($n) => $n->type === 'security.alert');
expect($user)->toHaveUnreadInboxItems(1);

// Quiet hours, digests and reminders: freeze time, then release what is due
Carbon::setTestNow('2026-11-10 07:01');
Notifications::sweep();

Notification::fake() dispatches no NotificationSending, so interop does not run under it.

Factories

Every model ships a factory resolved through the model seam:

InboxItem::factory()->recipient($user)->read()->create();
NotificationDelivery::factory()->recipient($user)->status(DeliveryStatus::Held)->create();
NotificationReminder::factory()->subject($contract)->create();
DigestRun::factory()->recipient($user)->create();
NotificationReceipt::factory()->create();

Testing an app

  • Publish both migration sets (notifications and options) — RefreshDatabase works on SQLite in tests.
  • Define a policy or the view ability for every subject class your types use, or the first send throws MissingAccessCheckException.
  • Turn the queue off (or use the sync connection) when a test needs mail to go out inline.
  • Use on / off values in phpunit.xml:
<php>
    <env name="NOTIFICATIONS_ROUTES_ENABLED" value="on"/>
    <env name="NOTIFICATIONS_QUEUE_ENABLED" value="off"/>
</php>

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.