PHPUnit and Sessions

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
kodra22
Forum Newbie
Posts: 1
Joined: Tue Oct 31, 2006 9:20 am

PHPUnit and Sessions

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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'));
   }
}
Post Reply