symfony/fake-chat-notifier
Symfony Fake Chat Notifier provides a fake chat transport for the Symfony Notifier component, ideal for local development and automated tests. Simulate sending chat messages without hitting real providers, with predictable, inspectable behavior.
Install the Package
composer require symfony/fake-chat-notifier
Publish Configuration
php artisan vendor:publish --provider="Symfony\Component\FakeChatNotifier\FakeChatNotifierServiceProvider" --tag="config"
Edit config/fake-chat-notifier.php to define your fake channel (e.g., log or email):
'channels' => [
'log' => [
'driver' => 'log',
'channel' => 'notifications',
],
'email' => [
'driver' => 'email',
'to' => 'dev@example.com',
'from' => 'notifier@example.com',
],
],
Configure DSN in .env
Choose either:
FAKE_CHAT_DSN=fakechat+logger://default
FAKE_CHAT_DSN=fakechat+email://default?to=dev@example.com&from=notifier@example.com
Bind the Fake Notifier in AppServiceProvider
use Symfony\Component\FakeChatNotifier\FakeChatNotifier;
public function register()
{
if (app()->environment('local')) {
$this->app->bind(\Symfony\Component\Notifier\NotifierInterface::class, function ($app) {
return new FakeChatNotifier(
$app->make(\Symfony\Component\Notifier\NotifierInterface::class)
);
});
}
}
Send a Test Notification
use Symfony\Component\Notifier\Message\ChatMessage;
$notifier = app(\Symfony\Component\Notifier\NotifierInterface::class);
$notifier->send(new ChatMessage('Hello from Fake Chat!', 'default'));
storage/logs/laravel.log for the notification.to address.Replace Real Notifiers
In AppServiceProvider, bind the fake notifier only in local environments:
if (app()->environment('local')) {
$this->app->bind(\Symfony\Component\Notifier\NotifierInterface::class, function ($app) {
return new FakeChatNotifier($app->make(\Symfony\Component\Notifier\NotifierInterface::class));
});
}
Leverage Laravel’s Notification System Create a custom notification class:
use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\Message\ChatMessage;
class ChatAlert extends Notification
{
public function via($notifier)
{
return ['database', 'fake-chat']; // Include 'fake-chat' for local testing
}
public function toFakeChat($notifiable)
{
return (new ChatMessage())
->subject('New Alert')
->text('This is a test alert from Laravel!');
}
}
Send Notifications
$user->notify(new ChatAlert());
Use fake notifications in queued jobs:
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
class SendChatAlert implements ShouldQueue
{
use Queueable, SerializesModels, InteractsWithQueue;
public function handle()
{
$user->notify(new ChatAlert());
}
}
Assert fake notifications in PHPUnit:
use Symfony\Component\Notifier\Message\ChatMessage;
public function test_chat_notification_sent()
{
$notifier = $this->app->make(\Symfony\Component\Notifier\NotifierInterface::class);
$notifier->send(new ChatMessage('Test', 'default'));
// Assert log output (Laravel 8+)
$this->assertLogged('Test');
// Or assert email (if using email channel)
Mail::assertSent(FakeEmail::class);
}
Double Notifications in Production
AppServiceProvider can send notifications to both fake and real channels.if (app()->environment('local')) {
// Bind fake notifier
}
Missing DSN Configuration
FAKE_CHAT_DSN is not set in .env, the fake notifier will fail silently.config/fake-chat-notifier.php:
'default_dsn' => env('FAKE_CHAT_DSN', 'fakechat+logger://default'),
Log Channel Not Found
notifications) doesn’t exist, fake notifications won’t appear in logs.config/logging.php:
'channels' => [
'notifications' => [
'driver' => 'single',
'path' => storage_path('logs/notifications.log'),
'level' => 'debug',
],
],
Check Log Levels
Ensure the log channel is set to debug or lower to capture fake notifications:
'notifications' => [
'driver' => 'single',
'path' => storage_path('logs/notifications.log'),
'level' => 'debug', // Critical for fake notifications
],
Validate Email Configuration If using the email channel, ensure Laravel’s mail configuration is correct:
MAIL_MAILER=log # For testing emails in logs
MAIL_FROM_ADDRESS="notifier@example.com"
MAIL_FROM_NAME="${APP_NAME}"
Override Fake Notifier in Tests Mock the fake notifier for isolated testing:
$this->app->instance(\Symfony\Component\Notifier\NotifierInterface::class, \Mockery::mock());
Custom Fake Channels Extend the fake notifier to support additional channels (e.g., Laravel Horizon):
use Symfony\Component\FakeChatNotifier\FakeChannelInterface;
use Symfony\Component\Notifier\Message\MessageInterface;
class HorizonFakeChannel implements FakeChannelInterface
{
public function send(MessageInterface $message)
{
// Push to Horizon queue
\App\Jobs\ProcessFakeNotification::dispatch($message);
}
}
Register the channel in config/fake-chat-notifier.php:
'channels' => [
'horizon' => [
'driver' => 'horizon',
],
],
Dynamic DSN Configuration Use environment variables to switch between log and email channels dynamically:
'default_dsn' => env('FAKE_CHAT_DSN', env('APP_ENV') === 'local'
? 'fakechat+logger://default'
: 'fakechat+email://default?to=dev@example.com'),
Laravel Notifications Integration Create a custom notification channel for seamless Laravel integration:
use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\Message\ChatMessage;
class FakeChatChannel
{
public function send($notifiable, Notification $notification)
{
$message = $notification->toFakeChat($notifiable);
app(\Symfony\Component\Notifier\NotifierInterface::class)->send($message);
}
}
Register the channel in config/notifications.php:
'channels' => [
'fake-chat' => [
'driver' => 'Symfony\Component\FakeChatNotifier\FakeChatChannel',
],
],
How can I help you explore Laravel packages today?