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

How to access Session variable #149

Open
cooldude77 opened this issue May 27, 2024 · 5 comments
Open

How to access Session variable #149

cooldude77 opened this issue May 27, 2024 · 5 comments

Comments

@cooldude77
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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: symfony/symfony#45662 (comment)

@kbond
Copy link
Member

kbond commented Jun 1, 2024

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) {
    // ...
})

@hubertinio
Copy link

hubertinio commented Nov 8, 2024

Maybe you can create correct session variables "one step before". This is integration tests...

For example CartControllerTest

public function test_list_items(): void
    {
        $user = UserFactory::createOne();
        $product = ProductFactory::randomOrCreate();
        $product2 = ProductFactory::randomOrCreate();
        $productPageUrl = '/product/' . $product->id();
        $productPageUrl2 = '/product/' . $product->id();

        $this->browser()
            ->actingAs($user)
            ->interceptRedirects()
            ->visit($productPageUrl)
            ->assertSuccessful()
            ->click('Add to cart')
            ->visit($productPageUrl2)
            ->assertSuccessful()
            ->click('Add to cart')
            ->assertRedirectedTo('/cart')
            ->assertSuccessful()
            ->assertSee($product->_real()->getName())
            ->assertSee($product2->_real()->getName())
            ->assertSee('Product added to cart')
            ->assertSeeIn('btn-primary', 'Order')
        ;
    }

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

No branches or pull requests

4 participants