Page 1 of 1

PHPUnit and Sessions

Posted: Tue Oct 31, 2006 9:23 am
by kodra22
I'm having a bit of trouble testing my code for a system that requires authentication. Is there anyway to set session variables using PHPUnit 2.3.6?

Posted: Tue Oct 31, 2006 9:42 am
by Weirdan
what troubles do you have? have you tried something like this:

Code: Select all

class TestOfSomething extends UnitTestCase {
   function testSomeMethodRequiringAuthenticationWhenLoggedOn() {
      $_SESSION = array();
      $_SESSION['user_id'] = 123;
      $this->assertTrue(SomeFunctionRequiringAuthentication('asd', 'zxc'));
   }
   function testSomeMethodRequiringAuthenticationWhenNotLoggedOn() {
      $_SESSION = array();
      unset($_SESSION['user_id']);
      $this->assertFalse(SomeFunctionRequiringAuthentication('asd', 'zxc'));
   }
}