@@ -61,6 +61,49 @@ public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables(
61
61
62
62
$ sender ->sendNow ($ notifiable , new DummyNotificationWithDatabaseVia );
63
63
}
64
+
65
+ public function testItCanSendQueuedNotificationsThroughMiddleware ()
66
+ {
67
+ $ notifiable = m::mock (Notifiable::class);
68
+ $ manager = m::mock (ChannelManager::class);
69
+ $ bus = m::mock (BusDispatcher::class);
70
+ $ bus ->shouldReceive ('dispatch ' )
71
+ ->withArgs (function ($ job ) {
72
+ return $ job ->middleware [0 ] instanceof TestNotificationMiddleware;
73
+ });
74
+ $ events = m::mock (EventDispatcher::class);
75
+
76
+ $ sender = new NotificationSender ($ manager , $ bus , $ events );
77
+
78
+ $ sender ->send ($ notifiable , new DummyNotificationWithMiddleware );
79
+ }
80
+
81
+ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMiddleware ()
82
+ {
83
+ $ notifiable = m::mock (Notifiable::class);
84
+ $ manager = m::mock (ChannelManager::class);
85
+ $ bus = m::mock (BusDispatcher::class);
86
+ $ bus ->shouldReceive ('dispatch ' )
87
+ ->once ()
88
+ ->withArgs (function ($ job ) {
89
+ return $ job ->middleware [0 ] instanceof TestMailNotificationMiddleware;
90
+ });
91
+ $ bus ->shouldReceive ('dispatch ' )
92
+ ->once ()
93
+ ->withArgs (function ($ job ) {
94
+ return $ job ->middleware [0 ] instanceof TestDatabaseNotificationMiddleware;
95
+ });
96
+ $ bus ->shouldReceive ('dispatch ' )
97
+ ->once ()
98
+ ->withArgs (function ($ job ) {
99
+ return empty ($ job ->middleware );
100
+ });
101
+ $ events = m::mock (EventDispatcher::class);
102
+
103
+ $ sender = new NotificationSender ($ manager , $ bus , $ events );
104
+
105
+ $ sender ->send ($ notifiable , new DummyMultiChannelNotificationWithConditionalMiddlware );
106
+ }
64
107
}
65
108
66
109
class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
@@ -110,3 +153,67 @@ public function via($notifiable)
110
153
return 'database ' ;
111
154
}
112
155
}
156
+
157
+ class DummyNotificationWithMiddleware extends Notification implements ShouldQueue
158
+ {
159
+ use Queueable;
160
+
161
+ public function via ($ notifiable )
162
+ {
163
+ return 'mail ' ;
164
+ }
165
+
166
+ public function middleware ()
167
+ {
168
+ return [
169
+ new TestNotificationMiddleware ,
170
+ ];
171
+ }
172
+ }
173
+
174
+ class DummyMultiChannelNotificationWithConditionalMiddlware extends Notification implements ShouldQueue
175
+ {
176
+ use Queueable;
177
+
178
+ public function via ($ notifiable )
179
+ {
180
+ return [
181
+ 'mail ' ,
182
+ 'database ' ,
183
+ 'broadcast ' ,
184
+ ];
185
+ }
186
+
187
+ public function middleware ($ notifiable , $ channel )
188
+ {
189
+ return match ($ channel ) {
190
+ 'mail ' => [new TestMailNotificationMiddleware ],
191
+ 'database ' => [new TestDatabaseNotificationMiddleware ],
192
+ default => []
193
+ };
194
+ }
195
+ }
196
+
197
+ class TestNotificationMiddleware
198
+ {
199
+ public function handle ($ command , $ next )
200
+ {
201
+ return $ next ($ command );
202
+ }
203
+ }
204
+
205
+ class TestMailNotificationMiddleware
206
+ {
207
+ public function handle ($ command , $ next )
208
+ {
209
+ return $ next ($ command );
210
+ }
211
+ }
212
+
213
+ class TestDatabaseNotificationMiddleware
214
+ {
215
+ public function handle ($ command , $ next )
216
+ {
217
+ return $ next ($ command );
218
+ }
219
+ }
0 commit comments