Skip to content

Commit c73ca0f

Browse files
committed
[Mime] Don't require passig the encoder name to TextPart
1 parent e57faea commit c73ca0f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Part/TextPart.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ private function getEncoder(): ContentEncoderInterface
220220
return self::$encoders[$this->encoding];
221221
}
222222

223-
public static function addEncoder(string $name, ContentEncoderInterface $encoder): void
223+
public static function addEncoder(ContentEncoderInterface $encoder): void
224224
{
225-
if (\in_array($name, self::DEFAULT_ENCODERS, true)) {
225+
if (\in_array($encoder->getName(), self::DEFAULT_ENCODERS, true)) {
226226
throw new InvalidArgumentException('You are not allowed to change the default encoders ("quoted-printable", "base64", and "8bit").');
227227
}
228228

229-
self::$encoders[$name] = $encoder;
229+
self::$encoders[$encoder->getName()] = $encoder;
230230
}
231231

232232
private function chooseEncoding(): string

Tests/Part/TextPartTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,29 @@ public function testEncoding()
103103
public function testCustomEncoderNeedsToRegisterFirst()
104104
{
105105
$this->expectException(InvalidArgumentException::class);
106-
$this->expectExceptionMessage('The encoding must be one of "quoted-printable", "base64", "8bit", "exception_test" ("upper_encoder" given).');
107-
TextPart::addEncoder('exception_test', $this->createMock(ContentEncoderInterface::class));
108-
new TextPart('content', 'utf-8', 'plain', 'upper_encoder');
106+
$this->expectExceptionMessage('The encoding must be one of "quoted-printable", "base64", "8bit", "upper_encoder" ("this_encoding_does_not_exist" given).');
107+
108+
$upperEncoder = $this->createMock(ContentEncoderInterface::class);
109+
$upperEncoder->method('getName')->willReturn('upper_encoder');
110+
111+
TextPart::addEncoder($upperEncoder);
112+
new TextPart('content', 'utf-8', 'plain', 'this_encoding_does_not_exist');
109113
}
110114

111115
public function testOverwriteDefaultEncoder()
112116
{
113117
$this->expectException(InvalidArgumentException::class);
114118
$this->expectExceptionMessage('You are not allowed to change the default encoders ("quoted-printable", "base64", and "8bit").');
115-
TextPart::addEncoder('8bit', $this->createMock(ContentEncoderInterface::class));
119+
120+
$base64Encoder = $this->createMock(ContentEncoderInterface::class);
121+
$base64Encoder->method('getName')->willReturn('base64');
122+
123+
TextPart::addEncoder($base64Encoder);
116124
}
117125

118126
public function testCustomEncoding()
119127
{
120-
TextPart::addEncoder('upper_encoder', new class implements ContentEncoderInterface {
128+
TextPart::addEncoder(new class implements ContentEncoderInterface {
121129
public function encodeByteStream($stream, int $maxLineLength = 0): iterable
122130
{
123131
$filter = stream_filter_append($stream, 'string.toupper', \STREAM_FILTER_READ);

0 commit comments

Comments
 (0)