Skip to content

Commit

Permalink
Only inject a message id when there is none (#21)
Browse files Browse the repository at this point in the history
* only inject a message id when there is none
  • Loading branch information
frederikbosch authored Aug 24, 2017
1 parent 90efa43 commit 66e63f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Transport/InjectMessageIdHeaderTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function __construct(TransportInterface $transport, string $domainName)
*/
public function send(MessageInterface $message): void
{
$this->decoratedTransport->send(
$message->withHeader(
MessageId::newRandom($this->domainName)
)
);
if (!$message->hasHeader('Message-ID')) {
$message = $message->withHeader(MessageId::newRandom($this->domainName));
}

$this->decoratedTransport->send($message);
}
}
26 changes: 26 additions & 0 deletions test/Unit/Transport/InjectMessageIdHeaderTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Genkgo\TestMail\Unit\Transport;

use Genkgo\Mail\Header\MessageId;
use Genkgo\Mail\HeaderInterface;
use Genkgo\Mail\Transport\InjectMessageIdHeaderTransport;
use Genkgo\TestMail\AbstractTestCase;;
use Genkgo\Mail\GenericMessage;
Expand All @@ -29,4 +30,29 @@ public function it_injects_message_id_header_in_message()
$this->assertInstanceOf(MessageId::class, $storage[0]->getHeader('message-id')[0]);
}

/**
* @test
*/
public function it_does_not_inject_a_message_id_header_when_there_is_one()
{
$message = (new GenericMessage())
->withHeader(new MessageId('left', 'right'));

$storage = new \ArrayObject();

$transport = new InjectMessageIdHeaderTransport(
new ArrayObjectTransport($storage),
'domain'
);
$transport->send($message);

$this->assertCount(1, $storage);
$this->assertTrue($storage[0]->hasHeader('message-id'));
$this->assertInstanceOf(MessageId::class, $storage[0]->getHeader('message-id')[0]);

/** @var HeaderInterface $header */
$header = $storage[0]->getHeader('message-id')[0];
$this->assertEquals('<left@right>', (string)$header->getValue());
}

}

0 comments on commit 66e63f3

Please sign in to comment.