邮件功能的单元/功能测试

时间:2014-10-28 10:51:12

标签: php unit-testing symfony phpunit

我已经开始为我的项目编写单元测试(在使用PHPUnit ofcourse的symfony中)。 我正在尝试为此后续功能编写功能/单元测试。 而且我不知道我应该如何接近这样做。 我知道我可以使用“测试双打”(模拟)来发送邮件/发送短信功能。 但是我需要在测试中检查什么?

我的功能:

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();
    }

1 个答案:

答案 0 :(得分:1)

首先,让我解释功能测试和单元测试之间的区别。 您希望编写功能测试,以测试是否满足完整的业务逻辑要求。 因此,在编写功能测试时,您不会进行任何模拟 在功能测试中,您需要确保设备执行的操作。 例如,如果您的代码假设在表上创建记录,则测试将运行该方法,然后执行select以检查是否已创建记录。在您的示例中,您需要验证是否已处理所有通知。 在单元测试方式中,您需要确保您的测试涵盖所有代码行(有PhpUnit here的代码覆盖率包。 您希望在单元测试中模拟的对象将是您的单元所依赖的所有对象(entity_manager,message_sender)。 然后在单元测试中,您需要执行验证方法,以验证在您的模拟上执行了某些方法。我会建议phockito

Phockito::verify($mockmessageSender, 1)->sendSms();