Skip to content

Make TestMailer proxying #117

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use yii\base\Security;
use yii\base\UserException;
use yii\mail\MessageInterface;
use yii\symfonymailer\Mailer;
use yii\web\Application;
use yii\web\ErrorHandler;
use yii\web\IdentityInterface;
Expand Down Expand Up @@ -422,33 +423,20 @@ protected function encodeCookies(
*/
protected function mockMailer(array $config): array
{
// options that make sense for mailer mock
$allowedOptions = [
'htmlLayout',
'textLayout',
'messageConfig',
'messageClass',
'useFileTransport',
'fileTransportPath',
'fileTransportCallback',
'view',
'viewPath',
];

$mailerConfig = [
'class' => TestMailer::class,
'callback' => function (MessageInterface $message) {
$this->emails[] = $message;
}
},
'mailer' => [
'class' => Mailer::class,
]
];

if (isset($config['components']['mailer']) && is_array($config['components']['mailer'])) {
foreach ($config['components']['mailer'] as $name => $value) {
if (in_array($name, $allowedOptions, true)) {
$mailerConfig[$name] = $value;
}
}
if (isset($config['components']['mailer'])) {
$mailerConfig['mailer'] = $config['components']['mailer'];
}

$config['components']['mailer'] = $mailerConfig;

return $config;
Expand Down
73 changes: 70 additions & 3 deletions src/Codeception/Lib/Connector/Yii2/TestMailer.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,90 @@
<?php
namespace Codeception\Lib\Connector\Yii2;

use yii\base\UnknownMethodException;
use yii\base\UnknownPropertyException;
use yii\di\Instance;
use yii\mail\BaseMailer;
use yii\mail\MailerInterface;

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

/**
* @var \Closure
*/
public $callback;

/**
* @var string|array|MailerInterface Mailer config or component to send mail out in the end
*/
public $mailer = 'mailer';

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
$this->mailer = Instance::ensure($this->mailer, MailerInterface::class);
}

/**
* @param string $name
* @param array $params
* @return mixed
*/
public function __call($name, $params)
{
try {
return parent::__call($name, $params);
} catch (UnknownMethodException $e) {
return call_user_func_array([$this->mailer, $name], $params);
}
}

/**
* @param string $name
* @return mixed
*/
public function __get($name)
{
try {
return parent::__get($name);
} catch (UnknownPropertyException $e) {
return $this->mailer->{$name};
}
}

/**
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
try {
parent::__set($name, $value);
} catch (UnknownPropertyException $e) {
$this->mailer->{$name} = $value;
}
}

/**
* @inheritdoc
*/
public function compose($view = null, array $params = [])
{
$message = $this->mailer->compose($view, $params);
$message->mailer = $this;
return $message;
}

protected function sendMessage($message)
{
call_user_func($this->callback, $message);
return true;
}

protected function saveMessage($message)
{
call_user_func($this->callback, $message);
Expand Down