RequestContext example

public function testForwardToRoute(): void
    {
        $router = static::createMock(RouterInterface::class);
        $router
            ->expects(static::once())
            ->method('generate')
            ->with('foo', ['foo' => 'bar'], Router::PATH_INFO)
            ->willReturn('/foo/generated');

        $router
            ->method('getContext')
            ->willReturn(new RequestContext(method: Request::METHOD_POST));

        $router
            ->expects(static::once())
            ->method('match')
            ->with('/foo/generated')
            ->willReturn(['_controller' => 'test_controller']);

        $request = new Request(
            [
                'forwardTo' => 'foo',
                'forwardParameters' => ['foo' => 'bar'],
            ]
$stack = $this->getContainer()->get('request_stack');

        // remove all request from stack         while ($stack->pop()) {
        }

        $stack->push($request);

        $router = $this->getContainer()->get('router');
        $context = $router->getContext();
        $router->setContext(new RequestContext('', 'GET', $case->host));

        $url = $router->generate($case->route, $case->params, $case->type);

        static::assertSame($case->expected, $url);

        while ($stack->pop()) {
        }

        $router->setContext($context);
    }

    
'host' => ['router.test', 'router.test:8000'],
            'subDir' => ['', '/public', '/sw/public'],
            'salesChannel' => ['', '/de', '/de/premium', '/public'],
        ];
        $cases = self::generateCases(array_keys($config)$config);

        return array_map(fn ($params) => [self::createCase($params['https']$params['host']$params['subDir']$params['salesChannel'])]$cases);
    }

    private function getContext(Request $request): RequestContext
    {
        return new RequestContext(
            $request->getBaseUrl(),
            $request->getMethod(),
            $request->getHost(),
            $request->getScheme(),
            (!$request->isSecure() && $request->getPort() !== 80) ? $request->getPort() : 80,
            ($request->isSecure() && $request->getPort() !== 443) ? $request->getPort() : 443,
            $request->getPathInfo(),
            ''
        );
    }

    

        $language = new LanguageEntity();
        $language->setUniqueIdentifier(Uuid::randomHex());
        $scDomain = new SalesChannelDomainEntity();
        $scDomain->setUniqueIdentifier(Uuid::randomHex());
        $scDomain->setUrl('http://localhost');
        $language->setSalesChannelDomains(new SalesChannelDomainCollection([$scDomain]));

        $changeLangMock = $this->createMock(AbstractChangeLanguageRoute::class);

        $routerMock = $this->createMock(RouterInterface::class);
        $routerMock->expects(static::once())->method('getContext')->willReturn(new RequestContext());
        $routerMock->expects(static::once())->method('generate')->willReturn('http://localhost');
        $requestStackMock = $this->createMock(RequestStack::class);
        $requestStackMock->expects(static::exactly(2))->method('getMainRequest')->willReturn(new Request());

        $contextSwitchRoute = $this->createMock(ContextSwitchRoute::class);
        $contextSwitchRoute->expects(static::once())->method('switchContext')->willReturn(
            new ContextTokenResponse(Uuid::randomHex(), 'http://localhost')
        );

        $controller = new ContextController(
            $contextSwitchRoute,
            
Home | Imprint | This part of the site doesn't use cookies.