Skip to content

feat: allow mails to be intercepted using events #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 26, 2025
30 changes: 29 additions & 1 deletion src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use yii\base\ExitException;
use yii\base\Security;
use yii\base\UserException;
use yii\mail\BaseMailer;
use yii\mail\MailerInterface;
use yii\mail\MailEvent;
use yii\mail\MessageInterface;
use yii\web\Application;
use yii\web\ErrorHandler;
Expand All @@ -30,6 +33,19 @@
{
use Shared\PhpSuperGlobalsConverter;

const MAIL_METHODS = [
self::MAIL_CATCH,
self::MAIL_EVENT_AFTER,
self::MAIL_EVENT_BEFORE,
self::MAIL_IGNORE
];

public const MAIL_CATCH = 'catch';
public const MAIL_EVENT_AFTER = 'after';
public const MAIL_EVENT_BEFORE = 'before';
public const MAIL_IGNORE = 'ignore';


const CLEAN_METHODS = [
self::CLEAN_RECREATE,
self::CLEAN_CLEAR,
Expand Down Expand Up @@ -64,6 +80,10 @@
*/
public $configFile;

/**
* @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_AFTER|self::MAIL_BEFORE $mailMethod method for handling mails
*/
public $mailMethod;

Check failure on line 86 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

PHPDoc tag @var for property Codeception\Lib\Connector\Yii2::$mailMethod contains unresolvable type.

Check failure on line 86 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.3)

PHPDoc tag @var for property Codeception\Lib\Connector\Yii2::$mailMethod contains unresolvable type.

Check failure on line 86 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

PHPDoc tag @var for property Codeception\Lib\Connector\Yii2::$mailMethod contains unresolvable type.

Check failure on line 86 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

PHPDoc tag @var for property Codeception\Lib\Connector\Yii2::$mailMethod contains unresolvable type.
/**
* @var string method for cleaning the response object before each request
*/
Expand Down Expand Up @@ -267,7 +287,15 @@
unset($config['container']);
}

$config = $this->mockMailer($config);
match ($this->mailMethod) {

Check failure on line 290 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

Match expression does not handle remaining value: mixed

Check failure on line 290 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.3)

Match expression does not handle remaining value: mixed

Check failure on line 290 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

Match expression does not handle remaining value: mixed

Check failure on line 290 in src/Codeception/Lib/Connector/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Match expression does not handle remaining value: mixed
self::MAIL_CATCH => $config= $this->mockMailer($config),
self::MAIL_EVENT_AFTER => $config['components']['mailer']['on ' . BaseMailer::EVENT_AFTER_SEND] = fn(MailEvent $event) => $this->emails[] = $event->message,
self::MAIL_EVENT_BEFORE => $config['components']['mailer']['on ' . BaseMailer::EVENT_BEFORE_SEND] = function(MailEvent $event) {
$this->emails[] = $event->message;
return true;
},
self::MAIL_IGNORE => null// Do nothing
};
Yii::$app = Yii::createObject($config);

if ($logger instanceof \yii\log\Logger) {
Expand Down
3 changes: 2 additions & 1 deletion src/Codeception/Lib/Connector/Yii2/TestMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Closure;
use yii\mail\BaseMailer;
use yii\symfonymailer\Message;

class TestMailer extends BaseMailer
{
public $messageClass = \yii\symfonymailer\Message::class;
public $messageClass = Message::class;

public Closure $callback;

Expand Down
12 changes: 12 additions & 0 deletions src/Codeception/Module/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
* changes will get discarded.
* * `recreateApplication` - (default: `false`) whether to recreate the whole
* application before each request
* * `mailMethod` - (default: `catch`) Method for handling email via the 'mailer'
* component. `ignore` will not do anything with mail, this means mails are not
* inspectable by the test runner, using `before` or `after` will use mailer
* events; making the mails inspectable but also allowing your default mail
* handling to work
*
* You can use this module by setting params in your `functional.suite.yml`:
*
Expand Down Expand Up @@ -187,6 +192,7 @@
'requestCleanMethod' => Yii2Connector::CLEAN_RECREATE,
'recreateComponents' => [],
'recreateApplication' => false,
'mailMethod' => Yii2Connector::MAIL_CATCH,
'closeSessionOnRecreateApplication' => true,
'applicationClass' => null,
];
Expand Down Expand Up @@ -289,6 +295,12 @@
"The response clean method must be one of: " . $validMethods
);
}
if (!in_array($this->config['mailMethod'], Yii2Connector::MAIL_METHODS, true)) {
throw new ModuleConfigException(
self::class,
"The mail method must be one of: " . $validMethods
);
}
if (!in_array($this->config['requestCleanMethod'], Yii2Connector::CLEAN_METHODS, true)) {
throw new ModuleConfigException(
self::class,
Expand Down Expand Up @@ -848,7 +860,7 @@
return [
'clientContext' => $this->getClient()->getContext(),
'headers' => $this->headers,
'cookie' => $_COOKIE ?? [],

Check failure on line 863 in src/Codeception/Module/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

Variable $_COOKIE on left side of ?? always exists and is not nullable.

Check failure on line 863 in src/Codeception/Module/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.3)

Variable $_COOKIE on left side of ?? always exists and is not nullable.

Check failure on line 863 in src/Codeception/Module/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8)

Variable $_COOKIE on left side of ?? always exists and is not nullable.

Check failure on line 863 in src/Codeception/Module/Yii2.php

View workflow job for this annotation

GitHub Actions / tests (8.4)

Variable $_COOKIE on left side of ?? always exists and is not nullable.
'session' => $_SESSION ?? [],
];
}
Expand Down
Loading