对发送包含两个收件人的电子邮件(一个收件人:,一个抄送:)的进程的功能测试错误地声明与收件人同时收件人:和抄送。
在开发环境中,下面测试中指出的过程向jmelanzane@bogus.info发送一封电子邮件,并发送一封电子邮件至admin@bogus.info。 (在测试调试模式中逐步执行该过程可确认正确的收件人。)然而,下面的测试失败。使用admin@bogus.info替换To:收件人可以使测试完全成功。
在测试期间在邮件程序进程中使用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'
swiftmailer:
logging: true
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;
}
答案 0 :(得分:0)
ARRRGGHHH!它是一个广受欢迎的未清除缓存问题。但是,在这种情况下,需要清除测试缓存!一旦清除,测试就会按预期成功。