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

There is currently no session available. #41654

Closed
thewalkingcoder opened this issue Jun 10, 2021 · 7 comments
Closed

There is currently no session available. #41654

thewalkingcoder opened this issue Jun 10, 2021 · 7 comments

Comments

@thewalkingcoder
Copy link

I migrate symfony 5.2.x to symfony 5.3.1

I want to fix deprecated message :

Since symfony/framework-bundle 5.3: The "Symfony\Component\HttpFoundation\Session\SessionInterface" and "SessionInterface" aliases are deprecated, use "$requestStack->getSession()" instead. It is being referenced by the

I change on my service

 public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

to

 public function __construct(RequestStack $requestStack)
    {
        $this->session = $requestStack->getSession();
    }

my service is a listener call on kernel.event_listener

tags:
    - { name: kernel.event_listener, priority: -10 }
   

the result is a

image

I read similary issue #40778 but can't understand how to resolve that on my listener.
Problem's is the listener ? priority ?

I callback to SessionInterface temporarily

@jderusse do you have idea ?

@jderusse
Copy link
Member

You should not store state in your service and should not call $requestStack->getSession() in the constructor.

Instead, call it when you need the session

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function onFoo()
    {
        $session = $this->requestStack->getSession();
        // ....
    }

@thewalkingcoder
Copy link
Author

Thanks you solve my problem ;-)

@thewalkingcoder
Copy link
Author

Work's fine but Functionnal test failed with same message "There is currently no session available."

Before my functionnal test i call service (who use requestStack) to force/simulate saving context with

 static::getContainer()->get(myservice)

my test/framework.yaml

framework:
    test: true
    session:
        storage_id: session.storage.mock_file

i must change logic ?

@jderusse
Copy link
Member

You need to set a session in you request when unint testing your service, or running you test in a request/response context. maybe similar to #41590

@thewalkingcoder
Copy link
Author

ok i explain the application logic.

After authenticated, user must select a workspace (the listener check if specific session exist, and redirect to select form is not, after select, session is created and user redirect to homepage). Work's fine.

When create functionnal test , i authenticate user with client->loginUser and util now, pre-save session workspace to test other functionality.
Work's fine when call my service and use SessionInterface

$workspaceSelectedRepository = static::getContainer()->get(WorkspaceSelectedRepository::class);
$workspaceSelectedRepository->save($workspace)
declare(strict_types=1);

namespace App\Account\Infrastructure\Persistence;

use App\Account\Domain\Interfaces\WorkspaceSelectedRepositoryInterface;
use App\Account\Domain\WorkspaceSelected;
use Symfony\Component\HttpFoundation\RequestStack;

class WorkspaceSelectedRepository implements WorkspaceSelectedRepositoryInterface
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    public const WORKSPACE_SELECTED = 'ssWorkspaceSelected';

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function getSession()
    {
        return $this->requestStack->getSession();
    }

    public function has(): bool
    {
        return $this->getSession()->has(self::WORKSPACE_SELECTED);
    }

    public function save(WorkspaceSelected $workspaceSelected): void
    {
        $this->getSession()->set(self::WORKSPACE_SELECTED, $workspaceSelected);
    }

    public function get(): ?WorkspaceSelected
    {
        return $this->getSession()->get(self::WORKSPACE_SELECTED);
    }

    public function delete(): void
    {
        $this->getSession()->remove(self::WORKSPACE_SELECTED);
    }
}

I try like $this->client->loginUser to create directly session

$session = static::getContainer()->get('test.service_container')->get('session.factory')->createSession();
$session->set(WorkspaceSelectedRepository ::WORKSPACE_SELECTED, $workpace);
$session->save();

When try that i don't have SessionNotFound but when i read $client->response i have the form listener who ask to select workspace (specific session is like empty).

What's wrong ? i must had session to the client after ?

@thewalkingcoder
Copy link
Author

i try with symfony/framework-bundle 5.3.x-dev 5548342 same result

@thewalkingcoder
Copy link
Author

Instead of create session before request, i try with your recommandations to set with client request/response context and works fine .

$crawler = $this->client->request('GET', self::HOST.'workspace-select');
$validateButton = $crawler->selectButton('Valider');
$form = $validateButton->form([], 'POST');
$form['account_workspace[workspace]']->disableValidation()->select($idworkspace);
$this->client->submit($form);

Thanks @jderusse

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

No branches or pull requests

2 participants