D_R
D_R

Reputation: 4962

Unit/functional test for a mailing function

I've started to write unit testing for my project (in symfony using PHPUnit ofcourse). I am trying to write a functional/unit test for this following function. and I don't know how exactly I should approach to do so. I know that I can use "test doubles" (mocking) for the send mail / send sms function. but what will I need to check in the Test ?

My function:

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

Upvotes: 0

Views: 115

Answers (1)

shacharsol
shacharsol

Reputation: 2342

First of all, let me explain the difference between functional testing and unit testing. You would like to write a functional test, to test whether the full business logic requirements fulfilled . Therefore, you won't do any mocking, when you write functional tests On Functional tests, you want to assure that actions performed by your unit. For example, if your code suppose to create a record on a table, your test will run the method, and then execute a select to check whether the record has been created. In your example you would want to verify that all notifyme were processed. In the unit test manner you will want to assure, that your test cover all lines of code (there is code coverage package for PhpUnit here. The objects which you would want to mock in your unit test, would be all the objects that your unit depends on,(entity_manager,message_sender). Then in your unit test you need to execute the verify method, to verify that some methods were executed on your mocks. I would recommend phockito for that:

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

Upvotes: 1

Related Questions