Symfony3中作为服务的电子邮件功能测试

时间:2017-02-09 16:16:31

标签: php symfony functional-testing swiftmailer

我正在尝试编写一个检查电子邮件是否已发送的功能测试。我的sendemail方法是在服务中定义的,因此我无法按照cookbook

中给出的示例进行操作

设置 -  控制器有一个表单,当表单提交并且有效时,它将在服务中调用sendmail方法

  

控制器

public function sampleAction(Request $request){

   $sampleEntity = new SampleEntity();
   $form = $this->createForm(sampleType::class, $sampleEntity)
   $form->handleRequest($request);
   if ($form->isSubmitted() && $form->isValid()){
      ...
      $sampleservice = $this->get('sampleservice');
      $sampleservice->sendMail();

      return $this->render('sample/formsubmitted.html.twig');
   }
}
  

服务

public function sendMail(){

        $body = $this->renderTemplate();

        $message = \Swift_Message::newInstance()
            ->setSubject('Sample Mail')
            ->setFrom($this->sender_mail)
            ->setTo($this->admin_mail)
            ->setBody($body);

        $this->mailer->send($message);
    }
  

测试

public function testEmailSend(){

        $client = static::createClient();

        $client->enableProfiler();
        $crawler = $client->request('POST', '/sampleform');
        $client->followRedirects(false);

        $form = $crawler->selectButton('Submit Form')->form();

        $crawler = $client->submit($form);

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }

可以更好地解释这一行$crawler = $client->request('POST', '/sampleform');,在cookbook中它说的是sendmail动作的路径,但由于它不是我的情况下的动作,在那里写什么或者我必须采取全新的方法?

除此之外,我尝试直接从服务调用sendmail,assertEqual给我0,因为没有访问sendmail(我猜)。关于在何处以及如何进行的任何想法。这是我第一次尝试编写功能测试,所以如果我犯了一些明显的错误,请原谅我。

  

编辑1

SampleType

public function buildForm(FormBuilderInterface $builder, array $options){

   $builder
            ->add('name', TextType::class, array('data' => 'Sample'))
            ->add('phone', TextType::class, array('data' => '1234567890'))
->add('save', SubmitType::class, array('label' => 'Submit Form'))
            ->getForm();    
}

编辑2 当尝试直接呼叫服务时,有没有其他方式直接呼叫服务?

public function testEmailSend(){

        $client = static::createClient();
        $client->enableProfiler();
        $crawler = $client->request('POST', '/src/AppBundle/Service/SampleService/sendMail()');

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }

这个返回“Failed断言0匹配预期的1。”

邮件在应用程序中成功发送,但是也想在测试中确保

1 个答案:

答案 0 :(得分:1)

根据我在cookbook条目中看到的内容,enableProfiler仅启用下一个请求的探查器。您的提交是第二个请求。在测试中提交表单之前启用探查器。

同样在config_test.yml你有探查者enabled: truecollect: true吗?请参阅本页底部:How to Use the Profiler in a Functional Test

<强>测试

public function testEmailSend(){

        $client = static::createClient();

        $crawler = $client->request('POST', '/sampleform');
        $client->followRedirects(false);

        $form = $crawler->selectButton('Submit Form')->form();

        $client->enableProfiler();
        $crawler = $client->submit($form);

        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

        $this->assertEquals(1, $mailCollector->getMessageCount());

    }