From 5b874d65a4340e6c3d83e4b008282c8e44ac6785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20St=C3=B6hr?= Date: Wed, 9 Oct 2024 19:53:54 +0200 Subject: [PATCH] fix: add empty request to help with live component tests --- .../src/Test/InteractsWithLiveComponents.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/LiveComponent/src/Test/InteractsWithLiveComponents.php b/src/LiveComponent/src/Test/InteractsWithLiveComponents.php index 377fbad1f34..cec75265ab8 100644 --- a/src/LiveComponent/src/Test/InteractsWithLiveComponents.php +++ b/src/LiveComponent/src/Test/InteractsWithLiveComponents.php @@ -13,6 +13,10 @@ use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorageFactory; use Symfony\UX\TwigComponent\ComponentFactory; /** @@ -34,6 +38,19 @@ protected function createLiveComponent(string $name, array $data = [], ?KernelBr throw new \LogicException(\sprintf('The "%s" component is not a live component.', $name)); } + /** @var RequestStack $requestStack */ + $requestStack = self::getContainer()->get(RequestStack::class); + if (null === $requestStack->getMainRequest() && null === $requestStack->getCurrentRequest()) { + $request = new Request(); + + /** @var MockFileSessionStorageFactory $sessionFactory */ + $sessionFactory = self::getContainer()->get('session.storage.factory.mock_file'); + $session = new Session($sessionFactory->createStorage($request)); + + $request->setSession($session); + $requestStack->push($request); + } + return new TestLiveComponent( $metadata, $data,