Mailer test fails with Call to a member function getSubject() on null

In a Symfony 5.0.2 project a test of the new Mailer fails with

Error: Call to a member function getSubject() on null

The email service and test are based on symfonycast tutorials.

Adding var_dump($email); in the service immediately after $email = ...; shows object(SymfonyBridgeTwigMimeTemplatedEmail)#24 (11) {..., which says there is a real object created in the service.


        $mailer: '@mailer'
        $senderAddress: '%app.sender_address%'


use SymfonyBridgeTwigMimeTemplatedEmail;

class EmailerService
    private $mailer;
    private $sender;

    public function __construct($mailer, $senderAddress)
        $this->mailer = $mailer;
        $this->sender = $senderAddress;

    public function appMailer($mailParams)
        $email = (new TemplatedEmail())


use AppServicesEmailerService;
use PHPUnitFrameworkTestCase;
use SymfonyComponentMailerMailerInterface;

class MailerTest extends TestCase
    public function testSimpleMessage()
        $symfonyMailer = $this->createMock(MailerInterface::class);

        $mailer = new EmailerService($symfonyMailer, '[email protected]', '[email protected]');
        $mailParams = [
            'view' => 'Email/non_user_forgotten_password.html.twig',
            'context' => ['supportEmail' => '[email protected]'],
            'recipient' => '[email protected]',
            'subject' => 'Test message',            
        $email = $mailer->appMailer($mailParams);

        $this->assertSame('Test message', $email->getSubject());

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *