RequestStack example

use Symfony\Component\HttpFoundation\RequestStack;

/** * @internal * * @covers \Shopware\Core\Framework\Adapter\Twig\TemplateScopeDetector */
class TemplateScopeDetectorTest extends TestCase
{
    public function testDetectWithNoRequest(): void
    {
        $detector = new TemplateScopeDetector(new RequestStack());
        static::assertSame([TemplateScopeDetector::DEFAULT_SCOPE]$detector->getScopes());
    }

    public function testDetectWithEmptyRequest(): void
    {
        $stack = new RequestStack();
        $stack->push(new Request());
        $detector = new TemplateScopeDetector($stack);
        static::assertSame([TemplateScopeDetector::DEFAULT_SCOPE]$detector->getScopes());
    }

    
/** * @covers \Shopware\Core\Framework\Log\Monolog\AnnotatePackageProcessor * * @internal */
#[Package('cause')] class AnnotatePackageProcessorTest extends TestCase
{
    public function testOnlyController(): void
    {
        $requestStack = new RequestStack();
        $inner = $this->createMock(AbstractHandler::class);
        $container = $this->createMock(ContainerInterface::class);
        $handler = new AnnotatePackageProcessor($requestStack$container);

        $request = new Request();
        $request->attributes->set('_controller', TestController::class D '::load');
        $requestStack->push($request);

        $record = new LogRecord(
            new \DateTimeImmutable(),
            'business events',
            
->method('match')
            ->with('/foo/generated')
            ->willReturn(['_controller' => 'test_controller']);

        $request = new Request(
            [
                'forwardTo' => 'foo',
                'forwardParameters' => ['foo' => 'bar'],
            ]
        );

        $requestStack = new RequestStack();
        $requestStack->push($request);

        $controllerResolver = static::createMock(ControllerResolverInterface::class);
        $controllerResolver
            ->method('getController')
            ->willReturn(fn () => new Response('<html lang="en">test</html>', Response::HTTP_PERMANENTLY_REDIRECT, ['Content-Type' => 'text/html']));

        $kernel = new HttpKernel(
            static::createMock(EventDispatcherInterface::class),
            $controllerResolver,
            $requestStack,
        );


        static::assertSame($expected, CacheResponseSubscriber::getSubscribedEvents());
    }

    public function testNoHeadersAreSetIfCacheIsDisabled(): void
    {
        $subscriber = new CacheResponseSubscriber(
            $this->createMock(CartService::class),
            100,
            false,
            new MaintenanceModeResolver(new RequestStack()new CoreMaintenanceModeResolver(new EventDispatcher())),
            false,
            null,
            null
        );

        $customer = new CustomerEntity();
        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $salesChannelContext->method('getCustomer')->willReturn($customer);

        $request = new Request();
        $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT, $salesChannelContext);

        
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/** * @internal */
class ThemeAssetPackageTest extends TestCase
{
    public function testEmptyStack(): void
    {
        $asset = new ThemeAssetPackage(['http://localhost']new EmptyVersionStrategy()new RequestStack()new MD5ThemePathBuilder());

        static::assertSame('http://localhost/all.js', $asset->getUrl('/all.js'));
    }

    public function testAdminRequest(): void
    {
        $request = new Request();
        $stack = new RequestStack();
        $stack->push($request);

        $asset = new ThemeAssetPackage(['http://localhost']new EmptyVersionStrategy()$stacknew MD5ThemePathBuilder());

        
return $browser->getResponse();
    }

    /** * @param array<string, mixed> $data * * @return array<string, mixed> */
    public function tokenize(string $route, array $data): array
    {
        $requestStack = new RequestStack();
        $request = new Request();
        /** @var Session $session */
        $session = $this->getSession();
        $request->setSession($session);
        $requestStack->push($request);

        return $data;
    }

    abstract protected static function getKernel(): KernelInterface;

    
false,
            ],
            'maintenance mode is active, error controller requested, proxy' => [
                self::getRequest(true, false, true, false, false, true),
                false,
            ],
        ];
    }

    private function getRequestStack(?Request $main = null): RequestStack
    {
        $requestStack = new RequestStack();

        if ($main instanceof Request) {
            $requestStack->push($main);
        }

        return $requestStack;
    }

