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.

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