8000 [Twig] Remove TemplatedEmail::template() by fabpot · Pull Request #30853 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Twig] Remove TemplatedEmail::template() #30853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
10000
Diff view
27 changes: 0 additions & 27 deletions src/Symfony/Bridge/Twig/Mime/BodyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public function render(Message $message): void
'email' => new WrappedTemplatedEmail($this->twig, $message),
]);

if ($template = $message->getTemplate()) {
$this->renderFull($message, $template, $vars);
}

if ($template = $message->getTextTemplate()) {
$message->text($this->twig->render($template, $vars));
}
Expand All @@ -68,29 +64,6 @@ public function render(Message $message): void
}
}

private function renderFull(TemplatedEmail $message, string $template, array $vars): void
{
$template = $this->twig->load($template);

if ($template->hasBlock('subject', $vars)) {
$message->subject($template->renderBlock('subject', $vars));
}

if ($template->hasBlock('text', $vars)) {
$message->text($template->renderBlock('text', $vars));
}

if ($template->hasBlock('html', $vars)) {
$message->html($template->renderBlock('html', $vars));
}

if ($template->hasBlock('config', $vars)) {
// we discard the output as we're only interested
// in the side effect of calling email methods
$template->renderBlock('config', $vars);
}
}

private function convertHtmlToText(string $html): string
{
if (null !== $this->converter) {
Expand Down
20 changes: 2 additions & 18 deletions src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,10 @@
*/
class TemplatedEmail extends Email
{
private $template;
private $htmlTemplate;
private $textTemplate;
private $context = [];

/**
* @return $this
*/
public function template(?string $template)
{
$this->template = $template;

return $this;
}

/**
* @return $this
*/
Expand All @@ -55,11 +44,6 @@ public function htmlTemplate(?string $template)
return $this;
}

public function getTemplate(): ?string
{
return $this->template;
}

public function getTextTemplate(): ?string
{
return $this->textTemplate;
Expand Down Expand Up @@ -90,15 +74,15 @@ public function getContext(): array
*/
public function __serialize(): array
{
return [$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()];
return [$this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()];
}

/**
* @internal
*/
public function __unserialize(array $data): void
{
[$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data;
[$this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data;

parent::__unserialize($parentData);
}
Expand Down
108 changes: 5 additions & 103 deletions src/Symfony/Bridge/Twig/Tests/Mime/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
use Symfony\Component\Mime\Part\TextPart;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class RendererTest extends TestCase
{
public function testRenderTextOnly(): void
{
$email = $this->prepareEmail(null, 'Text', null);
$email = $this->prepareEmail('Text', null);
$this->assertEquals('Text', $email->getBody()->bodyToString());
}

public function testRenderHtmlOnly(): void
{
$email = $this->prepareEmail(null, null, '<b>HTML</b>');
$email = $this->prepareEmail(null, '<b>HTML</b>');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
Expand All @@ -40,7 +37,7 @@ public function testRenderHtmlOnly(): void

public function testRenderHtmlOnlyWithTextSet(): void
{
$email = $this->prepareEmail(null, null, '<b>HTML</b>');
$email = $this->prepareEmail(null, '<b>HTML</b>');
$email->text('Text');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
Expand All @@ -50,118 +47,23 @@ public function testRenderHtmlOnlyWithTextSet(): void

public function testRenderTextAndHtml(): void
{
$email = $this->prepareEmail(null, 'Text', '<b>HTML</b>');
$email = $this->prepareEmail('Text', '<b>HTML</b>');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}

public function testRenderFullOnly(): void
{
$email = $this->prepareEmail(<<<EOF
{% block subject %}Subject{% endblock %}
{% block text %}Text{% endblock %}
{% block html %}<b>HTML</b>{% endblock %}
EOF
, null, null);
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Subject', $email->getSubject());
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}

public function testRenderFullOnlyWithTextOnly(): void
{
$email = $this->prepareEmail(<<<EOF
{% block text %}Text{% endblock %}
EOF
, null, null);
$body = $email->getBody();
$this->assertInstanceOf(TextPart::class, $body);
$this->assertEquals('', $email->getSubject());
$this->assertEquals('Text', $body->bodyToString());
}

public function testRenderFullOnlyWithHtmlOnly(): void
{
$email = $this->prepareEmail(<<<EOF
{% block html %}<b>HTML</b>{% endblock %}
EOF
, null, null);
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('', $email->getSubject());
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}

public function testRenderFullAndText(): void
{
$email = $this->prepareEmail(<<<EOF
{% block text %}Text full{% endblock %}
{% block html %}<b>HTML</b>{% endblock %}
EOF
, 'Text', null);
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}

public function testRenderFullAndHtml(): void
{
$email = $this->prepareEmail(<<<EOF
{% block text %}Text full{% endblock %}
{% block html %}<b>HTML</b>{% endblock %}
EOF
, null, '<i>HTML</i>');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Text full', $body->getParts()[0]->bodyToString());
$this->assertEquals('<i>HTML</i>', $body->getParts()[1]->bodyToString());
}

public function testRenderHtmlWithEmbeddedImages(): void
{
$email = $this->prepareEmail(null, null, '<img src="{{ email.image("image.jpg") }}" />');
$body = $email->getBody();
$this->assertInstanceOf(RelatedPart::class, $body);
$this->assertInstanceOf(AlternativePart::class, $body->getParts()[0]);
$this->assertStringMatchesFormat('<img src=3D"cid:%s@symfony" />', $body->getParts()[0]->getParts()[1]->bodyToString());
$this->assertEquals('Some image data', base64_decode($body->getParts()[1]->bodyToString()));
}

public function testRenderFullWithAttachments(): void
{
$email = $this->prepareEmail(<<<EOF
{% block text %}Text{% endblock %}
{% block config %}
{% do email.attach('document.txt') %}
{% endblock %}
EOF
, null, null);
$body = $email->getBody();
$this->assertInstanceOf(MixedPart::class, $body);
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('Some text document...', base64_decode($body->getParts()[1]->bodyToString()));
}

private function prepareEmail(?string $full, ?string $text, ?string $html): TemplatedEmail
private function prepareEmail(?string $text, ?string $html): TemplatedEmail
{
$twig = new Environment(new ArrayLoader([
'full' => $full,
'text' => $text,
'html' => $html,
'document.txt' => 'Some text document...',
'image.jpg' => 'Some image data',
]));
$renderer = new BodyRenderer($twig);
$email = (new TemplatedEmail())->to('fabien@symfony.com')->from('helene@symfony.com');
if (null !== $full) {
$email->template('full');
}
if (null !== $text) {
$email->textTemplate('text');
}
Expand Down
3 changes: 0 additions & 3 deletions src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public function test()
$email->context($context = ['product' => 'Symfony']);
$this->assertEquals($context, $email->getContext());

$email->template($template = 'full');
$this->assertEquals($template, $email->getTemplate());

$email->textTemplate($template = 'text');
$this->assertEquals($template, $email->getTextTemplate());

Expand Down
0