CookieTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace think\tests;
  3. use DateTime;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use PHPUnit\Framework\TestCase;
  7. use think\Config;
  8. use think\Cookie;
  9. use think\Request;
  10. class CookieTest extends TestCase
  11. {
  12. /** @var Cookie */
  13. protected $cookie;
  14. /** @var Request|MockInterface */
  15. protected $request;
  16. /** @var Config|MockInterface */
  17. protected $config;
  18. protected function setUp(): void
  19. {
  20. $this->request = m::mock(Request::class);
  21. $this->config = m::mock(Config::class);
  22. $this->cookie = new Cookie($this->request, [
  23. 'expire' => 3600,
  24. 'path' => '/',
  25. 'domain' => 'test.com',
  26. 'secure' => false,
  27. 'httponly' => true,
  28. 'samesite' => 'lax'
  29. ]);
  30. }
  31. protected function tearDown(): void
  32. {
  33. m::close();
  34. }
  35. public function testMakeMethod()
  36. {
  37. $this->config->shouldReceive('get')
  38. ->with('cookie')
  39. ->andReturn(['expire' => 7200]);
  40. $cookie = Cookie::__make($this->request, $this->config);
  41. $this->assertInstanceOf(Cookie::class, $cookie);
  42. }
  43. public function testGet()
  44. {
  45. $this->request->shouldReceive('cookie')
  46. ->with('test_cookie', 'default')
  47. ->andReturn('cookie_value');
  48. $result = $this->cookie->get('test_cookie', 'default');
  49. $this->assertEquals('cookie_value', $result);
  50. }
  51. public function testGetAll()
  52. {
  53. $this->request->shouldReceive('cookie')
  54. ->with('', null)
  55. ->andReturn(['cookie1' => 'value1', 'cookie2' => 'value2']);
  56. $result = $this->cookie->get();
  57. $this->assertEquals(['cookie1' => 'value1', 'cookie2' => 'value2'], $result);
  58. }
  59. public function testHas()
  60. {
  61. $this->request->shouldReceive('has')
  62. ->with('test_cookie', 'cookie')
  63. ->andReturn(true);
  64. $result = $this->cookie->has('test_cookie');
  65. $this->assertTrue($result);
  66. }
  67. public function testHasReturnsFalse()
  68. {
  69. $this->request->shouldReceive('has')
  70. ->with('nonexistent_cookie', 'cookie')
  71. ->andReturn(false);
  72. $result = $this->cookie->has('nonexistent_cookie');
  73. $this->assertFalse($result);
  74. }
  75. public function testSetBasic()
  76. {
  77. $this->request->shouldReceive('setCookie')
  78. ->with('test_cookie', 'test_value');
  79. $this->cookie->set('test_cookie', 'test_value');
  80. $cookies = $this->cookie->getCookie();
  81. $this->assertArrayHasKey('test_cookie', $cookies);
  82. $this->assertEquals('test_value', $cookies['test_cookie'][0]);
  83. }
  84. public function testSetWithNumericExpire()
  85. {
  86. $this->request->shouldReceive('setCookie')
  87. ->with('test_cookie', 'test_value');
  88. $this->cookie->set('test_cookie', 'test_value', 7200);
  89. $cookies = $this->cookie->getCookie();
  90. $this->assertArrayHasKey('test_cookie', $cookies);
  91. $this->assertGreaterThan(time(), $cookies['test_cookie'][1]);
  92. }
  93. public function testSetWithDateTimeExpire()
  94. {
  95. $expire = new DateTime('+1 hour');
  96. $this->request->shouldReceive('setCookie')
  97. ->with('test_cookie', 'test_value');
  98. $this->cookie->set('test_cookie', 'test_value', $expire);
  99. $cookies = $this->cookie->getCookie();
  100. $this->assertEquals($expire->getTimestamp(), $cookies['test_cookie'][1]);
  101. }
  102. public function testSetWithArrayOptions()
  103. {
  104. $options = [
  105. 'expire' => 1800,
  106. 'path' => '/test',
  107. 'domain' => 'example.com',
  108. 'secure' => true,
  109. 'httponly' => false,
  110. 'samesite' => 'strict'
  111. ];
  112. $this->request->shouldReceive('setCookie')
  113. ->with('test_cookie', 'test_value');
  114. $this->cookie->set('test_cookie', 'test_value', $options);
  115. $cookies = $this->cookie->getCookie();
  116. $cookieData = $cookies['test_cookie'];
  117. $this->assertEquals('test_value', $cookieData[0]);
  118. $this->assertGreaterThan(time(), $cookieData[1]);
  119. $this->assertEquals('/test', $cookieData[2]['path']);
  120. $this->assertEquals('example.com', $cookieData[2]['domain']);
  121. $this->assertTrue($cookieData[2]['secure']);
  122. $this->assertFalse($cookieData[2]['httponly']);
  123. $this->assertEquals('strict', $cookieData[2]['samesite']);
  124. }
  125. public function testSetWithDateTimeInOptions()
  126. {
  127. $expire = new DateTime('+2 hours');
  128. $options = ['expire' => $expire];
  129. $this->request->shouldReceive('setCookie')
  130. ->with('test_cookie', 'test_value');
  131. $this->cookie->set('test_cookie', 'test_value', $options);
  132. $cookies = $this->cookie->getCookie();
  133. $this->assertEquals($expire->getTimestamp(), $cookies['test_cookie'][1]);
  134. }
  135. public function testForever()
  136. {
  137. $this->request->shouldReceive('setCookie')
  138. ->with('forever_cookie', 'forever_value');
  139. $this->cookie->forever('forever_cookie', 'forever_value');
  140. $cookies = $this->cookie->getCookie();
  141. $this->assertArrayHasKey('forever_cookie', $cookies);
  142. $this->assertEquals('forever_value', $cookies['forever_cookie'][0]);
  143. $this->assertGreaterThan(time() + 315360000 - 10, $cookies['forever_cookie'][1]);
  144. }
  145. public function testForeverWithOptions()
  146. {
  147. $options = ['path' => '/forever', 'secure' => true];
  148. $this->request->shouldReceive('setCookie')
  149. ->with('forever_cookie', 'forever_value');
  150. $this->cookie->forever('forever_cookie', 'forever_value', $options);
  151. $cookies = $this->cookie->getCookie();
  152. $cookieData = $cookies['forever_cookie'];
  153. $this->assertEquals('/forever', $cookieData[2]['path']);
  154. $this->assertTrue($cookieData[2]['secure']);
  155. $this->assertGreaterThan(time() + 315360000 - 10, $cookieData[1]);
  156. }
  157. public function testForeverWithNullOptions()
  158. {
  159. $this->request->shouldReceive('setCookie')
  160. ->with('forever_cookie', 'forever_value');
  161. $this->cookie->forever('forever_cookie', 'forever_value', null);
  162. $cookies = $this->cookie->getCookie();
  163. $this->assertArrayHasKey('forever_cookie', $cookies);
  164. }
  165. public function testForeverWithNumericOptions()
  166. {
  167. $this->request->shouldReceive('setCookie')
  168. ->with('forever_cookie', 'forever_value');
  169. $this->cookie->forever('forever_cookie', 'forever_value', 123);
  170. $cookies = $this->cookie->getCookie();
  171. $this->assertArrayHasKey('forever_cookie', $cookies);
  172. }
  173. public function testDelete()
  174. {
  175. $this->request->shouldReceive('setCookie')
  176. ->with('test_cookie', null);
  177. $this->cookie->delete('test_cookie');
  178. $cookies = $this->cookie->getCookie();
  179. $this->assertArrayHasKey('test_cookie', $cookies);
  180. $this->assertEquals('', $cookies['test_cookie'][0]);
  181. $this->assertLessThan(time(), $cookies['test_cookie'][1]);
  182. }
  183. public function testDeleteWithOptions()
  184. {
  185. $options = ['path' => '/test', 'domain' => 'example.com'];
  186. $this->request->shouldReceive('setCookie')
  187. ->with('test_cookie', null);
  188. $this->cookie->delete('test_cookie', $options);
  189. $cookies = $this->cookie->getCookie();
  190. $cookieData = $cookies['test_cookie'];
  191. $this->assertEquals('', $cookieData[0]);
  192. $this->assertEquals('/test', $cookieData[2]['path']);
  193. $this->assertEquals('example.com', $cookieData[2]['domain']);
  194. }
  195. public function testGetCookie()
  196. {
  197. $this->request->shouldReceive('setCookie')
  198. ->with('cookie1', 'value1');
  199. $this->request->shouldReceive('setCookie')
  200. ->with('cookie2', 'value2');
  201. $this->cookie->set('cookie1', 'value1');
  202. $this->cookie->set('cookie2', 'value2');
  203. $cookies = $this->cookie->getCookie();
  204. $this->assertArrayHasKey('cookie1', $cookies);
  205. $this->assertArrayHasKey('cookie2', $cookies);
  206. $this->assertEquals('value1', $cookies['cookie1'][0]);
  207. $this->assertEquals('value2', $cookies['cookie2'][0]);
  208. }
  209. public function testSave()
  210. {
  211. // Mock the protected saveCookie method by extending the class
  212. $cookie = new class($this->request) extends Cookie {
  213. public $savedCookies = [];
  214. protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly, string $samesite): void
  215. {
  216. $this->savedCookies[] = [
  217. 'name' => $name,
  218. 'value' => $value,
  219. 'expire' => $expire,
  220. 'path' => $path,
  221. 'domain' => $domain,
  222. 'secure' => $secure,
  223. 'httponly' => $httponly,
  224. 'samesite' => $samesite,
  225. ];
  226. }
  227. };
  228. $this->request->shouldReceive('setCookie')
  229. ->with('test_cookie', 'test_value');
  230. $cookie->set('test_cookie', 'test_value');
  231. $cookie->save();
  232. $this->assertCount(1, $cookie->savedCookies);
  233. $this->assertEquals('test_cookie', $cookie->savedCookies[0]['name']);
  234. $this->assertEquals('test_value', $cookie->savedCookies[0]['value']);
  235. }
  236. public function testCaseInsensitiveConfig()
  237. {
  238. $cookie = new Cookie($this->request, [
  239. 'EXPIRE' => 1800,
  240. 'PATH' => '/test',
  241. 'DOMAIN' => 'TEST.COM'
  242. ]);
  243. $this->request->shouldReceive('setCookie')
  244. ->with('test_cookie', 'test_value');
  245. $cookie->set('test_cookie', 'test_value');
  246. $cookies = $cookie->getCookie();
  247. $cookieData = $cookies['test_cookie'];
  248. $this->assertEquals('/test', $cookieData[2]['path']);
  249. $this->assertEquals('TEST.COM', $cookieData[2]['domain']);
  250. }
  251. public function testDefaultConfig()
  252. {
  253. $cookie = new Cookie($this->request);
  254. $this->request->shouldReceive('setCookie')
  255. ->with('test_cookie', 'test_value');
  256. $cookie->set('test_cookie', 'test_value');
  257. $cookies = $cookie->getCookie();
  258. $cookieData = $cookies['test_cookie'];
  259. $this->assertEquals('/', $cookieData[2]['path']);
  260. $this->assertEquals('', $cookieData[2]['domain']);
  261. $this->assertFalse($cookieData[2]['secure']);
  262. $this->assertFalse($cookieData[2]['httponly']);
  263. }
  264. }