22
33namespace Illuminate \Tests \Notifications ;
44
5+ use Exception ;
56use Illuminate \Bus \Queueable ;
67use Illuminate \Container \Container ;
78use Illuminate \Contracts \Bus \Dispatcher as Bus ;
89use Illuminate \Contracts \Events \Dispatcher ;
910use Illuminate \Contracts \Queue \ShouldQueue ;
1011use Illuminate \Notifications \ChannelManager ;
12+ use Illuminate \Notifications \Events \NotificationFailed ;
1113use Illuminate \Notifications \Events \NotificationSending ;
1214use Illuminate \Notifications \Events \NotificationSent ;
1315use Illuminate \Notifications \Notifiable ;
1416use Illuminate \Notifications \Notification ;
1517use Illuminate \Notifications \SendQueuedNotifications ;
18+ use Illuminate \Support \Collection ;
1619use Mockery as m ;
1720use PHPUnit \Framework \TestCase ;
1821
@@ -34,6 +37,7 @@ public function testNotificationCanBeDispatchedToDriver()
3437 Container::setInstance ($ container );
3538 $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
3639 $ manager ->shouldReceive ('driver ' )->andReturn ($ driver = m::mock ());
40+ $ events ->shouldReceive ('listen ' )->once ();
3741 $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
3842 $ driver ->shouldReceive ('send ' )->once ();
3943 $ events ->shouldReceive ('dispatch ' )->with (m::type (NotificationSent::class));
@@ -49,6 +53,7 @@ public function testNotificationNotSentOnHalt()
4953 $ container ->instance (Dispatcher::class, $ events = m::mock ());
5054 Container::setInstance ($ container );
5155 $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
56+ $ events ->shouldReceive ('listen ' )->once ();
5257 $ events ->shouldReceive ('until ' )->once ()->with (m::type (NotificationSending::class))->andReturn (false );
5358 $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
5459 $ manager ->shouldReceive ('driver ' )->once ()->andReturn ($ driver = m::mock ());
@@ -66,6 +71,7 @@ public function testNotificationNotSentWhenCancelled()
6671 $ container ->instance (Dispatcher::class, $ events = m::mock ());
6772 Container::setInstance ($ container );
6873 $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
74+ $ events ->shouldReceive ('listen ' )->once ();
6975 $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
7076 $ manager ->shouldNotReceive ('driver ' );
7177 $ events ->shouldNotReceive ('dispatch ' );
@@ -81,6 +87,7 @@ public function testNotificationSentWhenNotCancelled()
8187 $ container ->instance (Dispatcher::class, $ events = m::mock ());
8288 Container::setInstance ($ container );
8389 $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
90+ $ events ->shouldReceive ('listen ' )->once ();
8491 $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
8592 $ manager ->shouldReceive ('driver ' )->once ()->andReturn ($ driver = m::mock ());
8693 $ driver ->shouldReceive ('send ' )->once ();
@@ -89,6 +96,56 @@ public function testNotificationSentWhenNotCancelled()
8996 $ manager ->send ([new NotificationChannelManagerTestNotifiable ], new NotificationChannelManagerTestNotCancelledNotification );
9097 }
9198
99+ public function testNotificationNotSentWhenFailed ()
100+ {
101+ $ this ->expectException (Exception::class);
102+
103+ $ container = new Container ;
104+ $ container ->instance ('config ' , ['app.name ' => 'Name ' , 'app.logo ' => 'Logo ' ]);
105+ $ container ->instance (Bus::class, $ bus = m::mock ());
106+ $ container ->instance (Dispatcher::class, $ events = m::mock ());
107+ Container::setInstance ($ container );
108+ $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
109+ $ manager ->shouldReceive ('driver ' )->andReturn ($ driver = m::mock ());
110+ $ driver ->shouldReceive ('send ' )->andThrow (new Exception ());
111+ $ events ->shouldReceive ('listen ' )->once ();
112+ $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
113+ $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (NotificationFailed::class));
114+ $ events ->shouldReceive ('dispatch ' )->never ()->with (m::type (NotificationSent::class));
115+
116+ $ manager ->send (new NotificationChannelManagerTestNotifiable , new NotificationChannelManagerTestNotification );
117+ }
118+
119+ public function testNotificationFailedDispatchedOnlyOnceWhenFailed ()
120+ {
121+ $ this ->expectException (Exception::class);
122+
123+ $ container = new Container ;
124+ $ container ->instance ('config ' , ['app.name ' => 'Name ' , 'app.logo ' => 'Logo ' ]);
125+ $ container ->instance (Bus::class, $ bus = m::mock ());
126+ $ container ->instance (Dispatcher::class, $ events = m::mock (Dispatcher::class));
127+ Container::setInstance ($ container );
128+ $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
129+ $ manager ->shouldReceive ('driver ' )->andReturn ($ driver = m::mock ());
130+ $ driver ->shouldReceive ('send ' )->andReturnUsing (function ($ notifiable , $ notification ) use ($ events ) {
131+ $ events ->dispatch (new NotificationFailed ($ notifiable , $ notification , 'test ' ));
132+ throw new Exception ();
133+ });
134+ $ listeners = new Collection ();
135+ $ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
136+ $ events ->shouldReceive ('listen ' )->once ()->andReturnUsing (function ($ event , $ callback ) use ($ listeners ) {
137+ $ listeners ->push ($ callback );
138+ });
139+ $ events ->shouldReceive ('dispatch ' )->once ()->with (m::type (NotificationFailed::class))->andReturnUsing (function ($ event ) use ($ listeners ) {
140+ foreach ($ listeners as $ listener ) {
141+ $ listener ($ event );
142+ }
143+ });
144+ $ events ->shouldReceive ('dispatch ' )->never ()->with (m::type (NotificationSent::class));
145+
146+ $ manager ->send (new NotificationChannelManagerTestNotifiable , new NotificationChannelManagerTestNotification );
147+ }
148+
92149 public function testNotificationCanBeQueued ()
93150 {
94151 $ container = new Container ;
@@ -98,6 +155,7 @@ public function testNotificationCanBeQueued()
98155 $ bus ->shouldReceive ('dispatch ' )->with (m::type (SendQueuedNotifications::class));
99156 Container::setInstance ($ container );
100157 $ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
158+ $ events ->shouldReceive ('listen ' )->once ();
101159
102160 $ manager ->send ([new NotificationChannelManagerTestNotifiable ], new NotificationChannelManagerTestQueuedNotification );
103161 }
0 commit comments