Skip to content

Commit dca7c44

Browse files
committed
feat: allow mails to be intercepted using events
1 parent 49393a4 commit dca7c44

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/Codeception/Lib/Connector/Yii2.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use yii\base\ExitException;
1919
use yii\base\Security;
2020
use yii\base\UserException;
21+
use yii\mail\BaseMailer;
22+
use yii\mail\MailerInterface;
23+
use yii\mail\MailEvent;
2124
use yii\mail\MessageInterface;
2225
use yii\web\Application;
2326
use yii\web\ErrorHandler;
@@ -30,6 +33,19 @@ class Yii2 extends Client
3033
{
3134
use Shared\PhpSuperGlobalsConverter;
3235

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+
3349
const CLEAN_METHODS = [
3450
self::CLEAN_RECREATE,
3551
self::CLEAN_CLEAR,
@@ -64,6 +80,10 @@ class Yii2 extends Client
6480
*/
6581
public $configFile;
6682

83+
/**
84+
* @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_AFTER|self::MAIL_BEFORE $mailMethod method for handling mails
85+
*/
86+
public $mailMethod;
6787
/**
6888
* @var string method for cleaning the response object before each request
6989
*/
@@ -267,7 +287,15 @@ public function startApp(?\yii\log\Logger $logger = null): void
267287
unset($config['container']);
268288
}
269289

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+
}
271299
Yii::$app = Yii::createObject($config);
272300

273301
if ($logger instanceof \yii\log\Logger) {

src/Codeception/Lib/Connector/Yii2/TestMailer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
use Closure;
88
use yii\mail\BaseMailer;
9+
use yii\symfonymailer\Message;
910

1011
class TestMailer extends BaseMailer
1112
{
12-
public $messageClass = \yii\symfonymailer\Message::class;
13+
public $messageClass = Message::class;
1314

1415
public Closure $callback;
1516

src/Codeception/Module/Yii2.php

+12
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
* changes will get discarded.
8787
* * `recreateApplication` - (default: `false`) whether to recreate the whole
8888
* application before each request
89+
* * `mailMethod` - (default: `catch`) Method for handling email via the 'mailer'
90+
* component. `ignore` will not do anything with mail, this means mails are not
91+
* inspectable by the test runner, using `before` or `after` will use mailer
92+
* events; making the mails inspectable but also allowing your default mail
93+
* handling to work
8994
*
9095
* You can use this module by setting params in your `functional.suite.yml`:
9196
*
@@ -187,6 +192,7 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
187192
'requestCleanMethod' => Yii2Connector::CLEAN_RECREATE,
188193
'recreateComponents' => [],
189194
'recreateApplication' => false,
195+
'mailMethod' => Yii2Connector::MAIL_CATCH,
190196
'closeSessionOnRecreateApplication' => true,
191197
'applicationClass' => null,
192198
];
@@ -289,6 +295,12 @@ protected function validateConfig(): void
289295
"The response clean method must be one of: " . $validMethods
290296
);
291297
}
298+
if (!in_array($this->config['mailMethod'], Yii2Connector::MAIL_METHODS, true)) {
299+
throw new ModuleConfigException(
300+
self::class,
301+
"The mail method must be one of: " . $validMethods
302+
);
303+
}
292304
if (!in_array($this->config['requestCleanMethod'], Yii2Connector::CLEAN_METHODS, true)) {
293305
throw new ModuleConfigException(
294306
self::class,

0 commit comments

Comments
 (0)