2
2
3
3
namespace Illuminate \Tests \Notifications ;
4
4
5
+ use Exception ;
5
6
use Illuminate \Bus \Queueable ;
6
7
use Illuminate \Container \Container ;
7
8
use Illuminate \Contracts \Bus \Dispatcher as Bus ;
8
9
use Illuminate \Contracts \Events \Dispatcher ;
9
10
use Illuminate \Contracts \Queue \ShouldQueue ;
10
11
use Illuminate \Notifications \ChannelManager ;
12
+ use Illuminate \Notifications \Events \NotificationFailed ;
11
13
use Illuminate \Notifications \Events \NotificationSending ;
12
14
use Illuminate \Notifications \Events \NotificationSent ;
13
15
use Illuminate \Notifications \Notifiable ;
14
16
use Illuminate \Notifications \Notification ;
15
17
use Illuminate \Notifications \SendQueuedNotifications ;
18
+ use Illuminate \Support \Collection ;
16
19
use Mockery as m ;
17
20
use PHPUnit \Framework \TestCase ;
18
21
@@ -34,6 +37,7 @@ public function testNotificationCanBeDispatchedToDriver()
34
37
Container::setInstance ($ container );
35
38
$ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
36
39
$ manager ->shouldReceive ('driver ' )->andReturn ($ driver = m::mock ());
40
+ $ events ->shouldReceive ('listen ' )->once ();
37
41
$ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
38
42
<
10000
span class=pl-c1>$ driver->shouldReceive ('send ' )->once ();
39
43
$ events ->shouldReceive ('dispatch ' )->with (m::type (NotificationSent::class));
@@ -49,6 +53,7 @@ public function testNotificationNotSentOnHalt()
49
53
$ container ->instance (Dispatcher::class, $ events = m::mock ());
50
54
Container::setInstance ($ container );
51
55
$ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
56
+ $ events ->shouldReceive ('listen ' )->once ();
52
57
$ events ->shouldReceive ('until ' )->once ()->with (m::type (NotificationSending::class))->andReturn (false );
53
58
$ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
54
59
$ manager ->shouldReceive ('driver ' )->once ()->andReturn ($ driver = m::mock ());
@@ -66,6 +71,7 @@ public function testNotificationNotSentWhenCancelled()
66
71
$ container ->instance (Dispatcher::class, $ events = m::mock ());
67
72
Container::setInstance ($ container );
68
73
$ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
74
+ $ events ->shouldReceive ('listen ' )->once ();
69
75
$ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
70
76
$ manager ->shouldNotReceive ('driver ' );
71
77
$ events ->shouldNotReceive ('dispatch ' );
@@ -81,6 +87,7 @@ public function testNotificationSentWhenNotCancelled()
81
87
$ container ->instance (Dispatcher::class, $ events = m::mock ());
82
88
Container::setInstance ($ container );
83
89
$ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
90
+ $ events ->shouldReceive ('listen ' )->once ();
84
91
$ events ->shouldReceive ('until ' )->with (m::type (NotificationSending::class))->andReturn (true );
85
92
$ manager ->shouldReceive ('driver ' )->once ()->andReturn ($ driver = m::mock ());
86
93
$ driver ->shouldReceive ('send ' )->once ();
@@ -89,6 +96,56 @@ public function testNotificationSentWhenNotCancelled()
89
96
$ manager ->send ([new NotificationChannelManagerTestNotifiable ], new NotificationChannelManagerTestNotCancelledNotification );
90
97
}
91
98
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
+
92
149
public function testNotificationCanBeQueued ()
93
150
{
94
151
$ container = new Container ;
@@ -98,6 +155,7 @@ public function testNotificationCanBeQueued()
98
155
$ bus ->shouldReceive ('dispatch ' )->with (m::type (SendQueuedNotifications::class));
99
156
Container::setInstance ($ container );
100
157
$ manager = m::mock (ChannelManager::class.'[driver] ' , [$ container ]);
158
+ $ events ->shouldReceive ('listen ' )->once ();
101
159
102
160
$ manager ->send ([new NotificationChannelManagerTestNotifiable ], new NotificationChannelManagerTestQueuedNotification );
103
161
}
0 commit comments