Mar 14, 2011
How can i override user attribute in my functional test?
Some times your code depend on a user attribute, so you want set the user attribute in your functional test.
The Problem is that the doCall method in the sfBrowser class rebuild the context and in the end calls theshutdown method from user and storage object to write the session data.
So you must create your own Browser class and override the doCall function and simple move the shutdown calls to the beginning.
Browser Class:
class myBrowser extends sfBrowser { /** * Calls a request to a uri. */ protected function doCall() { // manually shutdown user to save current session data if (isset($this->context) AND $this->context->getUser()) { $this->context->getUser()->shutdown(); $this->context->getStorage()->shutdown(); } // recycle our context object $this->context = $this->getContext(true); sfConfig::set('sf_test', true); // we register a fake rendering filter sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null)); $this->resetCurrentException(); // dispatch our request ob_start(); $this->context->getController()->dispatch(); $retval = ob_get_clean(); // append retval to the response content $this->context->getResponse()->setContent($retval); } }
Test code:
include dirname(__FILE__).'/../../bootstrap/functional.php'; $browser = new sfTestFunctional(new myBrowser()); $browser-> get('/site-one'); // override the session data $browser->getUser()->setAttribute('key', $value); $browser-> get('/site-one');
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.











Vielen Dank, das hat mir den Tag gerettet, nachdem ich schon seit Stunden versucht habe herauszufinden, wie ich in einem funktionalen Test einen Nutzer per sfGuardSecurityUser::signin() einloggen kann, da meine Action einen solchen voraussetzt. Der Nutzer war zwar im Test eingeloggt, aber nicht in der Action. Jetzt funktioniert’s, dankeschön!
Thanks a lot, wouldn’t have come up with that solution by myself!