7
7
use Illuminate \Foundation \Queue \OnQueue ;
8
8
use Illuminate \Mail \Mailable ;
9
9
use Illuminate \Mail \SendQueuedMailable ;
10
+ use Illuminate \Queue \Events \JobProcessing ;
10
11
use Illuminate \Support \Facades \Mail ;
11
12
use Illuminate \Support \Facades \Queue ;
12
13
use Orchestra \Testbench \TestCase ;
14
+ use PHPUnit \Framework \Attributes \DataProvider ;
13
15
use PHPUnit \Framework \Attributes \TestWith ;
14
16
15
17
class SupportMailTest extends TestCase
16
18
{
17
19
public function testItRegisterAndCallMacros ()
18
20
{
19
- Mail::macro ('test ' , fn (string $ str ) => $ str === 'foo '
21
+ Mail::macro ('test ' , fn (string $ str ) => $ str === 'foo '
20
22
? 'it works! '
21
23
: 'it failed. ' ,
22
24
);
@@ -26,7 +28,7 @@ public function testItRegisterAndCallMacros()
26
28
27
29
public function testItRegisterAndCallMacrosWhenFaked ()
28
30
{
29
- Mail::macro ('test ' , fn (string $ str ) => $ str === 'foo '
31
+ Mail::macro ('test ' , fn (string $ str ) => $ str === 'foo '
30
32
? 'it works! '
31
33
: 'it failed. ' ,
32
34
);
@@ -46,9 +48,8 @@ public function testEmailSent()
46
48
Mail::assertSent (TestMail::class);
47
49
}
48
50
49
- #[TestWith([TestMailWithOnQueue::class, 'my-queue ' ], 'string for queue ' )]
50
- #[TestWith([TestMailWithEnumOnQueue::class, 'queue-from-enum ' ], 'enum for queue ' )]
51
- public function testQueuedMailableRespectsStringOnQueueAttribute (string $ mailableClass , string $ queueName )
51
+ #[DataProvider('queueDataProvider ' )]
52
+ public function testQueuedMailableRespectsOnQueueAttribute (string $ mailableClass , string $ queueName )
52
53
{
53
54
Queue::fake ();
54
55
@@ -61,17 +62,87 @@ public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailabl
61
62
);
62
63
}
63
64
64
- public function testQueueableMailableUsesQueueIfSetAsProperty ()
65
+ #[DataProvider('queueDataProvider ' )]
66
+ public function testQueuedMailableDispatchedLaterRespectsOnQueueAttribute (string $ mailableClass , string $ queueName )
67
+ {
68
+ Queue::fake ();
69
+
70
+ Mail::later (100 , new $ mailableClass );
71
+
72
+ Queue::assertPushedOn (
73
+ $ queueName ,
74
+ SendQueuedMailable::class,
75
+ fn ($ job ) => is_a ($ job ->mailable , $ mailableClass , true )
76
+ );
77
+ }
78
+
79
+ #[DataProvider('connectionDataProvider ' )]
80
+ public function testQueuedMailableRespectsOnConnectionAttribute (string $ mailableClass , string $ connectionName )
81
+ {
82
+ Queue::fake ();
83
+
84
+ Queue::before (function (JobProcessing $ jobProcessing ) use ($ connectionName ) {
85
+ $ this ->assertEquals ($ connectionName , $ jobProcessing ->connectionName );
86
+ });
87
+
88
+ Mail::send (new $ mailableClass ());
89
+
90
+ }
91
+
92
+ #[DataProvider('connectionDataProvider ' )]
93
+ public function testLaterQueuedMailableRespectsOnConnectionAttribute (string $ mailableClass , string $ connectionName )
94
+ {
95
+ Queue::fake ();
96
+
97
+ Queue::before (function (JobProcessing $ jobProcessing ) use ($ connectionName ) {
98
+ $ this ->assertEquals ($ connectionName , $ jobProcessing ->connectionName );
99
+ });
100
+
101
+ Mail::later (100 , new $ mailableClass ());
102
+ }
103
+
104
+ public function testQueueableMailableUsesQueueAndConnectionFromClassProperties ()
65
105
{
66
106
Queue::fake ();
67
107
Mail::send (new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet ());
68
108
69
- Queue::assertPushed (function (SendQueuedMailable $ sendQueuedMailable ) {
109
+ Queue::assertPushed (function (SendQueuedMailable $ sendQueuedMailable ) {
110
+ return $ sendQueuedMailable ->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
111
+ && $ sendQueuedMailable ->connection === 'my-connection '
112
+ && $ sendQueuedMailable ->queue === 'some-other-queue ' ;
113
+ });
114
+ }
115
+
116
+ public function testQueueableMailableDispatchedLaterUsesQueueAndConnectionFromClassProperties ()
117
+ {
118
+ Queue::fake ();
119
+ Mail::later (100 , new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet ());
120
+
121
+ Queue::assertPushed (function (SendQueuedMailable $ sendQueuedMailable ) {
70
122
return $ sendQueuedMailable ->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
71
123
&& $ sendQueuedMailable ->connection === 'my-connection '
72
124
&& $ sendQueuedMailable ->queue === 'some-other-queue ' ;
73
125
});
74
126
}
127
+
128
+ /**
129
+ * @return array<string, array{class-string<Mailable>, string}>
130
+ */
131
+ public static function queueDataProvider (): array
132
+ {
133
+ return [
134
+ 'string for queue ' => [TestMailWithOnQueue::class, 'my-queue ' ],
135
+ 'enum for queue ' => [TestMailWithEnumOnQueue::class, 'queue-from-enum ' ],
136
+ ];
137
+ }
138
+
139
+ public static function connectionDataProvider (): array
140
+ {
141
+ return [
142
+ 'string for connection ' => [TestMailWithOnConnection::class, 'connection-string ' ],
143
+ 'enum for connection ' => [TestMailWithEnumOnConnection::class, 'connection-from-enum ' ],
144
+ ];
145
+ }
75
146
}
76
147
77
148
class TestMail extends Mailable
@@ -97,6 +168,29 @@ class TestMailWithEnumOnQueue extends Mailable implements ShouldQueue
97
168
{
98
169
}
99
170
171
+ #[OnConnection('connection-string ' )]
172
+ class TestMailWithOnConnection extends Mailable implements ShouldQueue
173
+ {
174
+ public $ queue = 'queue ' ;
175
+
176
+ public function build ()
177
+ {
178
+ return $ this ->view ('view ' );
179
+ }
180
+ }
181
+
182
+ #[OnConnection(SupportMailTestEnum::Connection)]
183
+ class TestMailWithEnumOnConnection extends Mailable implements ShouldQueue
184
+ {
185
+ public $ queue = 'queue ' ;
186
+
187
+ public function build ()
188
+ {
189
+ return $ this ->view ('view ' );
190
+ }
191
+ }
192
+
193
+
100
194
#[OnQueue(SupportMailTestEnum::Queue)]
101
195
#[OnConnection(SupportMailTestEnum::Connection)]
102
196
class TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet extends Mailable implements ShouldQueue
0 commit comments