当我查看这段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);
}
}
那么如何在不需要调用客户端测试的情况下测试应用程序是否发送电子邮件?
正如@BentCoder 建议的那样,您可以使用将邮件存储在 spool 目录中的方法并检查消息是否存在。为了识别使用自定义标题。就我而言,我只是在X-Agent
标题中放置了一个通用名称。
我的发送电子邮件服务代码是:
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='[email protected]';
$to='[email protected]';
$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
并将其作为可选参数放置在函数的发送参数上,以防号码不为空,您可以将其设置为标头。
因此,当您遍历假脱机目录时,您可以标记将通过其跟踪号发送的电子邮件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句