Skip to content

Instantly share code, notes, and snippets.

@clemherreman
Created June 21, 2011 15:12
Show Gist options
  • Save clemherreman/1038062 to your computer and use it in GitHub Desktop.
Save clemherreman/1038062 to your computer and use it in GitHub Desktop.
Login during functional test on symfony1.4
<?php
class CapWebndfTestFunctionnal extends sfTestFunctional
{
/**
* Login as a user
* @param string $username User's username
*/
public function signInAs($username)
{
$user = Doctrine_Core::getTable('sfGuardUser')->findOneByUsername($username);
//force the context to be created
$this->browser->getContext(true)->getUser()->signIn($user);
$this->info(sprintf('Signin user using username "%s"', $username));
if ($user->getIsSuperAdmin())
{
$this->info('Beware, you are logged as a superadmin!');
}
return $this;
}
}
<?php
$browser = new CapWebndfTestFunctionnal(new sfBrowser());
$browser->
signInAs('superadmin')->
with('user')->begin()->
isAuthenticated()->
end()->
get('/document')-> //a secured page
with('request')->begin()->
isParameter('module', 'document')->
isParameter('action', 'index')->
end()->
with('response')->begin()->
isStatusCode(200)-> //as superadmin is a sfGuard superadmin, this should be 200;
end()->
/* Outputs :
clem@ubuntu:/var/www/cap_webndf/trunk$ php symfony test:functional backend documentActions
> Signin user using username "superadmin"
> Beware, you are logged as a superadmin!
ok 1 - user is authenticated
# get /document
ok 2 - request parameter module is document
ok 3 - request parameter action is index
not ok 4 - status code is 200
# Failed test (./lib/vendor/symfony/lib/test/sfTesterResponse.class.php at line 412)
# got: 401
# expected: 200
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment