Skip to content

Commit

Permalink
Ensured special headers are only added once
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiergummi committed Jul 13, 2022
1 parent adf86cb commit 7e91653
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/Messages/HeaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function setReplyTo(string|array $replyTo): void
$target = $this->resolveTarget($replyTo);

if ($target) {
$this->addHeader('reply-to', $target);
$this->addHeader('reply-to', $target, true);
}
}

Expand All @@ -81,24 +81,29 @@ public function setReturnPath(string|array $returnPath): void
$target = $this->resolveTarget($returnPath);

if ($target) {
$this->addHeader('return-path', $target);
$this->addHeader('return-path', $target, true);
}
}

/**
* Adds a header to the message.
*
* @param string $name Name of the header.
* @param string|string[] $value Value of the header, or multiple values as
* an array.
* @param string $name Name of the header.
* @param string|string[] $value Value of the header, or multiple values
* as an array.
* @param bool $replace Whether any existing header value should
* be replaced with the new value.
*
* @psalm-suppress MixedPropertyTypeCoercion
*/
public function addHeader(string $name, string|array $value): void
{
public function addHeader(
string $name,
string|array $value,
bool $replace = false
): void {
$name = $this->normalizeHeaderName($name);

if ( ! $this->hasHeader($name)) {
if ($replace || ! $this->hasHeader($name)) {
$this->headers[$name] = [];
}

Expand Down Expand Up @@ -148,16 +153,21 @@ public function hasReturnPath(): bool
/**
* Sets a single header.
*
* @param string $name Name of the header.
* @param string|string[] $value Value of the header, or multiple values as
* an array.
* @param string $name Name of the header.
* @param string|string[] $value Value of the header, or multiple values
* as an array.
* @param bool $replace Whether any existing header value should
* be replaced with the new value.
*
* @return T Instance for chaining.
* @see self::addHeader()
*/
public function header(string $name, string|array $value): static
{
$this->addHeader($name, $value);
public function header(
string $name,
string|array $value,
bool $replace = false
): static {
$this->addHeader($name, $value, $replace);

return $this;
}
Expand Down Expand Up @@ -198,11 +208,11 @@ public function removeHeader(string $name): void
* @param string|array $replyTo Reply to address.
*
* @return T Instance for chaining.
* @see self::replyTo()
* @see self::setReplyTo()
*/
public function replyTo(string|array $replyTo): static
{
$this->replyTo($replyTo);
$this->setReplyTo($replyTo);

return $this;
}
Expand All @@ -213,7 +223,7 @@ public function replyTo(string|array $replyTo): static
* @param string|array $returnPath Reply to address.
*
* @return T Instance for chaining.
* @see self::returnPath()
* @see self::setReturnPath()
*/
public function returnPath(string|array $returnPath): static
{
Expand Down
117 changes: 117 additions & 0 deletions tests/Unit/Messages/MailgunTemplatedMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@

class MailgunTemplatedMessageTest extends TestCase
{
/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::getHeaders
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::header
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::addHeader
*/
public function testAppendsHeaders(): void
{
$message = new MailgunTemplatedMessage('foo');

self::assertEmpty($message->getHeaders());
$message->header('foo', 'bar');
self::assertEquals(['bar'], $message->getHeaders()['foo']);
$message->addHeader('foo', 'baz');
self::assertEquals(['bar', 'baz'], $message->getHeaders()['foo']);
$message->addHeader('foo', 'quz', true);
self::assertEquals(['quz'], $message->getHeaders()['foo']);
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
Expand Down Expand Up @@ -526,6 +546,103 @@ public function testRecipientSetting(): void
self::assertSame('bar', $message->getRecipient());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws JsonException
* @throws \PHPUnit\Framework\Exception
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::hasHeader
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::toArray
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::addHeader
*/
public function testRemovesPrefixFromHeaders(): void
{
$message = new MailgunTemplatedMessage('foo');
$message->addHeader('h:foo', 'bar');
self::assertTrue($message->hasHeader('foo'));
self::assertArrayHasKey('h:foo', $message->toArray());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws JsonException
* @throws \PHPUnit\Framework\Exception
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::hasOption
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::toArray
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::addOption
*/
public function testRemovesPrefixFromOptions(): void
{
$message = new MailgunTemplatedMessage('foo');
$message->addOption('o:foo', 'bar');
self::assertTrue($message->hasOption('foo'));
self::assertFalse($message->hasOption('o:foo'));
self::assertArrayHasKey('o:foo', $message->toArray());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws JsonException
* @throws \PHPUnit\Framework\Exception
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::hasParam
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::toArray
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::addParam
*/
public function testRemovesPrefixFromParams(): void
{
$message = new MailgunTemplatedMessage('foo');
$message->addParam('v:foo', 'bar');
self::assertTrue($message->hasParam('foo'));
self::assertFalse($message->hasParam('v:foo'));
self::assertArrayHasKey('v:foo', $message->toArray());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::replyTo
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::setReplyTo
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::hasReplyTo
*/
public function testReplyToSetting(): void
{
$message = new MailgunTemplatedMessage('foo');

self::assertFalse($message->hasReplyTo());
self::assertFalse($message->hasHeader('return-path'));
self::assertNull($message->getHeaders()['return-path'] ?? null);
$message->setReplyTo('foo');
self::assertEquals(['foo'], $message->getHeaders()['reply-to']);
self::assertTrue($message->hasReplyTo());
$message->replyTo('bar');
self::assertEquals(['bar'], $message->getHeaders()['reply-to']);
self::assertTrue($message->hasReplyTo());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::returnPath
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::setReturnPath
* @covers \Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage::hasReturnPath
*/
public function testReturnPathSetting(): void
{
$message = new MailgunTemplatedMessage('foo');

self::assertFalse($message->hasReturnPath());
self::assertFalse($message->hasHeader('return-path'));
self::assertNull($message->getHeaders()['return-path'] ?? null);
$message->setReturnPath('foo');
self::assertEquals(['foo'], $message->getHeaders()['return-path']);
self::assertTrue($message->hasReturnPath());
$message->returnPath('bar');
self::assertEquals(['bar'], $message->getHeaders()['return-path']);
self::assertTrue($message->hasReturnPath());
}

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
Expand Down

0 comments on commit 7e91653

Please sign in to comment.