Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Fake Chat Notifier Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Notification

  1. Install the Package

    composer require symfony/fake-chat-notifier
    
  2. 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',
        ],
    ],
    
  3. Configure DSN in .env Choose either:

    • Log Output:
      FAKE_CHAT_DSN=fakechat+logger://default
      
    • Email Output:
      FAKE_CHAT_DSN=fakechat+email://default?to=dev@example.com&from=notifier@example.com
      
  4. 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)
                );
            });
        }
    }
    
  5. 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'));
    
    • Log Output: Check storage/logs/laravel.log for the notification.
    • Email Output: Receive an email at the configured to address.

Implementation Patterns

Workflow: Local Development with Fake Notifications

  1. 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));
        });
    }
    
  2. 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!');
        }
    }
    
  3. Send Notifications

    $user->notify(new ChatAlert());
    
    • In local, notifications appear in logs/email.
    • In production, notifications use the real notifier.

Integration with Laravel Queues

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());
    }
}

Testing Notifications

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);
}

Gotchas and Tips

Common Pitfalls

  1. Double Notifications in Production

    • Issue: Forgetting to conditionally bind the fake notifier in AppServiceProvider can send notifications to both fake and real channels.
    • Fix: Always wrap the binding in an environment check:
      if (app()->environment('local')) {
          // Bind fake notifier
      }
      
  2. Missing DSN Configuration

    • Issue: If FAKE_CHAT_DSN is not set in .env, the fake notifier will fail silently.
    • Fix: Set a default DSN in config/fake-chat-notifier.php:
      'default_dsn' => env('FAKE_CHAT_DSN', 'fakechat+logger://default'),
      
  3. Log Channel Not Found

    • Issue: If the log channel (notifications) doesn’t exist, fake notifications won’t appear in logs.
    • Fix: Define the channel in config/logging.php:
      'channels' => [
          'notifications' => [
              'driver' => 'single',
              'path' => storage_path('logs/notifications.log'),
              'level' => 'debug',
          ],
      ],
      

Debugging Tips

  1. 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
    ],
    
  2. 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}"
    
  3. Override Fake Notifier in Tests Mock the fake notifier for isolated testing:

    $this->app->instance(\Symfony\Component\Notifier\NotifierInterface::class, \Mockery::mock());
    

Extension Points

  1. 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',
        ],
    ],
    
  2. 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'),
    
  3. 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',
        ],
    ],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views