I am trying to create a unit test for my Silex application. The unit test class looks something like this:
class PageTest extends WebTestCase { public function createApplication() { $app = require __DIR__ . '/../../app/app.php'; $app['debug'] = true; $app['session.storage'] = $app->share(function() { return new MockArraySessionStorage(); }); $app['session.test'] = true; unset($app['exception_handler']); return $app; } public function testIndex() { $client = $this->createClient(); $client->request('GET', '/'); $this->assertTrue($client->getResponse()->isOk()); } }
and the silex route that he is trying to request looks something like this:
$app->get('/', function() use($app) { $user = $app['session']->get('loginUser'); return $app['twig']->render('views/index.twig', array( 'user' => $user, )); });
This raises a RuntimeException: the session could not start because the headers have already been sent. in \ Symfony \ Component \ HttpFoundation \ Session \ Storage \ NativeSessionStorage.php: 142 with backtrace, which includes a line from the route with $ app ['session'] → get.
It seems that the result that occurred before trying to start the session in NativeSessionStorage is actually PHPUnit exit information, as this is the only output I get before the error message:
PHPUnit 3.7.8 by Sebastian Bergmann. Configuration read from (PATH)\phpunit.xml E.......
I am a little confused because this error is output from phpunit output on output before the actual testing method is executed. I do not run any other testing methods, so this should be due to this error.
How can I get PHPUnit to work on silex routes using session variables?
source share