vendredi 19 mars 2021

Symfony: How to set session when testing with PHPSpec

i'm currently trying to test my LocaleSubscriber (which handles most of the multi-language work of my application) with PHPSpec. So far I know, that I need to simulate a request.

My code currently looks like this:

public function it_rediredts_to_en_when_nothing_is_parsed(RequestEvent $event, Session $session)
{
    $request = $event->getRequest();
    $request->setSession($session);
    $request->shouldBeCalled()->willReturn(Request::create('/en/cleaning-rotas'));
    $session->get('language')->shouldBeCalled()->willReturn('en');
    $this->onKernelRequest($event);
}

Right now I'm getting this error: Call to undefined method Prophecy\Prophecy\MethodProphecy::setSession(). But as soon as I test this without any session stuff, I get the error message exc:BadMethodCallException("Session has not been set."), which make sense to me. But I dont have a clue how to set a session in this scenario, so that I can keep working with the request.

Until now, it's all about this part of code that I want to test.

public function onKernelRequest(RequestEvent $event)
{
    $request = $event->getRequest();

    //Specify Locale (set threw session/is null)
    $customLocale = $request->getSession()->get('language') ?: $this->defaultLocale;

Can someone give me any hints about this? I'm new to PHPSpec, just worked with e.g. PHPUnit until now.

Aucun commentaire:

Enregistrer un commentaire