Joeri Timmermans
Joeri Timmermans
PHP/Symfony Developer with
a passion for food and drinks
Developer @ Intracto

Testing with Sessions (Symfony2)

Tuesday February 26, 2013 - Permalink - Tags: testing, phpunit, symfony2, session

Today I encountered a problem with our phpunit tests for our symfony2 project. We were having issues with the ‘ErrorException: Notice: A session had already been started – ignoring session_start()’.

So after some googleing I found out that we shouldn’t create our own mock like we did here

$session = $this
->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
->setMethods(array('getId','get'))
->getMock();
$session
->expects($this->any())
->method('getId')
->will($this->returnValue(md5(date('YmdHihs'))));

But symfony now has their own mock to create a session.

use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;

$session = new Session(new MockArraySessionStorage());

Another problem solved in my day as a junior developer

Source: http://symfony.com/doc/current/components/http_foundation/session_testing.html

comments powered by Disqus