无需调用控制器方法即可测试电子邮件发送功能

Test email sending functionality without the need of invoking a controller method

当我查看 this 一段 symfony 文档时,我意识到实际测试是在控制器上完成的。

在我的例子中,我想测试的是服务中的自定义函数,例如:

class MyMailService
{

 public function __construct(SwiftMailer $mailer)
 {
   $this->mailer=$mailer;
 }


 public function sendHelloEmail($from,$to)
 {
   $message = (new \Swift_Message('Hello Email'))
        ->setFrom($from)
        ->setTo($to)
        ->setBody('I can see stars');

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

}

那么如何在不需要调用客户端测试的情况下测试应用程序是否发送电子邮件?

So how I can test whether the application sends an email without the need to invoke client tests?

没有必要这样做。测试是否发送邮件属于功能测试甚至系统集成测试的范畴(因为邮件是否真正发送还取决于邮件环境配置、网络等)。如果您只想检查您的邮件结构是否正确,如果您将实际的邮件创建与邮件发送分开,则可以对服务进行单元测试。

正如@BentCoder 建议的那样,您可以使用将邮件存储在假脱机目录中的方法,并检查邮件是否存在。为了识别使用自定义 header。在我的例子中,我只是在 X-Agent header.

中放置了一个通用名称

我的发送邮件服务代码是:

namespace AppBundle\Services;

use AppBundle\Interfaces\EmailSenderInterface;
use \Swift_Mailer;
use \Swift_Message;

class NormalEmailSend implements EmailSenderInterface
{

  /**
  * @var Swift_Mailer
  */
  private $mailer=null;

  public function __construct(Swift_Mailer $mailer)
  {
    $this->mailer=$mailer;
  }

  /**
  * @inheritdoc
  */
  public function send($from,$to,$title="",$bodyPlain="",$bodyHtml="",array $cc=[],array $bcc=[])
  {

    $message=new Swift_Message($title);
    $message->setFrom($from)->setTo($to)->setBody($bodyPlain,'text/plain');

    if($bodyHtml){
        $message->addPart($bodyHtml,'text/html');
    }

    $headers = $message->getHeaders();
    $headers->addTextHeader('X-Agent','ellakcy_member_app');

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


}

测试是:

namespace Tests\AppBundle\Services;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use \Swift_Message;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;

use AppBundle\Services\NormalEmailSend;

/**
* @testype functional
*/
class ContactEmailSendTest extends KernelTestCase
{

   /**
   * @var AppBundle\Services\NormalEmailSend
   */
   private $service;

   /**
   * @var String
   */
   private $spoolPath=null;

    /**
    * {@inheritDoc}
    */
     protected function setUp()
     {
         $kernel = self::bootKernel();
         $container = $kernel->getContainer();
         $this->service = $container->get(NormalEmailSend::class);
         $this->spoolPath = $container->getParameter('swiftmailer.spool.default.file.path');
     }

    public function testSendEmail()
    {
       $from='sender@example.com';
       $to='receiver@example.com';
       $this->service->send($from,$to,'Hello','Hello','Hello');
       $this->checkEmail();
    }

    private function checkEmail()
    {
      $spoolDir = $this->getSpoolDir();
      $filesystem = new Filesystem();

      if ($filesystem->exists($spoolDir)) {
          $finder = new Finder();
          $finder->in($spoolDir)
                 ->ignoreDotFiles(true)
                ->files();

          if(!$finder->hasResults()){
            $this->fail('No email has been sent');
          }

          $counter=0;
          foreach ($finder as $file) {
              /** @var Swift_Message $message */
              $message = unserialize(file_get_contents($file));
              $header = $message->getHeaders()->get('X-Agent');
              $this->assertEquals($header->getValue(),'ellakcy_member_app');
              $counter++;
          }

          //@todo Possibly Consinder not doing this check
          if($counter===0){
              $this->fail('No email has been sent');
          }

      } else {
        $this->fail('On test environment the emails should be spooled and not actuallt be sent');
      }
    }


    /**
    * {@inheritDoc}
    */
    public function tearDown()
    {
        $spoolDir = $this->getSpoolDir();
        $filesystem = new Filesystem();
        $filesystem->remove($spoolDir);
    }

    /**
    * @return string
    */
    private function getSpoolDir()
    {
      return $this->spoolPath;
    }

}

希望对您有所帮助,您也可以修改上面看到的代码,以便通过 X-Mail-Id 放置一个跟踪号码,并将其作为可选参数放在函数的发送参数上,以防该号码不为空您可以将其设置为 header.

因此,当您遍历假脱机目录时,您可以通过其跟踪编号标记将要发送的电子邮件。