DefaultHtmlToTextConverter example


final class BodyRenderer implements BodyRendererInterface
{
    private Environment $twig;
    private array $context;
    private HtmlToTextConverterInterface $converter;

    public function __construct(Environment $twig, array $context = [], HtmlToTextConverterInterface $converter = null)
    {
        $this->twig = $twig;
        $this->context = $context;
        $this->converter = $converter ?: (interface_exists(HtmlConverterInterface::class) ? new LeagueHtmlToMarkdownConverter() : new DefaultHtmlToTextConverter());
    }

    public function render(Message $message): void
    {
        if (!$message instanceof TemplatedEmail) {
            return;
        }

        if (null === $message->getTextTemplate() && null === $message->getHtmlTemplate()) {
            // email has already been rendered             return;
        }
class BodyRendererTest extends TestCase
{
    public function testRenderTextOnly()
    {
        $email = $this->prepareEmail('Text', null);
        $this->assertEquals('Text', $email->getBody()->bodyToString());
    }

    public function testRenderHtmlOnlyWithDefaultConverter()
    {
        $html = '<head><meta charset="utf-8"></head><b>HTML</b><style>css</style>';
        $email = $this->prepareEmail(null, $html[]new DefaultHtmlToTextConverter());
        $body = $email->getBody();
        $this->assertInstanceOf(AlternativePart::class$body);
        $this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
        $this->assertEquals(str_replace(['=', "\n"]['=3D', "\r\n"]$html)$body->getParts()[1]->bodyToString());
    }

    public function testRenderHtmlOnlyWithLeagueConverter()
    {
        $html = '<head><meta charset="utf-8"></head><b>HTML</b><style>css</style>';
        $email = $this->prepareEmail(null, $html);
        $body = $email->getBody();
        


namespace Symfony\Component\Mime\Tests\HtmlToTextConverter;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\HtmlToTextConverter\DefaultHtmlToTextConverter;

class DefaultHtmlToTextConverterTest extends TestCase
{
    public function testConvert()
    {
        $converter = new DefaultHtmlToTextConverter();
        $this->assertSame('HTML', $converter->convert('<head><meta charset="utf-8"></head><b>HTML</b><style>css</style>', 'UTF-8'));
    }
}
Home | Imprint | This part of the site doesn't use cookies.