PHPUnit error when using Silex SessionServiceProvider

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?

+3
source share
2 answers

EDIT after comment below

Well, I have the same problem, and after an hour of browsing the web I managed to pass the tests.

In Silex 2.0-dev, calling $app['session.test'] = true from the WebTestCase class does not work at all, this should happen in bootstrap.

There are many ways to achieve this, here are two of them:

1 / s phpunit.xml.dist

 <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="./app.php" > <php> <env name="TEST" value="true" /> //-> This is the trick </php> <testsuites> <testsuite name="Your app Test Suite"> <directory>./tests/</directory> </testsuite> </testsuites> </phpunit> 

then in bootstrap

 $app = new \Silex\Application(); ... $app->register(new \Silex\Provider\SessionServiceProvider(), [ 'session.test' => false !== getenv('TEST') ]); ... return $app; 


2 / Extending Silex\Application so you can pass the environment to the constructor

 namespace Your\Namespace; class YourApp extends \Silex\Application { public function __construct($env, array $params = array()) { $this['env'] = $env; parent::__construct($params); } } 

then in your bootstrap

 $env = // Your logic ... $app = new \Your\Namespace\YourApp($env); ... $app->register(new \Silex\Provider\SessionServiceProvider(), [ 'session.test' => 'test' === $app['env'], ]); ... return $app; 

Hope this helps, cheers!

+5
source

Ok, I found the answer. This seems to be a bug in Silex.

The problem occurred when I register a branch extension before registering a standard FormServiceProvider. This was not caused by anything inside the branch extension, the error still occurs if I split the entire extension class into nothing but empty methods.

So, the registration of twig extensions in the Silex application object should always be done AFTER the suppliers are registered, at least after the FormServiceProvider (until the error is resolved).

0
source

Source: https://habr.com/ru/post/1436853/


All Articles