Wednesday, June 3, 2009

symfony, test and sf_user attributes

It is surprisingly hard to change the attributes of the user session in a functionnal test. Suppose you want to do something like
$sf_user->setAttribute('myownkey', 'myownval');

But you are in a funcitonnal test, here is how i did it
$storage = new sfSessionTestStorage(array(
'session_path' => sfConfig::get('sf_test_cache_dir')."/sessions"
));
$storage->write('symfony/user/sfUser/attributes', array(
'symfony/user/sfUser/attributes' => array(
'myownkey' =>'myownval'
)
));
$storage->shutdown();

1 comments:

marduc said...

After setting an attribute in this way, are you able to check it using, e.g.,

$browser = new sfTestFunctional(new sfBrowser());
$browser->
get('/')->
with('user')->begin()->
isAttribute('myownkey', 'myownval')->
end();

or is there another way? I am trying to use this for setting a captcha value in the user session before submitting a form with the same captcha value from my functional test, but I can't seem to get this working..

Share/Save/Bookmark