Symfony PHPUnit 模拟 SwiftMailer
Symfony PHPUnit mock SwiftMailer
我对我的端点(api 操作)postLeadAction 进行了功能测试,在刷新新实体后,我发送电子邮件表示祝贺。我使用带有传输 sendGrid 的 SwiftMailer 帮助发送电子邮件。以及如何在发送电子邮件之前检查 sebject、fromName、fromEmail、toEmail。现在我 运行 使用 --env=test 进行测试,并在测试环境的配置 swiftmailer 中添加假脱机参数以将电子邮件文件放入目录而不是发送电子邮件
如何模拟 swiftMailer 并在发送电子邮件之前检查参数?
这是我的config_test.yml
swiftmailer:
default_mailer: default
mailers:
default:
transport: %mailer_transport%
host: '%mailer_host%'
port: 587
encryption: ~
username: '%mailer_user%'
password: '%mailer_password%'
spool:
type: file
path: '%kernel.root_dir%/spool'
此 MailerWrapper class,使用 SwiftMailer 功能发送电子邮件 'send'
class MailerWrapper
{
protected $mailer;
/**
* @var \Swift_Message
*/
//some parameters
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function newMessage()
{
//some parameters
return $this;
}
public function send()
{
//some logic with message
return $this->mailer->send($this->message);
}
我喜欢食谱
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer.mailer.default');
// Check that an email was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
但有错误
PHP Fatal error: Call to a member function getCollector() on a non-object
更新
在配置中我没有启用配置文件
framework:
test: ~
session:
storage_id: session.storage.mock_file
cookie_httponly: true
cookie_secure: true
profiler:
collect: false
但我有错误,因为我在 http 请求之后启用了配置文件,当我之前启用时 - 一切正常
$client->enableProfiler();
$this->request(
'post',
$this->generateUrl('post_lead', [], UrlGeneratorInterface::RELATIVE_PATH),
[],
[
// some parameters
]
);
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
我使用 Symfony 2.8 进行了测试,我必须在配置中启用探查器:
# app/config_test.yml
framework:
profiler:
enabled: true
collect: false
您的测试应该在定义 enabled: true
后运行。
为了避免在我的测试中出现 PHP 致命错误,我在测试电子邮件之前添加了一个小检查:
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();
// Check that the profiler is available.
if ($profile = $this->client->getProfile()) {
$mailCollector = $profile->getCollector('swiftmailer');
// Check that an e-mail was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
// …
}
else {
$this->markTestIncomplete(
'Profiler is disabled.'
);
}
通过此检查,测试将被 PHPUnit 标记为不完整,而不是返回错误并破坏测试套件。
我对我的端点(api 操作)postLeadAction 进行了功能测试,在刷新新实体后,我发送电子邮件表示祝贺。我使用带有传输 sendGrid 的 SwiftMailer 帮助发送电子邮件。以及如何在发送电子邮件之前检查 sebject、fromName、fromEmail、toEmail。现在我 运行 使用 --env=test 进行测试,并在测试环境的配置 swiftmailer 中添加假脱机参数以将电子邮件文件放入目录而不是发送电子邮件
如何模拟 swiftMailer 并在发送电子邮件之前检查参数?
这是我的config_test.yml
swiftmailer:
default_mailer: default
mailers:
default:
transport: %mailer_transport%
host: '%mailer_host%'
port: 587
encryption: ~
username: '%mailer_user%'
password: '%mailer_password%'
spool:
type: file
path: '%kernel.root_dir%/spool'
此 MailerWrapper class,使用 SwiftMailer 功能发送电子邮件 'send'
class MailerWrapper
{
protected $mailer;
/**
* @var \Swift_Message
*/
//some parameters
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function newMessage()
{
//some parameters
return $this;
}
public function send()
{
//some logic with message
return $this->mailer->send($this->message);
}
我喜欢食谱
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer.mailer.default');
// Check that an email was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
但有错误
PHP Fatal error: Call to a member function getCollector() on a non-object
更新
在配置中我没有启用配置文件
framework:
test: ~
session:
storage_id: session.storage.mock_file
cookie_httponly: true
cookie_secure: true
profiler:
collect: false
但我有错误,因为我在 http 请求之后启用了配置文件,当我之前启用时 - 一切正常
$client->enableProfiler();
$this->request(
'post',
$this->generateUrl('post_lead', [], UrlGeneratorInterface::RELATIVE_PATH),
[],
[
// some parameters
]
);
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
我使用 Symfony 2.8 进行了测试,我必须在配置中启用探查器:
# app/config_test.yml
framework:
profiler:
enabled: true
collect: false
您的测试应该在定义 enabled: true
后运行。
为了避免在我的测试中出现 PHP 致命错误,我在测试电子邮件之前添加了一个小检查:
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();
// Check that the profiler is available.
if ($profile = $this->client->getProfile()) {
$mailCollector = $profile->getCollector('swiftmailer');
// Check that an e-mail was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
// …
}
else {
$this->markTestIncomplete(
'Profiler is disabled.'
);
}
通过此检查,测试将被 PHPUnit 标记为不完整,而不是返回错误并破坏测试套件。