Mailer 测试失败,在 null 上调用成员函数 getSubject()
Mailer test fails with Call to a member function getSubject() on null
在 Symfony 5.0.2 项目中,新 Mailer 的测试失败并显示
Error: Call to a member function getSubject() on null
电子邮件 service and test 基于 symfonycast 教程。
在$email = ...;
之后立即在服务中添加var_dump($email);
显示object(Symfony\Bridge\Twig\Mime\TemplatedEmail)#24 (11) {...
,这表示在服务中创建了一个真实的对象。
services.yaml:
App\Services\EmailerService:
$mailer: '@mailer'
$senderAddress: '%app.sender_address%'
服务:
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
class EmailerService
{
private $mailer;
private $sender;
public function __construct($mailer, $senderAddress)
{
$this->mailer = $mailer;
$this->sender = $senderAddress;
}
public function appMailer($mailParams)
{
$email = (new TemplatedEmail())
->from($this->sender)
->to($mailParams['recipient'])
->subject($mailParams['subject'])
->htmlTemplate($mailParams['view'])
->context($mailParams['context']);
$this->mailer->send($email);
}
}
测试:
use App\Services\EmailerService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\MailerInterface;
class MailerTest extends TestCase
{
public function testSimpleMessage()
{
$symfonyMailer = $this->createMock(MailerInterface::class);
$symfonyMailer->expects($this->once())
->method('send');
$mailer = new EmailerService($symfonyMailer, 'admin@bogus.info', 'admin@bogus.info');
$mailParams = [
'view' => 'Email/non_user_forgotten_password.html.twig',
'context' => ['supportEmail' => 'admin@bogus.info'],
'recipient' => 'bborko@bogus.info',
'subject' => 'Test message',
];
$email = $mailer->appMailer($mailParams);
$this->assertSame('Test message', $email->getSubject());
}
}
appMailer()
必须 return 一个 TemplatedEmail
对象,这样你才能调用 getSubject()
。目前它 return 什么都没有。将其更改为:
public function appMailer($mailParams)
{
$email = (new TemplatedEmail())
->from($this->sender)
->to($mailParams['recipient'])
->subject($mailParams['subject'])
->htmlTemplate($mailParams['view'])
->context($mailParams['context']);
$this->mailer->send($email);
return $email; // I added this line.
}
在 Symfony 5.0.2 项目中,新 Mailer 的测试失败并显示
Error: Call to a member function getSubject() on null
电子邮件 service and test 基于 symfonycast 教程。
在$email = ...;
之后立即在服务中添加var_dump($email);
显示object(Symfony\Bridge\Twig\Mime\TemplatedEmail)#24 (11) {...
,这表示在服务中创建了一个真实的对象。
services.yaml:
App\Services\EmailerService:
$mailer: '@mailer'
$senderAddress: '%app.sender_address%'
服务:
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
class EmailerService
{
private $mailer;
private $sender;
public function __construct($mailer, $senderAddress)
{
$this->mailer = $mailer;
$this->sender = $senderAddress;
}
public function appMailer($mailParams)
{
$email = (new TemplatedEmail())
->from($this->sender)
->to($mailParams['recipient'])
->subject($mailParams['subject'])
->htmlTemplate($mailParams['view'])
->context($mailParams['context']);
$this->mailer->send($email);
}
}
测试:
use App\Services\EmailerService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\MailerInterface;
class MailerTest extends TestCase
{
public function testSimpleMessage()
{
$symfonyMailer = $this->createMock(MailerInterface::class);
$symfonyMailer->expects($this->once())
->method('send');
$mailer = new EmailerService($symfonyMailer, 'admin@bogus.info', 'admin@bogus.info');
$mailParams = [
'view' => 'Email/non_user_forgotten_password.html.twig',
'context' => ['supportEmail' => 'admin@bogus.info'],
'recipient' => 'bborko@bogus.info',
'subject' => 'Test message',
];
$email = $mailer->appMailer($mailParams);
$this->assertSame('Test message', $email->getSubject());
}
}
appMailer()
必须 return 一个 TemplatedEmail
对象,这样你才能调用 getSubject()
。目前它 return 什么都没有。将其更改为:
public function appMailer($mailParams)
{
$email = (new TemplatedEmail())
->from($this->sender)
->to($mailParams['recipient'])
->subject($mailParams['subject'])
->htmlTemplate($mailParams['view'])
->context($mailParams['context']);
$this->mailer->send($email);
return $email; // I added this line.
}