2017-02-09 25 views
2

我想写一个功能测试,检查电子邮件发送与否。我sendemail方法是在服务中定义的,所以我不能够按照所给出的例子cookbookSymfony3电子邮件服务功能测试

设置 - 控制器有一个形式,当表单提交和有效的,它会调用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');,在菜谱它说路径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()); 

    } 

这一个返回“失败断言0匹配预期1”。

邮件获取应用程序成功发送,但要确保在测试以及

+0

确定测试进入'如果($形式 - > isSubmitted()...'行动,因为你没有张贴值?你的表单类型是什么样子的? – COil

+0

雅,我在'if()'为true时写了测试,我已经为true和false写了测试,所以试图扩展sendmail的真正测试。让我们假设表单中的所有值都有预定义的数据。 –

+0

只需致电您的电子邮件服务即可使用获取操作。它在开发模式下工作吗? – COil

回答

1

从我的食谱录入看到,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()); 

    }