Skip to content

Commit

Permalink
[Mime] Don't require passig the encoder name to TextPart
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Nov 5, 2024
1 parent e57faea commit c73ca0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ private function getEncoder(): ContentEncoderInterface
return self::$encoders[$this->encoding];
}

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

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

private function chooseEncoding(): string
Expand Down
18 changes: 13 additions & 5 deletions Tests/Part/TextPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,29 @@ public function testEncoding()
public function testCustomEncoderNeedsToRegisterFirst()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The encoding must be one of "quoted-printable", "base64", "8bit", "exception_test" ("upper_encoder" given).');
TextPart::addEncoder('exception_test', $this->createMock(ContentEncoderInterface::class));
new TextPart('content', 'utf-8', 'plain', 'upper_encoder');
$this->expectExceptionMessage('The encoding must be one of "quoted-printable", "base64", "8bit", "upper_encoder" ("this_encoding_does_not_exist" given).');

$upperEncoder = $this->createMock(ContentEncoderInterface::class);
$upperEncoder->method('getName')->willReturn('upper_encoder');

TextPart::addEncoder($upperEncoder);
new TextPart('content', 'utf-8', 'plain', 'this_encoding_does_not_exist');
}

public function testOverwriteDefaultEncoder()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You are not allowed to change the default encoders ("quoted-printable", "base64", and "8bit").');
TextPart::addEncoder('8bit', $this->createMock(ContentEncoderInterface::class));

$base64Encoder = $this->createMock(ContentEncoderInterface::class);
$base64Encoder->method('getName')->willReturn('base64');

TextPart::addEncoder($base64Encoder);
}

public function testCustomEncoding()
{
TextPart::addEncoder('upper_encoder', new class implements ContentEncoderInterface {
TextPart::addEncoder(new class implements ContentEncoderInterface {
public function encodeByteStream($stream, int $maxLineLength = 0): iterable
{
$filter = stream_filter_append($stream, 'string.toupper', \STREAM_FILTER_READ);
Expand Down

0 comments on commit c73ca0f

Please sign in to comment.