sessionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Session测试
  13. * @author 大漠 <zhylninc@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Session;
  17. class sessionTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. *
  21. * @var \think\Session
  22. */
  23. protected $object;
  24. /**
  25. * Sets up the fixture, for example, opens a network connection.
  26. * This method is called before a test is executed.
  27. */
  28. protected function setUp()
  29. {
  30. // $this->object = new Session ();
  31. // register_shutdown_function ( function () {
  32. // } ); // 此功能无法取消,需要回调函数配合。
  33. set_exception_handler(function () {});
  34. set_error_handler(function () {});
  35. }
  36. /**
  37. * Tears down the fixture, for example, closes a network connection.
  38. * This method is called after a test is executed.
  39. */
  40. protected function tearDown()
  41. {
  42. register_shutdown_function('think\Error::appShutdown');
  43. set_error_handler('think\Error::appError');
  44. set_exception_handler('think\Error::appException');
  45. }
  46. /**
  47. * @covers think\Session::prefix
  48. *
  49. * @todo Implement testPrefix().
  50. */
  51. public function testPrefix()
  52. {
  53. Session::prefix(null);
  54. Session::prefix('think_');
  55. $this->assertEquals('think_', Session::prefix());
  56. }
  57. /**
  58. * @covers think\Session::init
  59. *
  60. * @todo Implement testInit().
  61. */
  62. public function testInit()
  63. {
  64. Session::prefix(null);
  65. $config = [
  66. // cookie 名称前缀
  67. 'prefix' => 'think_',
  68. // cookie 保存时间
  69. 'expire' => 60,
  70. // cookie 保存路径
  71. 'path' => '/path/to/test/session/',
  72. // cookie 有效域名
  73. 'domain' => '.thinkphp.cn',
  74. 'var_session_id' => 'sessionidtest',
  75. 'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
  76. 'name' => 'session_name',
  77. 'use_trans_sid' => '1',
  78. 'use_cookies' => '1',
  79. 'cache_limiter' => '60',
  80. 'cache_expire' => '60',
  81. 'type' => '', // memcache
  82. 'namespace' => '\\think\\session\\driver\\', // ?
  83. 'auto_start' => '1',
  84. ];
  85. $_REQUEST[$config['var_session_id']] = $config['id'];
  86. Session::init($config);
  87. // 开始断言
  88. $this->assertEquals($config['prefix'], Session::prefix());
  89. $this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]);
  90. $this->assertEquals($config['name'], session_name());
  91. $this->assertEquals($config['path'], session_save_path());
  92. $this->assertEquals($config['use_cookies'], ini_get('session.use_cookies'));
  93. $this->assertEquals($config['domain'], ini_get('session.cookie_domain'));
  94. $this->assertEquals($config['expire'], ini_get('session.gc_maxlifetime'));
  95. $this->assertEquals($config['expire'], ini_get('session.cookie_lifetime'));
  96. $this->assertEquals($config['cache_limiter'], session_cache_limiter($config['cache_limiter']));
  97. $this->assertEquals($config['cache_expire'], session_cache_expire($config['cache_expire']));
  98. // 检测分支
  99. $_REQUEST[$config['var_session_id']] = null;
  100. session_write_close();
  101. session_destroy();
  102. Session::init($config);
  103. // 测试auto_start
  104. // PHP_SESSION_DISABLED
  105. // PHP_SESSION_NONE
  106. // PHP_SESSION_ACTIVE
  107. // session_status()
  108. if (strstr(PHP_VERSION, 'hhvm')) {
  109. $this->assertEquals('', ini_get('session.auto_start'));
  110. } else {
  111. $this->assertEquals(0, ini_get('session.auto_start'));
  112. }
  113. $this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
  114. Session::init($config);
  115. $this->assertEquals($config['id'], session_id());
  116. }
  117. /**
  118. * 单独重现异常
  119. * @expectedException \think\Exception
  120. */
  121. public function testException()
  122. {
  123. $config = [
  124. // cookie 名称前缀
  125. 'prefix' => 'think_',
  126. // cookie 保存时间
  127. 'expire' => 0,
  128. // cookie 保存路径
  129. 'path' => '/path/to/test/session/',
  130. // cookie 有效域名
  131. 'domain' => '.thinkphp.cn',
  132. 'var_session_id' => 'sessionidtest',
  133. 'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
  134. 'name' => 'session_name',
  135. 'use_trans_sid' => '1',
  136. 'use_cookies' => '1',
  137. 'cache_limiter' => '60',
  138. 'cache_expire' => '60',
  139. 'type' => '\\think\\session\\driver\\Memcache', //
  140. 'auto_start' => '1',
  141. ];
  142. // 测试session驱动是否存在
  143. // @expectedException 异常类名
  144. $this->setExpectedException('\think\exception\ClassNotFoundException', 'error session handler');
  145. Session::init($config);
  146. }
  147. /**
  148. * @covers think\Session::set
  149. *
  150. * @todo Implement testSet().
  151. */
  152. public function testSet()
  153. {
  154. Session::prefix(null);
  155. Session::set('sessionname', 'sessionvalue');
  156. $this->assertEquals('sessionvalue', $_SESSION['sessionname']);
  157. Session::set('sessionnamearr.subname', 'sessionvalue');
  158. $this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']);
  159. Session::set('sessionnameper', 'sessionvalue', 'think_');
  160. $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']);
  161. Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_');
  162. $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']);
  163. }
  164. /**
  165. * @covers think\Session::get
  166. *
  167. * @todo Implement testGet().
  168. */
  169. public function testGet()
  170. {
  171. Session::prefix(null);
  172. Session::set('sessionnameget', 'sessionvalue');
  173. $this->assertEquals(Session::get('sessionnameget'), $_SESSION['sessionnameget']);
  174. Session::set('sessionnamegetarr.subname', 'sessionvalue');
  175. $this->assertEquals(Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']);
  176. Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_');
  177. $this->assertEquals(Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']);
  178. Session::set('sessionnamegetper', 'sessionvalue', 'think_');
  179. $this->assertEquals(Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']);
  180. Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_');
  181. $this->assertEquals(Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']);
  182. }
  183. public function testPull()
  184. {
  185. Session::prefix(null);
  186. Session::set('sessionnamedel', 'sessionvalue');
  187. $this->assertEquals('sessionvalue', Session::pull('sessionnameget'));
  188. $this->assertNull(Session::get('sessionnameget'));
  189. }
  190. /**
  191. * @covers think\Session::delete
  192. *
  193. * @todo Implement testDelete().
  194. */
  195. public function testDelete()
  196. {
  197. Session::prefix(null);
  198. Session::set('sessionnamedel', 'sessionvalue');
  199. Session::delete('sessionnamedel');
  200. $this->assertEmpty($_SESSION['sessionnamedel']);
  201. Session::set('sessionnamedelarr.subname', 'sessionvalue');
  202. Session::delete('sessionnamedelarr.subname');
  203. $this->assertEmpty($_SESSION['sessionnamedelarr']['subname']);
  204. Session::set('sessionnamedelper', 'sessionvalue', 'think_');
  205. Session::delete('sessionnamedelper', 'think_');
  206. $this->assertEmpty($_SESSION['think_']['sessionnamedelper']);
  207. Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_');
  208. Session::delete('sessionnamedelperarr.subname', 'think_');
  209. $this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']);
  210. }
  211. /**
  212. * @covers think\Session::clear
  213. *
  214. * @todo Implement testClear().
  215. */
  216. public function testClear()
  217. {
  218. Session::prefix(null);
  219. Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
  220. Session::clear('think_');
  221. $this->assertNull($_SESSION['think_']);
  222. Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
  223. Session::clear();
  224. $this->assertEmpty($_SESSION);
  225. }
  226. /**
  227. * @covers think\Session::has
  228. *
  229. * @todo Implement testHas().
  230. */
  231. public function testHas()
  232. {
  233. Session::prefix(null);
  234. Session::set('sessionnamehas', 'sessionvalue');
  235. $this->assertTrue(Session::has('sessionnamehas'));
  236. Session::set('sessionnamehasarr.subname', 'sessionvalue');
  237. $this->assertTrue(Session::has('sessionnamehasarr.subname'));
  238. Session::set('sessionnamehasper', 'sessionvalue', 'think_');
  239. $this->assertTrue(Session::has('sessionnamehasper', 'think_'));
  240. Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_');
  241. $this->assertTrue(Session::has('sessionnamehasarrper.subname', 'think_'));
  242. }
  243. /**
  244. * @covers think\Session::pause
  245. *
  246. * @todo Implement testPause().
  247. */
  248. public function testPause()
  249. {
  250. Session::pause();
  251. }
  252. /**
  253. * @covers think\Session::start
  254. *
  255. * @todo Implement testStart().
  256. */
  257. public function testStart()
  258. {
  259. Session::start();
  260. }
  261. /**
  262. * @covers think\Session::destroy
  263. *
  264. * @todo Implement testDestroy().
  265. */
  266. public function testDestroy()
  267. {
  268. Session::set('sessionnamedestroy', 'sessionvalue');
  269. Session::destroy();
  270. $this->assertEmpty($_SESSION['sessionnamedestroy']);
  271. }
  272. }