2121use yii \base \ExitException ;
2222use yii \base \Security ;
2323use yii \base \UserException ;
24- use yii \mail \BaseMessage ;
24+ use yii \mail \BaseMailer ;
25+ use yii \mail \MailEvent ;
26+ use yii \mail \MessageInterface ;
2527use yii \web \Application ;
2628use yii \web \IdentityInterface ;
2729use yii \web \Request as YiiRequest ;
@@ -36,20 +38,21 @@ class Yii2 extends Client
3638{
3739 use Shared \PhpSuperGlobalsConverter;
3840
39- const MAIL_METHODS = [
41+
42+ public const array MAIL_METHODS = [
4043 self ::MAIL_CATCH ,
4144 self ::MAIL_EVENT_AFTER ,
4245 self ::MAIL_EVENT_BEFORE ,
4346 self ::MAIL_IGNORE
4447 ];
4548
46- public const MAIL_CATCH = 'catch ' ;
47- public const MAIL_EVENT_AFTER = 'after ' ;
48- public const MAIL_EVENT_BEFORE = 'before ' ;
49- public const MAIL_IGNORE = 'ignore ' ;
49+ public const string MAIL_CATCH = 'catch ' ;
50+ public const string MAIL_EVENT_AFTER = 'after ' ;
51+ public const string MAIL_EVENT_BEFORE = 'before ' ;
52+ public const string MAIL_IGNORE = 'ignore ' ;
5053
5154
52- const CLEAN_METHODS = [
55+ const array CLEAN_METHODS = [
5356 self ::CLEAN_RECREATE ,
5457 self ::CLEAN_CLEAR ,
5558 self ::CLEAN_FORCE_RECREATE ,
@@ -59,55 +62,55 @@ class Yii2 extends Client
5962 * Clean the response object by recreating it.
6063 * This might lose behaviors / event handlers / other changes that are done in the application bootstrap phase.
6164 */
62- const CLEAN_RECREATE = 'recreate ' ;
65+ const string CLEAN_RECREATE = 'recreate ' ;
6366 /**
6467 * Same as recreate but will not warn when behaviors / event handlers are lost.
6568 */
66- const CLEAN_FORCE_RECREATE = 'force_recreate ' ;
69+ const string CLEAN_FORCE_RECREATE = 'force_recreate ' ;
6770 /**
6871 * Clean the response object by resetting specific properties via its' `clear()` method.
6972 * This will keep behaviors / event handlers, but could inadvertently leave some changes intact.
7073 * @see \yii\web\Response::clear()
7174 */
72- const CLEAN_CLEAR = 'clear ' ;
75+ const string CLEAN_CLEAR = 'clear ' ;
7376
7477 /**
7578 * Do not clean the response, instead the test writer will be responsible for manually resetting the response in
7679 * between requests during one test
7780 */
78- const CLEAN_MANUAL = 'manual ' ;
81+ const string CLEAN_MANUAL = 'manual ' ;
7982
8083
8184 /**
8285 * @var string application config file
8386 */
84- public $ configFile ;
87+ public string $ configFile ;
8588
8689 /**
87- * @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_AFTER |self::MAIL_BEFORE $mailMethod method for handling mails
90+ * @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_EVENT_AFTER |self::MAIL_EVENT_BEFORE method for handling mails
8891 */
89- public $ mailMethod ;
92+ public string $ mailMethod ;
9093 /**
9194 * @var string method for cleaning the response object before each request
9295 */
93- public $ responseCleanMethod ;
96+ public string $ responseCleanMethod ;
9497
9598 /**
9699 * @var string method for cleaning the request object before each request
97100 */
98- public $ requestCleanMethod ;
101+ public string $ requestCleanMethod ;
99102
100103 /**
101104 * @var string[] List of component names that must be recreated before each request
102105 */
103- public $ recreateComponents = [];
106+ public array $ recreateComponents = [];
104107
105108 /**
106109 * This option is there primarily for backwards compatibility.
107110 * It means you cannot make any modification to application state inside your app, since they will get discarded.
108111 * @var bool whether to recreate the whole application before each request
109112 */
110- public $ recreateApplication = false ;
113+ public bool $ recreateApplication = false ;
111114
112115 /**
113116 * @var bool whether to close the session in between requests inside a single test, if recreateApplication is set to true
@@ -122,7 +125,7 @@ class Yii2 extends Client
122125
123126
124127 /**
125- * @var list<BaseMessage >
128+ * @var list<MessageInterface >
126129 */
127130 private array $ emails = [];
128131
@@ -224,7 +227,7 @@ public function getInternalDomains(): array
224227
225228 /**
226229 * @internal
227- * @return list<BaseMessage > List of sent emails
230+ * @return list<MessageInterface > List of sent emails
228231 */
229232 public function getEmails (): array
230233 {
@@ -303,7 +306,13 @@ public function startApp(?\yii\log\Logger $logger = null): void
303306 unset($ config ['container ' ]);
304307 }
305308
306- $ config = $ this ->mockMailer ($ config );
309+ match ($ this ->mailMethod ) {
310+ self ::MAIL_CATCH => $ config = $ this ->mockMailer ($ config ),
311+ self ::MAIL_EVENT_AFTER => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_AFTER_SEND ] = fn (MailEvent $ event ) => $ this ->emails [] = $ event ->message ,
312+ self ::MAIL_EVENT_BEFORE => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_BEFORE_SEND ] = fn (MailEvent $ event ) => $ this ->emails [] = $ event ->message ,
313+ self ::MAIL_IGNORE => null // Do nothing
314+ };
315+
307316 $ app = Yii::createObject ($ config );
308317 if (!$ app instanceof \yii \base \Application) {
309318 throw new ModuleConfigException ($ this , "Failed to initialize Yii2 app " );
@@ -468,7 +477,7 @@ protected function mockMailer(array $config): array
468477
469478 $ mailerConfig = [
470479 'class ' => TestMailer::class,
471- 'callback ' => function (BaseMessage $ message ): void {
480+ 'callback ' => function (MessageInterface $ message ): void {
472481 $ this ->emails [] = $ message ;
473482 }
474483 ];
0 commit comments