2014-10-28 113 views
0

我已經開始爲我的項目編寫單元測試(在使用PHPUnit of course的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

首先,讓我解釋功能測試和單元測試之間的區別。 您想編寫一個功能測試,以測試是否滿足完整的業務邏輯需求。 因此,當您編寫功能測試時,您不會做任何嘲諷 在功能測試中,您希望確保您的設備執行的操作。 例如,如果您的代碼假設在表上創建記錄,您的測試將運行該方法,然後執行select以檢查記錄是否已創建。在你的例子中,你會想驗證所有的notifyme都被處理了。 在單元測試的方式,你會想要保證,你的測試覆蓋所有代碼行(有代碼覆蓋包爲PhpUnit here。 你想在你的單元測試中嘲笑的對象將是所有對象你的單位取決於,(entity_manager,message_sender) 然後在你的單元測試,你需要執行的驗證方法,驗證一些方法是在你的嘲弄執行我建議phockito爲:。

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