onMessage example

use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Message;

class MessengerTransportListenerTest extends TestCase
{
    public function testNoMessengerTransportStampsByDefault()
    {
        $l = new MessengerTransportListener();
        $envelope = new Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]);
        $message = new Message(new Headers());
        $event = new MessageEvent($message$envelope, 'smtp', true);
        $l->onMessage($event);
        $this->assertEmpty($event->getStamps());
    }

    public function testMessengerTransportStampViaHeader()
    {
        $l = new MessengerTransportListener();
        $envelope = new Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]);
        $headers = (new Headers())->addTextHeader('X-Bus-Transport', 'async');
        $message = new Message($headers);
        $event = new MessageEvent($message$envelope, 'smtp', true);
        $l->onMessage($event);
        
class MessageListenerTest extends TestCase
{
    /** * @dataProvider provideHeaders */
    public function testHeaders(Headers $initialHeaders, Headers $defaultHeaders, Headers $expectedHeaders, array $rules = MessageListener::DEFAULT_RULES)
    {
        $message = new Message($initialHeaders);
        $listener = new MessageListener($defaultHeaders, null, $rules);
        $event = new MessageEvent($messagenew Envelope(new Address('sender@example.com')[new Address('recipient@example.com')]), 'smtp');
        $listener->onMessage($event);

        $this->assertEquals($expectedHeaders$event->getMessage()->getHeaders());
    }

    public static function provideHeaders(): iterable
    {
        $initialHeaders = new Headers();
        $defaultHeaders = (new Headers())
            ->add(new MailboxListHeader('from', [new Address('from-default@example.com')]))
        ;
        yield 'No defaults, all headers copied over' => [$initialHeaders$defaultHeaders$defaultHeaders];

        
Home | Imprint | This part of the site doesn't use cookies.