【发布时间】:2014-10-28 10:51:12
【问题描述】:
我已经开始为我的项目编写单元测试(当然在 symfony 中使用 PHPUnit)。 我正在尝试为以下功能编写功能/单元测试。 而且我不知道我应该如何做到这一点。 我知道我可以使用“测试替身”(模拟)来发送邮件/发送短信功能。 但是我需要在测试中检查什么?
我的功能:
public function notifyAlerts()
{
$alertSettings = $this->entity_manager->getRepository('TravelyoCoreBundle:Alert')->getActive();
$notifiedIdsObject = array(); // Will store the ids of the AlertLog that has been notified.
foreach($alertSettings as $alert)
{
$code = $alert->getEventCode();
$agencyId = $alert->getAgencyId();
$agency = $this->entity_manager->getRepository('TravelyoCoreBundle:Agency')->findOneBy(array('id'=>$agencyId));
$site= $agency->getSite();
$alertsToNotify = $this->entity_manager->getRepository('TravelyoCoreBundle:AlertLog')->getForCodeAgencyDate($code, $agencyId);
$messageArrays = array();
foreach($alertsToNotify as $notifyMe)
{
$notifiedIdsObject[$notifyMe->getId()] = $notifyMe->getId();
$methods = $alert->getMethods();
$routeParams = $notifyMe->getRouteParam();
$routeParams['trav_host'] = $site->getFullUrl(); //We need to know on which backoffice we need to send him
$url = $this->router->generate($notifyMe->getRoute(),$routeParams,true);
$shortUrl = GooglesShortUrl::generateUrl($url);
$messageParam = $notifyMe->getMessageParam();
$messageParam['%link%'] = $shortUrl;
if(in_array('sms', $methods))
{
$this->messageSender->sendSms($recipientsArray, $notifyMe->getMessage(),$messageParam, 'alert',strtolower($alert->getLanguage()), false, $agency->getSmsSettings());
}
if(in_array('email', $methods))
{
$recipients = $alert->getEmail();
$recipientsArray = explode(";",$recipients);
$messageArrays[] = array(
"message" => $notifyMe->getMessage(),
"messageParam" => $messageParam,
"domain" => "alert"
);
}
$notifyMe->setProcessed(1);
$this->entity_manager->persist($notifyMe);
}
if(count($messageArrays)>0)
{
$comType = array(MessageSenderManager::COMMUNICATION_TYPE_EMAIL);
$options = array('mail-settings'=> $agency->getMailSettings(),'subject'=>'Alert System : '. $notifyMe->getEventCode(), "template"=> "TravelyoAdminBundle:Admin/Mail:alert.html.twig");
$this->messageSender->send($agency->getMailSettingEmailAddress(),join(',',$recipientsArray), $messageArrays, $comType, $options );
}
}
$this->entity_manager->flush();
}
【问题讨论】:
标签: php unit-testing symfony phpunit