1

Ive been looking around on how to test sessions.. iven't really found any thing helpful...

 $user_id = $_SESSION['user_id'];
     $login_string = $_SESSION['login_string'];
     $username = $_SESSION['username'];
     $ip_address = $_SERVER['REMOTE_ADDR']; // Get the IP address of the user. 
     $user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.

how should i test them?

Shall i test to destroy the sessions and then look if its destroyed? Or do any kind of assertEquals?

Could anyone please write me an example and explain, becaouse i learn best in that way:) or give me a website that explains...

Thanks for help.

1 Answer 1

1

how [sic!] should i [sic!] test them?

They have been tested for you, the testing is done in the PHP Q&A team, help is always welcome.

Shall i [sic!] test to destroy the sessions and then look if its destroyed? Or do any kind of assertEquals?

No. Totally not necessary and only a waste of time.

Could anyone please write me an example and explain, becaouse [sic!] i [sic!] learn best in that way:) [sic!] or give me a website that explains... [sic!]

This website here does. Look around.

Long answer:

Superglobals like $_SESSION are part of the PHP language. As such, you do not need to unit-test if a superglobal works, you can just assume it does work and fine.

Also sessions with the superglobal $_SESSION are a feature and part of the PHP language. As such, you don't need to unit-test if session works. You can just assume it does work and fine.

If there is an issue with those, it's a configuration issue and unit-tests can not (and should not) take care of that.

If you are asking because of the problem the global static state PHP has here introduces you in testing other units, you need to mock sessions. To get that done, you need to abstract sessions first so that you can mock them later. That normally starts with defining your own interface(s) to session functionality and then write one implementation that is using PHP's default feature implementation.

Sign up to request clarification or add additional context in comments.

Comments

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.