Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] Testing forms causes exeption "There is currently no session available." #2177

Closed
daFish opened this issue Sep 17, 2024 · 8 comments · Fixed by #2251
Closed

Comments

@daFish
Copy link
Contributor

daFish commented Sep 17, 2024

I'm trying to test my live component which contains a form. The following code raises an exception of type Symfony\Component\HttpFoundation\Exception\SessionNotFoundException.

My sample testcode:

$formComponent = $this->createLiveComponent(
        name: ProductionForm::class,
)->actingAs($testUser);

$formComponent->set('form_name.title', 'Loreum Ipsum');

The user and the field exist. What else is needed to get the test working?

@daFish
Copy link
Contributor Author

daFish commented Sep 17, 2024

Forgot to add: I‘m using the latest versions of Symfony and the UX components.

@smnandre
Copy link
Member

Hi @daFish !

I'd need a bit more information to help you I think, as I dont see right now what could be the problem. Could you maybe share some code from your test and/or your component?

Or, even better, if you could create a minimal bug reproducer ?

@daFish
Copy link
Contributor Author

daFish commented Sep 18, 2024

Hi @smnandre,

I have created a reproducer. The commit with all relevant changes is daFish/symfony-ux-issue-2177@12cc45f

The project has been created from the Symfony demo application. Running the tests with vendor/bin/phpunit tests/Components/ReportComponentTest.php fails with the above exception.

@smnandre
Copy link
Member

@daFish thank you for creating this ideal reproducer 😃

I'll look at it tonight!

@smnandre
Copy link
Member

It seems to occur when the CSRF token is generated, but i'm not too sure why right now.

@daFish
Copy link
Contributor Author

daFish commented Sep 25, 2024

No change with the latest releases of Symfony UX packages.

@daFish
Copy link
Contributor Author

daFish commented Oct 9, 2024

Ok, after some digging and with the help of Fracsi in Slack I got to the point where the exception is no longer thrown:

$request = new Request();
$request->setSession($this->createMock(SessionInterface::class));
self::getContainer()->get(RequestStack::class)->push($request);

Adding this boilerplate code before creating the live component in the test solved it. However, I was under the impression that something like this isn't needed at all.

@mkrauser
Copy link

I have this issue as well. This occurs only in Components using the ComponentWithFormsTrait.

In ComponentsWithFormsTrait::initializeForm (which is called PostMount), the FormView is generated, which in turn creates a CSRF-Token.

When running Tests, there is no Request on the RequestStack. That's why the Session-Object cannot be fetched from the RequestStack.

There is already a PR for this: #2254

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants