-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Use custom Mailer class for running tests #14718
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
Comments
I have the same problem Codeception/Codeception#4011 when running the functional case, DavertMik mark it not a problem . so I temporary add the class in the $allowedOptions to use my own mailer class. |
@bologer, you can use something like this |
@bologer you can try to redefine the malier's class before the test case start or you can new a extention file to listen the case start . http://codeception.com/docs/08-Customization class MyExtension extends \Codeception\Extension
}` |
@JunelanMe this is nice, but it is kind of a work around. @cebe , @samdark is there solutiuon Yii-like way to define custom mailing class for tests only? |
@JunelanMe there is no need in both two lines in beforeTest method, each of the line gives the expected result but in a different way. |
@StalkAlex, alright, when I try to make the following: public function __before() {
Yii::$app->set('mailer', 'common\models\mailer\Mailer');
} I get the following error:
I get the same error message if I put this: Yii::$app->set('mailer', 'common\models\mailer\Mailer'); Inside of the test methods that use mailing class. I tried it like this: Yii::$app->set('mailer', ['class' => 'common\models\mailer\Mailer']) Still get the same error. Any idea what is wrong with it? |
@bologer just checked by myself, here's green test:
Exception says that there is a problem with you configuration. In yii module there are different parts that can be included: init, orm, email and fixtures. Check those. Also I'm using last Codeception and Yii2 versions. P.S. Are you using |
@StalkAlex, thank you very much! It worked. |
hello |
The most correct solution at the moment is to use Yii2’s container to override $config['container']['definitions'][\Codeception\Lib\Connector\Yii2\TestMailer::class] = \app\components\mailer\CustomMailer::class; Then, in your custom mailer class, you should add the following methods: public $callback;
protected function sendMessage($message)
{
if (YII_ENV_TEST) {
call_user_func($this->callback, $message);
return true;
}
return parent::sendMessage($message);
}
protected function saveMessage($message)
{
if (YII_ENV_TEST) {
call_user_func($this->callback, $message);
return true;
}
return parent::saveMessage($message);
} With this approach, when running tests ( In my case, this even allows me to properly test a custom Hope this helps! 🚀 |
@Eseperio could you also test Codeception/module-yii2#120 |
I cannot try on my current project due to it not being still updated to latest php version. But i´ve checked the code and it looks like a really much clean solution. |
What steps will reproduce the problem?
I'm overriding Mailer class with my own and use it to create queue of emails.
When I run tests, it seems to be using another class (Codeception\Lib\Connector\Yii2\TestMailer).
What is the expected result?
Use custom Mailing class.
What do you get instead?
Exception with message for each test where mailing class with custom method
myOwnMathod()
is used:Where
myOwnMethod()
is defined in my overriten mailer class, but as in tests it is using\yii\swiftmailer\Mailer
, which certainly is missing that method.Is there a way to set-up custom mailing class for tests?
The text was updated successfully, but these errors were encountered: