Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JillzTom's avatar

Notification in Lumen 5.3

Is that Notification system from laravel 5.3 available in Lumen 5.3? If not, how can I implement it in Lumen?

0 likes
4 replies
jekinney's avatar

Doesn't look like it on the docs. Lumen 5.1 had pretty much the entire Laravel files under vendor folder. Look in there. If it is implement it via composer auto load or use statement.

dmattingley's avatar

You can pull in the notification library used in laravel 5.3 by putting

 "illuminate/notifications": "5.3.*"

into your composer.json then running composer update to pull in the notification libraries.

You will also need to add

$app->register(Illuminate\Notifications\NotificationServiceProvider::class);

to your bootstrap/app.php

To use slack notifications you'll also need guzzle in your composer.json

"guzzlehttp/guzzle": "6.0.*"

Tested on lumen 5.3

1 like
wheeler's avatar

Everything @dmattingley said is accurate.

One thing to be aware of is that (unsurprisingly) Lumen doesn't support testing notifications out of the box.

Just update tests/TestCase as follows:


<?php

use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;

abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{
    /** @var array */
    protected $dispatchedNotifications = [];

    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    public function createApplication()
    {
        return require __DIR__ . '/../bootstrap/app.php';
    }

    /**
     * Mock the notification dispatcher so all notifications are silenced.
     *
     * @return $this
     */
    protected function withoutNotifications()
    {
        $mock = Mockery::mock(NotificationDispatcher::class);
        $mock->shouldReceive('send')->andReturnUsing(function ($notifiable, $instance, $channels = []) {
            $this->dispatchedNotifications[] = compact(
                'notifiable', 'instance', 'channels'
            );
        });
        $this->app->instance(NotificationDispatcher::class, $mock);
        return $this;
    }

    /**
     * Specify a notification that is expected to be dispatched.
     *
     * @param  mixed $notifiable
     * @param  string $notification
     * @return $this
     */
    protected function expectsNotification($notifiable, $notification)
    {
        $this->withoutNotifications();
        $this->beforeApplicationDestroyed(function () use ($notifiable, $notification) {
            foreach ($this->dispatchedNotifications as $dispatched) {
                $notified = $dispatched['notifiable'];
                if (($notified === $notifiable ||
                        $notified->getKey() == $notifiable->getKey()) &&
                    get_class($dispatched['instance']) === $notification
                ) {
                    return $this;
                }
            }
            throw new Exception(
                'The following expected notification were not dispatched: [' . $notification . ']'
            );
        });
        return $this;
    }

    /**
     * Specify a notification that is not expected to be dispatched.
     *
     * @param  mixed $notifiable
     * @param  string $notification
     * @return $this
     */
    protected function doesntExpectNotification($notifiable, $notification)
    {
        $this->withoutNotifications();
        $this->beforeApplicationDestroyed(function () use ($notifiable, $notification) {
            foreach ($this->dispatchedNotifications as $dispatched) {
                $notified = $dispatched['notifiable'];
                if (($notified === $notifiable ||
                        $notified->getKey() == $notifiable->getKey()) &&
                    get_class($dispatched['instance']) === $notification
                ) {
                    throw new Exception(
                        'These unexpected notifications were fired: [' . $notification . ']'
                    );
                }
            }
        });
        return $this;
    }
}

Usage:


/** @test */
function test_something()
{
    $this->expectsNotification($user, Notifications\SpaceshipHasLaunched::class);
    $this->doesntExpectNotification($user, Notifications\SpaceshipHasLaunched::class); 
}

I wrote an article about setting up notfications: https://stevethomas.com.au/php/using-laravel-notifications-in-lumen.html

2 likes

Please or to participate in this conversation.