You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.8 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-25
  8. *
  9. * Unit tests for Session class
  10. * @TODO: Session::destroy() - check if session was started
  11. * @TODO: Session::destroy() - uncheck started flag after destroy
  12. */
  13. require_once dirname(__FILE__) . '/../../session/Session.php';
  14. class SessionTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $started = null;
  17. protected function setUp()
  18. {
  19. $class = new ReflectionClass('Session');
  20. $this->started = $class->getProperty('started');
  21. $this->started->setAccessible(true);
  22. Session::start();
  23. $sid = Session::getId();
  24. if (!empty($sid)) {
  25. Session::destroy();
  26. }
  27. $this->started->setValue(null, false);
  28. }
  29. public function testStart()
  30. {
  31. Session::start();
  32. $this->assertAttributeEquals(true, 'started', 'Session');
  33. }
  34. public function testSetGet()
  35. {
  36. Session::set('one', 1);
  37. Session::set('two', 'three');
  38. Session::set(array('first' => '1st', 'second' => '2nd'));
  39. $this->assertEquals('1st', Session::get('first'));
  40. $this->assertEquals('three', Session::get('two'));
  41. $this->assertNotEquals('three', Session::get('thr'));
  42. }
  43. public function testNullKey()
  44. {
  45. $this->assertNull(Session::get());
  46. Session::start();
  47. $this->assertEmpty(Session::get());
  48. }
  49. public function testReturnDefaultValue()
  50. {
  51. Session::start();
  52. $this->assertEquals(1, Session::get('key', 1));
  53. }
  54. public function testDestroyedGet()
  55. {
  56. $this->assertFalse($this->started->getValue());
  57. $_COOKIE[session_name()] = session_name();
  58. $this->assertEquals(1, Session::get('key', 1));
  59. }
  60. public function testDel()
  61. {
  62. Session::set('one', 1);
  63. Session::set('two', 'three');
  64. $this->assertEquals('three', Session::get('two'));
  65. Session::del('two');
  66. $this->assertNull(Session::get('two'));
  67. }
  68. public function testDestroyedDel()
  69. {
  70. Session::del('two');
  71. $this->assertNull(Session::get('two'));
  72. $this->assertFalse($this->started->getValue());
  73. $_COOKIE[session_name()] = session_name();
  74. Session::del('two');
  75. $this->assertNull(Session::get('two'));
  76. }
  77. public function testRegenerateId()
  78. {
  79. $this->assertEmpty(session_id());
  80. Session::start();
  81. $ssid = Session::getId();
  82. $this->assertNotEmpty($ssid);
  83. Session::regenerateId();
  84. $new_ssid = Session::getId();
  85. $this->assertNotEmpty($new_ssid);
  86. $this->assertNotEquals($new_ssid, $ssid);
  87. }
  88. public function testRememberUntil()
  89. {
  90. Session::start();
  91. $ssid = Session::getId();
  92. $params = session_get_cookie_params();
  93. Session::rememberUntil(400);
  94. $new_ssid = Session::getId();
  95. $new_params = session_get_cookie_params();
  96. $this->assertNotEquals($ssid, $new_ssid);
  97. $this->assertNotEquals($params, $new_params);
  98. $this->assertEquals(400, $new_params['lifetime']);
  99. Session::rememberUntil();
  100. $new_params = session_get_cookie_params();
  101. $this->assertEquals(0, $new_params['lifetime']);
  102. }
  103. public function testForget()
  104. {
  105. Session::start();
  106. $ssid = Session::getId();
  107. Session::forget();
  108. $new_ssid = Session::getId();
  109. $new_params = session_get_cookie_params();
  110. $this->assertNotEquals($ssid, $new_ssid);
  111. $this->assertEquals(0, $new_params['lifetime']);
  112. }
  113. public function testRemember()
  114. {
  115. Session::start();
  116. $ssid = Session::getId();
  117. Session::remember();
  118. $new_params = session_get_cookie_params();
  119. $this->assertEquals(1209600, $new_params['lifetime']);
  120. Session::remember(-30);
  121. $new_params = session_get_cookie_params();
  122. $this->assertEquals(1209600, $new_params['lifetime']);
  123. Session::remember(530);
  124. $new_params = session_get_cookie_params();
  125. $this->assertEquals(530, $new_params['lifetime']);
  126. }
  127. public function testExpireSessionCookie()
  128. {
  129. Session::start();
  130. $params = session_get_cookie_params();
  131. $_COOKIE[session_name()] = true;
  132. Session::expireSessionCookie();
  133. $this->assertNotNull($_COOKIE);
  134. }
  135. public function testSetSessionHandler()
  136. {
  137. Session::setSessionHandler('Handler');
  138. $this->assertTrue(TRUE);
  139. }
  140. }
  141. class Handler
  142. {
  143. public static function open()
  144. {
  145. }
  146. public static function close()
  147. {
  148. }
  149. public static function read()
  150. {
  151. }
  152. public static function write()
  153. {
  154. }
  155. public static function destroy()
  156. {
  157. }
  158. public static function gc()
  159. {
  160. }
  161. }