功能测试中的电子邮件收件人不正确
Incorrect e-mail recipients in functional test
发送一封电子邮件的过程的功能测试有两个收件人(一个收件人:一个抄送:)错误地声称收件人与收件人和抄送相同。
在开发环境中,下面测试中指示的进程会向 jmelanzane@bogus.info 发送一封电子邮件,并抄送至 admin@bogus.info。 (在测试调试模式下逐步完成该过程可确认正确的收件人。)但下面的测试失败了。用 admin@bogus.info 替换 To: recipient 允许测试完全成功。
控制台输出
在测试期间在邮件进程中使用 print_r($recipient);
输出。
PHPUnit 4.3.5 by Sebastian Bergmann.
Configuration read from G:\Documents\workspace\volunteer\app\phpunit.xml.dist
FArray
(
[0] => jmelanzane@bogus.info
)
Time: 4.5 seconds, Memory: 26.25Mb
There was 1 failure:
1) Truckee\MatchingBundle\Tests\Controller\AdminMailerTest::testActivateOrganizationEmail
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'jmelanzane@bogus.info'
+'admin@bogus.info'
config_test.yml
swiftmailer:
logging: true
config_dev.yml
swiftmailer:
# disable_delivery: true
# delivery_address: admin@bogus.info
测试
public function testActivateOrganizationEmail()
{
$crawler = $this->login('admin');
$this->client->followRedirects(false);
$link = $crawler->selectLink('Accept organization')->link();
$crawler = $this->client->click($link);
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
$this->assertEquals('jmelanzane@bogus.info', key($message->getTo()));
$this->assertEquals('admin@bogus.info', key($message->getCc()));
}
编辑 - 过程。
在此实例中,$to
中有一个元素 jmelanzane@bogus.info 和 $this->adminRecipients
中有一个元素 admin@bogus.info。
public function activateOrgMail($organization, $to)
{
$recipient = [];
foreach ($to as $user) {
$recipient[] = $this->getAddressee($user);
}
$cc = $this->adminRecipients();
if (!empty($recipient)) {
$message = \Swift_Message::newInstance()
->setSubject('Organization now active')
->setFrom($this->parameters['address'])
->setTo($recipient[0])
->setCc($cc)
->setContentType('text/html')
->setBody(
$this->twig->render(
'activated_org', array(
'organization' => $organization,
'recipient' => $recipient,
), 'text/html'
)
)
;
return $this->mailer->send($message);
}
else {
return 0;
}
}
private function adminRecipients()
{
$em = $this->em;
$admins = $em->getRepository("TruckeeMatchingBundle:Admin")->findBy(['locked' => false]);
$adminEmail = [];
foreach ($admins as $admin) {
$email = $admin->getEmail();
$adminEmail[] = $email;
}
return $adminEmail;
}
啊啊啊!这是一直流行的未清除缓存问题。然而,在这种情况下,需要清除测试缓存!清除后,测试会按预期成功。
发送一封电子邮件的过程的功能测试有两个收件人(一个收件人:一个抄送:)错误地声称收件人与收件人和抄送相同。
在开发环境中,下面测试中指示的进程会向 jmelanzane@bogus.info 发送一封电子邮件,并抄送至 admin@bogus.info。 (在测试调试模式下逐步完成该过程可确认正确的收件人。)但下面的测试失败了。用 admin@bogus.info 替换 To: recipient 允许测试完全成功。
控制台输出
在测试期间在邮件进程中使用 print_r($recipient);
输出。
PHPUnit 4.3.5 by Sebastian Bergmann.
Configuration read from G:\Documents\workspace\volunteer\app\phpunit.xml.dist
FArray
(
[0] => jmelanzane@bogus.info
)
Time: 4.5 seconds, Memory: 26.25Mb
There was 1 failure:
1) Truckee\MatchingBundle\Tests\Controller\AdminMailerTest::testActivateOrganizationEmail
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'jmelanzane@bogus.info'
+'admin@bogus.info'
config_test.yml
swiftmailer:
logging: true
config_dev.yml
swiftmailer:
# disable_delivery: true
# delivery_address: admin@bogus.info
测试
public function testActivateOrganizationEmail()
{
$crawler = $this->login('admin');
$this->client->followRedirects(false);
$link = $crawler->selectLink('Accept organization')->link();
$crawler = $this->client->click($link);
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
$this->assertEquals('jmelanzane@bogus.info', key($message->getTo()));
$this->assertEquals('admin@bogus.info', key($message->getCc()));
}
编辑 - 过程。
在此实例中,$to
中有一个元素 jmelanzane@bogus.info 和 $this->adminRecipients
中有一个元素 admin@bogus.info。
public function activateOrgMail($organization, $to)
{
$recipient = [];
foreach ($to as $user) {
$recipient[] = $this->getAddressee($user);
}
$cc = $this->adminRecipients();
if (!empty($recipient)) {
$message = \Swift_Message::newInstance()
->setSubject('Organization now active')
->setFrom($this->parameters['address'])
->setTo($recipient[0])
->setCc($cc)
->setContentType('text/html')
->setBody(
$this->twig->render(
'activated_org', array(
'organization' => $organization,
'recipient' => $recipient,
), 'text/html'
)
)
;
return $this->mailer->send($message);
}
else {
return 0;
}
}
private function adminRecipients()
{
$em = $this->em;
$admins = $em->getRepository("TruckeeMatchingBundle:Admin")->findBy(['locked' => false]);
$adminEmail = [];
foreach ($admins as $admin) {
$email = $admin->getEmail();
$adminEmail[] = $email;
}
return $adminEmail;
}
啊啊啊!这是一直流行的未清除缓存问题。然而,在这种情况下,需要清除测试缓存!清除后,测试会按预期成功。