1

I am in the process of writing some functional tests for a symfony 1.4.19 website, using Propel ORM. Some of my pages display differently, depending on whether a user is logged in (authenticated) or not.

I have been looking at the online documentation, to see how to implement:

  1. Tests that run depending on whether a user is logged in (authenticated) or not (as the case may be)
  2. How to login or logout a user as part of the functional testing.

However, I can't seem to locate anything that shows how to do this.

2
  • Why don't you just connect with a user, make your test, disconnect, continue, etc ... ? Commented Nov 9, 2012 at 14:56
  • @j0k: Thats what I want to do, its just that its not clear to me how to "connect with a user" in code (in a functional test). Can you show a snippet that shows how to do this? Commented Nov 9, 2012 at 15:20

1 Answer 1

2

To login:

$username = 'root';
$password = 'root';

$browser->
  post('/login', array('signin' => array('username' => $username, 'password' => $password)))->
  with('request')->begin()->
    isParameter('module', 'sfGuardAuth')->
    isParameter('action', 'signin')->
  end()->
  with('response')->begin()->
    isStatusCode(302)->
    isRedirected()->
  end()->
  followRedirect()
;

To logout:

$browser->
  get('/logout')->
  with('request')->begin()->
    isParameter('module', 'sfGuardAuth')->
    isParameter('action', 'signout')->
  end()->
  with('response')->begin()->
    isStatusCode(302)->
    isRedirected()->
  end()->
  followRedirect()
;

// this will reset session
$browser->restart();
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the code. Ok, thats very good. Now, all I need is the sfUser object, so that I can check whether the user is logged in or not. Thanks.
You won't have access to the sfUser object in functional test, since you are browsing your app like a browser. You need to check if there is a link like logout or you're connected (or what ever you define) in your template.
Thanks. I think I can work with that. If I come unstuck, I'll come in here and ask for help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.