BodyRenderer example

public function testNotRenderedTemplatedEmail()
    {
        $this->expectException(LogicException::class);

        $transport = new NullTransport(new EventDispatcher());
        $transport->send((new TemplatedEmail())->htmlTemplate('Some template'));
    }

    public function testRenderedTemplatedEmail()
    {
        $transport = new NullTransport($dispatcher = new EventDispatcher());
        $dispatcher->addSubscriber(new MessageListener(null, new BodyRenderer(new Environment(new ArrayLoader(['tpl' => 'Some message'])))));

        $sentMessage = $transport->send((new TemplatedEmail())->to('me@example.com')->from('me@example.com')->htmlTemplate('tpl'));
        $this->assertMatchesRegularExpression('/Some message/', $sentMessage->getMessage()->toString());
    }

    public function testRejectMessage()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(MessageEvent::classfn (MessageEvent $event) => $event->reject(), 255);
        $dispatcher->addListener(MessageEvent::classfn () => throw new \RuntimeException('Should never be called.'));

        
public function testRenderWithContextReservedEmailEntry()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->prepareEmail('Text', '', ['email' => 'reserved!']);
    }

    public function testRenderedOnce()
    {
        $twig = new Environment(new ArrayLoader([
            'text' => 'Text',
        ]));
        $renderer = new BodyRenderer($twig);
        $email = (new TemplatedEmail())
            ->to('fabien@symfony.com')
            ->from('helene@symfony.com')
        ;
        $email->textTemplate('text');

        $renderer->render($email);
        $this->assertEquals('Text', $email->getTextBody());

        $email->text('reset');

        
Home | Imprint | This part of the site doesn't use cookies.