|
12 | 12 | namespace Symfony\Component\Mime\Tests\Part;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\Mime\Encoder\ContentEncoderInterface; |
| 16 | +use Symfony\Component\Mime\Exception\InvalidArgumentException; |
| 17 | +use Symfony\Component\Mime\Exception\RuntimeException; |
15 | 18 | use Symfony\Component\Mime\Header\Headers;
|
16 | 19 | use Symfony\Component\Mime\Header\ParameterizedHeader;
|
17 | 20 | use Symfony\Component\Mime\Header\UnstructuredHeader;
|
@@ -87,6 +90,57 @@ public function testEncoding()
|
87 | 90 | ), $p->getPreparedHeaders());
|
88 | 91 | }
|
89 | 92 |
|
| 93 | + public function testCustomEncoderNeedsToRegisterFirst() |
| 94 | + { |
| 95 | + $this->expectException(InvalidArgumentException::class); |
| 96 | + $this->expectExceptionMessage('The encoding must be one of "quoted-printable", "base64", "8bit", "exception_test" ("upper_encoder" given).'); |
| 97 | + TextPart::addEncoder('exception_test', $this->createMock(ContentEncoderInterface::class)); |
| 98 | + new TextPart('content', 'utf-8', 'plain', 'upper_encoder'); |
| 99 | + } |
| 100 | + |
| 101 | + public function testOverwriteDefaultEncoder() |
| 102 | + { |
| 103 | + $this->expectException(InvalidArgumentException::class); |
| 104 | + $this->expectExceptionMessage('You are not allowed to change the default encoders ("quoted-printable","base64","8bit"). If you want to modify their behaviour please register and use a new encoder.'); |
| 105 | + TextPart::addEncoder('8bit', $this->createMock(ContentEncoderInterface::class)); |
| 106 | + } |
| 107 | + |
| 108 | + public function testCustomEncoding() |
| 109 | + { |
| 110 | + TextPart::addEncoder('upper_encoder', new class() implements ContentEncoderInterface { |
| 111 | + public function encodeByteStream($stream, int $maxLineLength = 0): iterable |
| 112 | + { |
| 113 | + $filter = stream_filter_append($stream, 'string.toupper', \STREAM_FILTER_READ); |
| 114 | + if (!\is_resource($filter)) { |
| 115 | + throw new RuntimeException('Unable to set the upper content encoder to the filter.'); |
| 116 | + } |
| 117 | + |
| 118 | + while (!feof($stream)) { |
| 119 | + yield fread($stream, 16372); |
| 120 | + } |
| 121 | + stream_filter_remove($filter); |
| 122 | + } |
| 123 | + |
| 124 | + public function getName(): string |
| 125 | + { |
| 126 | + return 'upper_encoder'; |
| 127 | + } |
| 128 | + |
| 129 | + public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string |
| 130 | + { |
| 131 | + return strtoupper($string); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + $p = new TextPart('content', 'utf-8', 'plain', 'upper_encoder'); |
| 136 | + $this->assertEquals('CONTENT', $p->bodyToString()); |
| 137 | + $this->assertEquals('CONTENT', implode('', iterator_to_array($p->bodyToIterable()))); |
| 138 | + $this->assertEquals(new Headers( |
| 139 | + new ParameterizedHeader('Content-Type', 'text/plain', ['charset' => 'utf-8']), |
| 140 | + new UnstructuredHeader('Content-Transfer-Encoding', 'upper_encoder') |
| 141 | + ), $p->getPreparedHeaders()); |
| 142 | + } |
| 143 | + |
90 | 144 | public function testSerialize()
|
91 | 145 | {
|
92 | 146 | $r = fopen('php://memory', 'r+', false);
|
|
0 commit comments