1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<?php
namespace Illuminate\Tests\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Container\Container;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Notifications\Events\NotificationSending;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\SendQueuedNotifications;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class NotificationChannelManagerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
Container::setInstance(null);
}
public function testNotificationCanBeDispatchedToDriver()
{
$container = new Container;
$container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
$container->instance(Bus::class, $bus = m::mock());
$container->instance(Dispatcher::class, $events = m::mock());
Container::setInstance($container);
$manager = m::mock(ChannelManager::class.'[driver]', [$container]);
$manager->shouldReceive('driver')->andReturn($driver = m::mock());
$events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
$driver->shouldReceive('send')->once();
$events->shouldReceive('dispatch')->with(m::type(NotificationSent::class));
$manager->send(new NotificationChannelManagerTestNotifiable, new NotificationChannelManagerTestNotification);
}
public function testNotificationNotSentOnHalt()
{
$container = new Container;
$container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
$container->instance(Bus::class, $bus = m::mock());
$container->instance(Dispatcher::class, $events = m::mock());
Container::setInstance($container);
$manager = m::mock(ChannelManager::class.'[driver]', [$container]);
$events->shouldReceive('until')->once()->with(m::type(NotificationSending::class))->andReturn(false);
$events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
$manager->shouldReceive('driver')->once()->andReturn($driver = m::mock());
$driver->shouldReceive('send')->once();
$events->shouldReceive('dispatch')->with(m::type(NotificationSent::class));
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestNotificationWithTwoChannels);
}
public function testNotificationCanBeQueued()
{
$container = new Container;
$container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
$container->instance(Dispatcher::class, $events = m::mock());
$container->instance(Bus::class, $bus = m::mock());
$bus->shouldReceive('dispatch')->with(m::type(SendQueuedNotifications::class));
Container::setInstance($container);
$manager = m::mock(ChannelManager::class.'[driver]', [$container]);
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification);
}
}
class NotificationChannelManagerTestNotifiable
{
use Notifiable;
}
class NotificationChannelManagerTestNotification extends Notification
{
public function via()
{
return ['test'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
}
class NotificationChannelManagerTestNotificationWithTwoChannels extends Notification
{
public function via()
{
return ['test', 'test2'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
}
class NotificationChannelManagerTestQueuedNotification extends Notification implements ShouldQueue
{
use Queueable;
public function via()
{
return ['test'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
}
|