我对我的端点(api动作) postLeadAction进行了功能测试,在刷新新实体之后,我发送电子邮件表示祝贺。我发送电子邮件与帮助SwiftMailer与运输sendGrid。以及如何在发送电子邮件之前检查sebject、fromName、fromEmail、toEmail。现在,我使用-env= test运行测试,并在配置快速邮件程序中为测试环境添加用于目录的发送电子邮件文件的假脱机参数,而不是发送电子邮件。
如何模拟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类,电子邮件发送与SwiftMailer函数‘发送’
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');发布于 2016-04-20 11:59:15
我用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标记为不完整,而不是返回错误和破坏测试套件。
https://stackoverflow.com/questions/36737791
复制相似问题