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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<?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 testNotificationNotSentWhenCancelled()
{
$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')->with(m::type(NotificationSending::class))->andReturn(true);
$manager->shouldNotReceive('driver');
$events->shouldNotReceive('dispatch');
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestCancelledNotification);
}
public function testNotificationSentWhenNotCancelled()
{
$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')->with(m::type(NotificationSending::class))->andReturn(true);
$manager->shouldReceive('driver')->once()->andReturn($driver = m::mock());
$driver->shouldReceive('send')->once();
$events->shouldReceive('dispatch')->once()->with(m::type(NotificationSent::class));
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestNotCancelledNotification);
}
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 NotificationChannelManagerTestCancelledNotification extends Notification
{
public function via()
{
return ['test'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
public function shouldSend($notifiable, $channel)
{
return false;
}
}
class NotificationChannelManagerTestNotCancelledNotification extends Notification
{
public function via()
{
return ['test'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
public function shouldSend($notifiable, $channel)
{
return true;
}
}
class NotificationChannelManagerTestQueuedNotification extends Notification implements ShouldQueue
{
use Queueable;
public function via()
{
return ['test'];
}
public function message()
{
return $this->line('test')->action('Text', 'url');
}
}
|