getTransport example

use Symfony\Component\Messenger\Bridge\Beanstalkd\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransport;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

final class BeanstalkdTransportTest extends TestCase
{
    public function testItIsATransport()
    {
        $transport = $this->getTransport();

        $this->assertInstanceOf(TransportInterface::class$transport);
    }

    public function testReceivesMessages()
    {
        $transport = $this->getTransport(
            $serializer = $this->createMock(SerializerInterface::class),
            $connection = $this->createMock(Connection::class)
        );

        
/** * @dataProvider messageDataProvider */
    public function testCanBeConstructed(MessageInterface $message)
    {
        $nullMessage = new NullMessage($message);

        $this->assertSame($message->getSubject()$nullMessage->getSubject());
        $this->assertSame($message->getRecipientId()$nullMessage->getRecipientId());
        $this->assertSame($message->getOptions()$nullMessage->getOptions());

        (null === $message->getTransport())
            ? $this->assertSame('null', $nullMessage->getTransport())
            : $this->assertSame($message->getTransport()$nullMessage->getTransport());
    }

    public static function messageDataProvider(): \Generator
    {
        yield [new DummyMessageWithoutTransport()];
        yield [new DummyMessageWithTransport()];
    }
}
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

/** * @requires extension amqp */
class AmqpTransportTest extends TestCase
{
    public function testItIsATransport()
    {
        $transport = $this->getTransport();

        $this->assertInstanceOf(TransportInterface::class$transport);
    }

    public function testReceivesMessages()
    {
        $transport = $this->getTransport(
            $serializer = $this->createMock(SerializerInterface::class),
            $connection = $this->createMock(Connection::class)
        );

        

        $this->connection = $this->createMock(Connection::class);
        // Mocking the concrete receiver class because mocking multiple interfaces is deprecated         $this->receiver = $this->createMock(AmazonSqsReceiver::class);
        $this->sender = $this->createMock(SenderInterface::class);

        $this->transport = new AmazonSqsTransport($this->connection, null, $this->receiver, $this->sender);
    }

    public function testItIsATransport()
    {
        $transport = $this->getTransport();

        $this->assertInstanceOf(TransportInterface::class$transport);
    }

    public function testReceivesMessages()
    {
        $transport = $this->getTransport(
            $serializer = $this->createMock(SerializerInterface::class),
            $connection = $this->createMock(Connection::class)
        );

        
$this->assertStringEndsWith('/sendMessage', $url);
            $this->assertSame($expectedBodyjson_decode($options['body'], true));

            return $response;
        });

        $transport = self::createTransport($client, 'testChannel');

        $sentMessage = $transport->send(new ChatMessage('testMessage'));