    /** * @param string[] $allowedIpAddresses */
    
$this->accountService,
            $this->listAddressRoute,
            $this->abstractUpsertAddressRoute,
            $this->deleteAddressRoute,
            $this->changeCustomerProfileRoute,
        );

        $translator = $this->createMock(TranslatorInterface::class);

        $translator->method('trans')->willReturnCallback(fn (string $key): string => $key);
        $containerBuilder = new ContainerBuilder();
        $containerBuilder->set('request_stack', new RequestStack());
        $containerBuilder->set('translator', $translator);
        $this->controller->setContainer($containerBuilder);
    }

    public function testAddressBook(): void
    {
        $context = Generator::createSalesChannelContext();
        $request = new Request();
        $dataBag = new RequestDataBag();

        $customer = new CustomerEntity();
        

class RequestFacadeFactoryTest extends TestCase
{
    public function testFactory(): void
    {
        $request = new Request();
        $request->server->set('REQUEST_URI', '/foo/bar');
        $request->attributes->set('sw-original-request-uri', 'https://example.com/foo/bar');
        $stack = new RequestStack();
        $stack->push($request);

        $factory = new RequestFacadeFactory($stack);

        static::assertSame('request', $factory->getName());

        $script = new Script('foo', 'bar', new \DateTimeImmutable());

        $facade = $factory->factory($this->createMock(Hook::class)$script);

        static::assertSame('https://example.com/foo/bar', $facade->uri());
    }

trait InstallerControllerTestTrait
{
    /** * @param array<string, object> $services */
    private function getInstallerContainer(Environment $twig, array $services = []): ContainerInterface
    {
        $container = new ContainerBuilder();
        $container->set('twig', $twig);
        $requestStack = new RequestStack();
        $requestStack->push(new Request([][]['_route' => 'installer.language-selection']));
        $container->set('request_stack', $requestStack);
        $container->setParameter('shopware.installer.supportedLanguages', ['en' => 'en-GB', 'de' => 'de-DE']);
        $container->setParameter('kernel.shopware_version', Kernel::SHOPWARE_FALLBACK_VERSION);

        foreach ($services as $id => $service) {
            $container->set($id$service);
        }

        return $container;
    }

    

class ThemeAssetPackageTest extends TestCase
{
    /** * @dataProvider urlCases */
    public function testGetUrl(string $inputUrl, ?Request $request, string $expectedUrl): void
    {
        $requestStack = new RequestStack();

        if ($request instanceof Request) {
            $requestStack->push($request);
        }

        $themeAssetPackage = new ThemeAssetPackage(
            ['http://localhost'],
            new StaticVersionStrategy('v1'),
            $requestStack,
            new MD5ThemePathBuilder()
        );

        

class CartMergedSubscriberTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testMergedHintIsAdded(): void
    {
        $session = new Session(new MockArraySessionStorage());
        $request = new Request();
        $request->setSession($session);
        $requestStack = new RequestStack();
        $requestStack->push($request);

        $translator = $this->createMock(TranslatorInterface::class);
        $translator->expects(static::once())
            ->method('trans')
            ->with('checkout.cart-merged-hint')
            ->willReturn('checkout.cart-merged-hint');

        $subscriber = new CartMergedSubscriber($translator$requestStack);

        $currentContextToken = 'currentToken';
        

class StoreSessionExpiredMiddlewareTest extends TestCase
{
    public function testReturnsResponseIfStatusCodeIsNotUnauthorized(): void
    {
        $response = new Response(200, [], '{"payload":"data"}');

        $middleware = new StoreSessionExpiredMiddleware(
            $this->createMock(Connection::class),
            new RequestStack(),
        );

        $handledResponse = $middleware($response);

        static::assertSame($response$handledResponse);
    }

    public function testReturnsResponseWithRewoundBodyIfCodeIsNotMatched(): void
    {
        $response = new Response(401, [], '{"payload":"data"}');

        


    public function testMediaUrlWithEmptyRequest(): void
    {
        $mediaEntity = new MediaEntity();
        $mediaEntity->assign(
            [
                'id' => Uuid::randomHex(),
                'fileName' => 'file.jpg',
            ]
        );
        $requestStack = new RequestStack();
        $requestStack->push(new Request());
        $urlGenerator = new UrlGenerator(new FilenamePathnameStrategy()new Filesystem(new InMemoryFilesystemAdapter()['public_url' => 'http://localhost:8000']));
        static::assertSame(
            'http://localhost:8000/media/d0/b3/24/file.jpg',
            $urlGenerator->getAbsoluteMediaUrl($mediaEntity)
        );
    }

    public function testAbsoluteThumbnailUrl(): void
    {
        $mediaEntity = new MediaEntity();
        
$this->accountLoginPageLoader,
            $passwordRecoveryPageLoader,
            $resetPasswordRoute,
            $this->loginRoute,
            $logoutRoute,
            $cartFacade,
            $recoverPasswordRoute,
            $this->salesChannelContextService,
        );

        $containerBuilder = new ContainerBuilder();
        $containerBuilder->set('request_stack', new RequestStack());
        $this->controller->setContainer($containerBuilder);
    }

    public function testAccountRegister(): void
    {
        $context = Generator::createSalesChannelContext();
        $context->assign(['customer' => null]);
        $request = new Request();
        $request->attributes->set('_route', 'frontend.account.login.page');
        $dataBag = new RequestDataBag();
        $page = new AccountLoginPage();

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