RouterContextStamp example

private RequestContextAwareInterface $router;

    public function __construct(RequestContextAwareInterface $router)
    {
        $this->router = $router;
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        if (!$envelope->last(ConsumedByWorkerStamp::class) || !$contextStamp = $envelope->last(RouterContextStamp::class)) {
            $context = $this->router->getContext();
            $envelope = $envelope->with(new RouterContextStamp(
                $context->getBaseUrl(),
                $context->getMethod(),
                $context->getHost(),
                $context->getScheme(),
                $context->getHttpPort(),
                $context->getHttpsPort(),
                $context->getPathInfo(),
                $context->getQueryString()
            ));

            return $stack->next()->handle($envelope$stack);
        }
$router = $this->createMock(RequestContextAwareInterface::class);
        $context = new RequestContext('', 'POST', 'github.com');

        $router
            ->expects($this->once())
            ->method('getContext')
            ->willReturn($context);

        $middleware = new RouterContextMiddleware($router);
        $envelope = new Envelope(new \stdClass()[
            new ConsumedByWorkerStamp(),
            new RouterContextStamp('', 'GET', 'symfony.com', 'https', 80, 443, '/', ''),
        ]);

        $nextMiddleware = $this->createMock(MiddlewareInterface::class);
        $nextMiddleware
            ->expects($this->once())
            ->method('handle')
            ->willReturnCallback(function DEnvelope $envelope, StackInterface $stack) use ($context): Envelope {
                $this->assertSame('symfony.com', $context->getHost());

                return $envelope;
            })
        ;
Home | Imprint | This part of the site doesn't use cookies.