        $this->assertEquals(1, $sentMessage->getMessageId());
        $this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
    }

    public function testSendWithOptionForEditMessage()
    {
        $response = $this->createMock(ResponseInterface::class);
        $response->expects($this->exactly(2))
            ->method('getStatusCode')
            ->willReturn(200);

        $content = <<<JSON { "ok": true, "result": { "message_id": 1, "from": { "id": 12345678, "first_name": "YourBot", "username": "YourBot" }, "chat": { "id": 1234567890, "first_name": "John", "last_name": "Doe", "username": "JohnDoe", "type": "private" }, "date": 1459958199, "text": "Hello from Bot!" } }
foreach ($this->transports as $transport) {
            if ($transport->supports($message)) {
                return true;
            }
        }

        return false;
    }

    public function send(MessageInterface $message): SentMessage
    {
        if (!$transport = $message->getTransport()) {
            foreach ($this->transports as $transport) {
                if ($transport->supports($message)) {
                    return $transport->send($message);
                }
            }
            throw new LogicException(sprintf('None of the available transports support the given message (available transports: "%s").', implode('", "', array_keys($this->transports))));
        }

        if (!isset($this->transports[$transport])) {
            throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transportimplode('", "', array_keys($this->transports))));
        }

        


        $message = new ChatMessage('subject');

        $one->method('supports')->with($message)->willReturn(true);

        $one->expects($this->once())->method('send')->willReturn(new SentMessage($message, 'one'));

        $sentMessage = $transports->send($message);

        $this->assertSame($message$sentMessage->getOriginalMessage());
        $this->assertSame('one', $sentMessage->getTransport());
    }

    public function testSendToFirstSupportedTransportIfMessageDoesNotDefineATransport()
    {
        $transports = new Transports([
            'one' => $one = $this->createMock(TransportInterface::class),
            'two' => $two = $this->createMock(TransportInterface::class),
        ]);

        $message = new ChatMessage('subject');

        

        return $this->decoratedMessage->getSubject();
    }

    public function getOptions(): ?MessageOptionsInterface
    {
        return $this->decoratedMessage->getOptions();
    }

    public function getTransport(): ?string
    {
        return $this->decoratedMessage->getTransport() ?? 'null';
    }
}

    private array $events = [];

    /** * @var array<string, bool> */
    private array $transports = [];

    public function add(MessageEvent $event): void
    {
        $this->events[] = $event;
        $this->transports[$event->getTransport()] = true;
    }

    public function getTransports(): array
    {
        return array_keys($this->transports);
    }

    /** * @return MessageEvent[] */
    public function getEvents(string $name = null): array
    {
use Symfony\Component\Messenger\Bridge\Doctrine\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\Connection;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransport;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

class DoctrineTransportTest extends TestCase
{
    public function testItIsATransport()
    {
        $transport = $this->getTransport();

        $this->assertInstanceOf(TransportInterface::class$transport);
    }

    public function testReceivesMessages()
    {
        $transport = $this->getTransport(
            $serializer = $this->createMock(SerializerInterface::class),
            $connection = $this->createMock(Connection::class)
        );

        
/** * @author Fabien Potencier <fabien@symfony.com> */
class NotificationEvents
{
    private array $events = [];
    private array $transports = [];

    public function add(MessageEvent $event): void
    {
        $this->events[] = $event;
        $this->transports[(string) $event->getMessage()->getTransport()] = true;
    }

    public function getTransports(): array
    {
        return array_keys($this->transports);
    }

    /** * @return MessageEvent[] */
    public function getEvents(string $name = null): array
    {
$message = new PushMessage('Hello', 'World');
        $message->content('dlrow olleH');

        $this->assertSame('dlrow olleH', $message->getContent());
    }

    public function testSetTransport()
    {
        $message = new PushMessage('Hello', 'World');
        $message->transport('next_one');

        $this->assertSame('next_one', $message->getTransport());
    }

    public function testCreateFromNotification()
    {
        $notification = new Notification('Hello');
        $notification->content('World');

        $message = PushMessage::fromNotification($notification);

        $this->assertSame('Hello', $message->getSubject());
        $this->assertSame('World', $message->getContent());
        
use Symfony\Component\Messenger\Bridge\Redis\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Redis\Transport\Connection;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransport;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

class RedisTransportTest extends TestCase
{
    public function testItIsATransport()
    {
        $transport = $this->getTransport();

        $this->assertInstanceOf(TransportInterface::class$transport);
    }

    public function testReceivesMessages()
    {
        $transport = $this->getTransport(
            $serializer = $this->createMock(SerializerInterface::class),
            $connection = $this->createMock(Connection::class)
        );

        
public function toString(): string
    {
        return sprintf('is "%s"', $this->expectedText);
    }

    /** * @param MessageInterface $message */
    protected function matches($message): bool
    {
        return $message->getTransport() === $this->expectedText;
    }

    /** * @param MessageInterface $message */
    protected function failureDescription($message): string
    {
        return 'the Notification transport '.$this->toString();
    }
}
Home | Imprint | This part of the site doesn't use cookies.