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,