zenstruck / browser

A fluent interface for your Symfony functional tests.
MIT License
185 stars 17 forks source link

How to access Session variable #149

Open cooldude77 opened 2 months ago

cooldude77 commented 2 months ago

Hi, For testing purposes , I need to access session and set a value to it .

The class Browser has marked session as protected . I could not find in documentation any reference to session in any method

Thanks

nikophil commented 2 months ago

hello @cooldude77

by "session", do you mean "http session"? Browser does not deal with http session. And the method Browser::session() refers do the "mink session" which is a different concept

cooldude77 commented 2 months ago

Hey @nikophil ,

Sorry for not explaining it fully.

I wish to test how controller logic behave when I set a session variable ( for eg. a filled cart in a web shop or an empty cart).

Here I would like to set the session variable which I access in controller using RequestStack getRequest() method in my controller method and then getting the session from it. Based on values in session, controller performs a logic.

I understand that the browser does not have session because it is handled by Mink. Will that be the same session as I explained above and can I manipulate it somewhere in test case ?

Thanks.

nikophil commented 2 months ago

currently, browser does not provide a way to manipulate the session. Maybe this could be a nice addition to this lib? ping @kbond

For now, maybe you could use this trick: https://github.com/symfony/symfony/discussions/45662#discussioncomment-4352880

kbond commented 2 months ago

Yes, this would be a nice addition.

You can currently access the "cookie jar":

$browser->use(CookieJar $cookies) {
    // ...
})

I use it successfully to expire the current session:

$browser->use(function(CookieJar $cookieJar) {
    $cookieJar->expire('MOCKSESSID');
})

It would be nice if we could do the following:

$browser->use(SessionInterface $session) {
    // ...
})