Skip to content

Instantly share code, notes, and snippets.

@sousk
Created November 8, 2009 03:01
Show Gist options
  • Save sousk/229067 to your computer and use it in GitHub Desktop.
Save sousk/229067 to your computer and use it in GitHub Desktop.
php doctest helper: getting an authenticated user instance
/**
* #test test_user
* <code>
* #diag('expect error');
* try {
* $u = test_user('peko');
* #fail('should raise error');
* }
* catch(Exception $e) {
* #ok($e,$e->getMessage());
* }
* #diag("exipect ok");
* #ok($u = test_user(), "ok");
* #isa_ok($u, 'myUser', 'myUser');
* #is($u->getUserId(), 1);
* #is($u->getUsername(), 'john');
* </code>
*/
function test_user($username='john', $pwd='hoge')
{
$error = ""; $con = sfContext::getInstance();
$con->getRequest()->setParameter('password', $pwd);
$validator = new sfGuardUserValidator;
$validator->initialize($con);
$validator->execute($username, $error);
$u = $con->getUser();
if (strlen($error) > 0) {
throw new sfException('failed to authenticate, test user has missed: '. $error);
}
return $u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment