Symfony 2 SessionCsrfProvider & PHPUnit testing -
edit
actually answered myself, simple:
$session = $this->getmock( '\symfony\component\httpfoundation\session\session', array(), array(), '', false // don't call constructor ); $container->register('session', $session); mocking session object within phpunit bootstrap file.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
it seems me impossible test properly.
i using symfony 2 standalone components - not using frameworkbundle etc etc. have controller, in turn creates form, (attempts to) add csrf extension form builder.
however, passing in session object gives me:
runtimeexception: failed start session because headers have been sent. some code produces this:
$session = $this->container->get('session') // instance of symfony\component\httpfoundation\session\session $form_obj = new myform(); // extends abstracttype $secret = $form_obj->getname(); // random $form_obj->addextension(new csrfextension(new sessioncsrfprovider($session, $secret))); ok, session can't start because phpunit sends output first. putting session_start();
at top of bootstrap file not help, or work.
runtimeexception: failed start session because session has been started and in case, that's fudge fix wouldn't start session in 2 different places anyway.
so next, tried register mock session object per these instructions: http://symfony.com/doc/master/components/http_foundation/session_testing.html
but, sadly sessioncsrfprovider interface requires:
argument 1 passed symfony\component\form\extension\csrf\csrfprovider\sessioncsrfprovider::__construct() must instance of symfony\component\httpfoundation\session\session, instance of symfony\component\httpfoundation\session\storage\mockfilesessionstorage given so ... cannot use sessioncsrfprovider within test code, unless provide session phpunit mock ... require me alter design of code inject session object constructor or parameter method calls addextension() method. currently, above piece of code adds extension, wrapped within function itself, mean making required pass in session object manually, dont want introduce.
any ideas?
i know little late better way use symfonys mockarraysessionstorage.
$session = new session(new mockarraysessionstorage()); $csrfprovider = new sessioncsrfprovider($session, $secret);
Comments
Post a Comment