Mocking Symfony2's request and session in PHPUnit Mocking Symfony2's request and session in PHPUnit symfony symfony

Mocking Symfony2's request and session in PHPUnit


Not all code is worth unit testing. Usually this is an indicator that your code could be simplified. When you unit test code that is somewhat complex the tests can become a burden and normally it would be better to do an integration of edge-to-edge test in these cases. It's also not clear in your example how your class gets the RequestStack so I will assume that it has been injected in __construct.

With that said here's how you would test that code:

protected function setUp(){    $this->requestStack = $this->getMock('Fully-qualified RequestStack namespace');    $this->SUT = new MyClass($this->requestStack);}    /** @test */public function it_should_store_value_in_the_session(){    $value = 'test value';    $request = $this->getMock('Request');    $request->request = $this->getMock('ParameterBag');    $session = $this->getMock('Session');    $this->requestStack        ->expects($this->atLeastOnce())        ->method('getCurrentRequest')        ->will($this->returnValue());    $request->request        ->expects($this->atLeastOnce())        ->method('get')        ->with('something')        ->will($this->returnValue($value));    $request        ->expects($this->once())        ->method('getSession')        ->will($this->returnValue($session));    $session        ->expects($this->once())        ->method('set')        ->with('somevar', $value);    $this->SUT->doSomething();}

This should give you a starting point but beware having a wall-of mocks in your tests because very small changes to the implementation details can cause your tests to fail even though the behaviour is still correct and this is something you want to avoid as much as possible so the tests aren't expensive to maintain.

Edit: I thought some more about your question and realized that typically you can inject the Session as a dependency. If that's possible in your use case it would simplify the tests a lot.


You don't need to mock RequestStack, it's a super simple class. You can just create a fake request and push it to it. You can also mock the session.

// you can overwrite any value you want through the constructor if you need more control$fakeRequest = Request::create('/', 'GET');$fakeRequest->setSession(new Session(new MockArraySessionStorage()));$requestStack = new RequestStack();$requestStack->push($fakeRequest);// then pass the requestStack to your service under test.

But in terms of testing, having to mess around with the internals of a class is not a good sign. Maybe you can create a handler class to encapsulate the logic you need from the request stack so you can test more easily.


It's difficult to imagine a situation where you'd have to be dealing with GET/POST parameters inside a unit-tested class. Have the Controller deal with HTTP requests and sessions (that's pretty much what they're there for), and pass the variables down into the relevant classes to deal with the rest.

That being said, Kevin's response is a possible solution if you want to go down that route.