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

Functional test for Email as Service in Symfony3

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

中给出的示例

设置 - Controller 有一个表单,当表单提交并有效时,它会调用服务中的 sendmail 方法

Controller

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

Service

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

Test

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(我猜)。关于在哪里以及如何进行的任何想法。这是我第一次尝试编写功能测试,所以如果我犯了一些明显的错误,请原谅我。

Edit 1

样本类型

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

    }

这个returns"Failed asserting that 0 matches expected 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());

    }