18
18
use yii \base \ExitException ;
19
19
use yii \base \Security ;
20
20
use yii \base \UserException ;
21
+ use yii \mail \BaseMailer ;
22
+ use yii \mail \MailerInterface ;
23
+ use yii \mail \MailEvent ;
21
24
use yii \mail \MessageInterface ;
22
25
use yii \web \Application ;
23
26
use yii \web \ErrorHandler ;
@@ -30,6 +33,19 @@ class Yii2 extends Client
30
33
{
31
34
use Shared \PhpSuperGlobalsConverter;
32
35
36
+ const MAIL_METHODS = [
37
+ self ::MAIL_CATCH ,
38
+ self ::MAIL_EVENT_AFTER ,
39
+ self ::MAIL_EVENT_BEFORE ,
40
+ self ::MAIL_IGNORE
41
+ ];
42
+
43
+ public const MAIL_CATCH = 'catch ' ;
44
+ public const MAIL_EVENT_AFTER = 'after ' ;
45
+ public const MAIL_EVENT_BEFORE = 'before ' ;
46
+ public const MAIL_IGNORE = 'ignore ' ;
47
+
48
+
33
49
const CLEAN_METHODS = [
34
50
self ::CLEAN_RECREATE ,
35
51
self ::CLEAN_CLEAR ,
@@ -64,6 +80,10 @@ class Yii2 extends Client
64
80
*/
65
81
public $ configFile ;
66
82
83
+ /**
84
+ * @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_AFTER|self::MAIL_BEFORE $mailMethod method for handling mails
85
+ */
86
+ public $ mailMethod ;
67
87
/**
68
88
* @var string method for cleaning the response object before each request
69
89
*/
@@ -267,7 +287,15 @@ public function startApp(?\yii\log\Logger $logger = null): void
267
287
unset($ config ['container ' ]);
268
288
}
269
289
270
- $ config = $ this ->mockMailer ($ config );
290
+ match ($ this ->mailMethod ) {
291
+ self ::MAIL_CATCH => $ config = $ this ->mockMailer ($ config ),
292
+ self ::MAIL_EVENT_AFTER => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_AFTER_SEND ] = fn (MailEvent $ event ) => $ this ->emails [] = $ event ->message ,
293
+ self ::MAIL_EVENT_BEFORE => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_BEFORE_SEND ] = function (MailEvent $ event ) {
294
+ $ this ->emails [] = $ event ->message ;
295
+ return true ;
296
+ },
297
+ self ::MAIL_IGNORE => null // Do nothing
298
+ }
271
299
Yii::$ app = Yii::createObject ($ config );
272
300
273
301
if ($ logger instanceof \yii \log \Logger) {
0 commit comments