【发布时间】:2018-04-17 17:55:34
【问题描述】:
我已经根据本教程定义了一个简单的电子邮件控制器:
https://symfony.com/doc/3.4/email.html
所以php文件代码是:
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpKernel\Tests\Controller;
class SendEmailController extends Controller
{
public function indexAction($originlEmail, $destinationEmail1, $destinationEmail2, $name, \Swift_Mailer $mailer)
{
$message = (new \Swift_Message('Email Title'))
->setFrom($originlEmail)
->setTo($destinationEmail1, $destinationEmail2)
->setBody(
$this->renderView(
'emails/send-email.html.twig',
array('name' => $name)
),
'text/html'
);
$mailer->send($message);
return $this->render(...);
}
}
.twig 模板代码是:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<h3>This is an email!</h3>
<p>Hi {{ name }}, this is an email! </p>
</body>
</html>
现在,按照本教程 https://symfony.com/doc/3.4/email/testing.html 我创建了一个 PhpUnit 测试类:
<?php
namespace tests\AppBundle\Controller;
use AppBundle\Controller\SendMyEmailController;
class SendCustomerEmailControllerTest extends PHPUnit_Framework_TestCase
{
public function testMailIsSentAndContentIsCorrect()
{
$client = static:: createClient();
$client->enableProfiler();
$crawler = $client->request('POST', 'path/to/above/action');
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
$this->assertSame(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
$this->assertInstanceOf('Swift_Message', $message);
$this->assertSame('My Email Title', $message->getSubject());
$this->assertSame($originEmail, key($message->getFrom()));
$this->assertSame($destinationEmail1, $destinationEmail2, key($message->getTo()));
##Asserting template content???
$this->assertSame(?????, $message->getBody()
);
}
}
那么,我遇到的问题是:
我应该在哪里以及如何定义(在测试中)变量,例如 $originEmail,因为在“assertSame...”行中,我收到错误“未定义的变量” $originEmail”。
$this->assertSame($destinationEmail1, $destinationEmail2, key($message->getTo()));行的语法(考虑到它们不止一个变量)是否正确?如果不是在 .php 控制器中定义的文本,而是显示在 .twig 模板(在另外,通过使用像 {{ name }} 这样的变量?
【问题讨论】:
-
请注意,我们更喜欢这里的技术写作风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,聊天材料和缩写 txtspk,恳求,你多久了被卡住、投票建议、元评论等。只需解释您的问题,并展示您尝试过的内容、预期的内容以及实际发生的情况。
标签: php symfony phpunit swiftmailer