我已根据本教程定义了一个简单的电子邮件控制器:
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 ...”行中,我得到错误“Undefined variable” $ originEmail”。
行$this->assertSame($destinationEmail1, $destinationEmail2, key($message->getTo()));
的语法是否正确(考虑到多个变量)?
如何断言正文内容,如果不是.php控制器中的已定义文本,则会在 .twig模板中显示(在另外,使用像{{name}}?
答案 0 :(得分:0)
关于1)
我将如何进行此操作取决于此信息如何传递到操作中。如果它不是请求的一部分,我会通过执行以下操作来声明预期值:
$this->assertSame('expected@email.com', $message->getFrom());
如果它是路线的一部分,我会在测试中分配一个局部变量,然后将其传递给路线,可能是这样的:
$originalEmail = 'expected@email.com';
$path = sprintf('/path/to/action/%s', $originalEmail);
$client->request('POST', $path);
...
$this->assertSame($originalEmail, $message->getFrom());
如果它是POST数据的一部分:
$data = [
'originalEmail' => 'expected@email.com',
];
$client->request('POST', '/path/to/action', $data);
...
$this->assertSame($data['originalEmail'], $message->getFrom());
关于2)
这不会按预期工作,但你可以这样做(如果电子邮件是数组中的值):
$this->assertContains($destinationEmail1, $message->getTo());
或者像这样,如果它是一把钥匙(就像你的例子中那样):
$this->assertArrayHasKey($destinationEmail1, $message->getTo());
关于3)
如果要比较正文与示例相匹配,则可能需要将参考输出存储在文件中,然后比较内容:
$this->assertSame(
file_get_contents(__DIR__ . '/emails/reference_output.html'),
$message->getBody()
);
在这种情况下,您必须小心您的电子邮件正文包含与参考输出相同的名称。
或者您也可以使用crawler查看某些html元素及其内容,例如检查标题<h3>This is an email!</h3>
是否在那里:
$crawler = $client->request('POST', '/path/to/action');
$this->assertSame(
'This is an email!',
$crawler->filter('h3')->first()->text()
);
// alternative:
$this->assertGreaterThan(
0,
$crawler->filter('h3:contains("This is an email!")')->count()
);