|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Twig\Tests\Mime; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bridge\Twig\Mime\BodyRenderer; |
| 16 | +use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
| 17 | +use Twig\Environment; |
| 18 | +use Twig\Error\LoaderError; |
| 19 | +use Twig\Loader\FilesystemLoader; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Alexander Hofbauer <a.hofbauer@fify.at |
| 23 | + */ |
| 24 | +class WrappedTemplatedEmailTest extends TestCase |
| 25 | +{ |
| 26 | + public function testEmailImage() |
| 27 | + { |
| 28 | + $email = $this->buildEmail('email/image.html.twig'); |
| 29 | + $body = $email->toString(); |
| 30 | + $contentId1 = $email->getAttachments()[0]->getContentId(); |
| 31 | + $contentId2 = $email->getAttachments()[1]->getContentId(); |
| 32 | + |
| 33 | + $part1 = str_replace("\n", "\r\n", |
| 34 | + <<<PART |
| 35 | + Content-ID: <$contentId1> |
| 36 | + Content-Type: image/png; name="$contentId1" |
| 37 | + Content-Transfer-Encoding: base64 |
| 38 | + Content-Disposition: inline; |
| 39 | + name="$contentId1"; |
| 40 | + filename="@assets/images/logo1.png" |
| 41 | +
|
| 42 | + PART |
| 43 | + ); |
| 44 | + |
| 45 | + $part2 = str_replace("\n", "\r\n", |
| 46 | + <<<PART |
| 47 | + Content-ID: <$contentId2> |
| 48 | + Content-Type: image/png; name="$contentId2" |
| 49 | + Content-Transfer-Encoding: base64 |
| 50 | + Content-Disposition: inline; |
| 51 | + name="$contentId2"; filename=image.png |
| 52 | +
|
| 53 | + PART |
| 54 | + ); |
| 55 | + |
| 56 | + self::assertStringContainsString('', $body); |
| 57 | + self::assertStringContainsString($part1, $body); |
| 58 | + self::assertStringContainsString($part2, $body); |
| 59 | + } |
| 60 | + |
| 61 | + public function testEmailAttach() |
| 62 | + { |
| 63 | + $email = $this->buildEmail('email/attach.html.twig'); |
| 64 | + $body = $email->toString(); |
| 65 | + |
| 66 | + $part1 = str_replace("\n", "\r\n", |
| 67 | + <<<PART |
| 68 | + Content-Type: image/png; name=logo1.png |
| 69 | + Content-Transfer-Encoding: base64 |
| 70 | + Content-Disposition: attachment; name=logo1.png; filename=logo1.png |
| 71 | +
|
| 72 | + PART |
| 73 | + ); |
| 74 | + |
| 75 | + $part2 = str_replace("\n", "\r\n", |
| 76 | + <<<PART |
| 77 | + Content-Type: image/png; name=image.png |
| 78 | + Content-Transfer-Encoding: base64 |
| 79 | + Content-Disposition: attachment; name=image.png; filename=image.png |
| 80 | +
|
| 81 | + PART |
| 82 | + ); |
| 83 | + |
| 84 | + self::assertStringContainsString($part1, $body); |
| 85 | + self::assertStringContainsString($part2, $body); |
| 86 | + } |
| 87 | + |
| 88 | + private function buildEmail(string $template): TemplatedEmail |
| 89 | + { |
| 90 | + $email = (new TemplatedEmail()) |
| 91 | + ->from('a.hofbauer@fify.at') |
| 92 | + ->htmlTemplate($template); |
| 93 | + |
| 94 | + $loader = new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'); |
| 95 | + $loader->addPath(\dirname(__DIR__).'/Fixtures/assets', 'assets'); |
| 96 | + |
| 97 | + $environment = new Environment($loader); |
| 98 | + $renderer = new BodyRenderer($environment); |
| 99 | + $renderer->render($email); |
| 100 | + |
| 101 | + return $email; |
| 102 | + } |
| 103 | +} |
0 commit comments