cookieTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Cookie测试
  13. * @author Haotong Lin <lofanmi@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use ReflectionClass;
  17. use think\Cookie;
  18. class cookieTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $ref;
  21. protected $default = [
  22. // cookie 名称前缀
  23. 'prefix' => '',
  24. // cookie 保存时间
  25. 'expire' => 0,
  26. // cookie 保存路径
  27. 'path' => '/',
  28. // cookie 有效域名
  29. 'domain' => '',
  30. // cookie 启用安全传输
  31. 'secure' => false,
  32. // httponly设置
  33. 'httponly' => '',
  34. // 是否使用 setcookie
  35. 'setcookie' => false,
  36. ];
  37. protected function setUp()
  38. {
  39. $reflectedClass = new ReflectionClass('\think\Cookie');
  40. $reflectedPropertyConfig = $reflectedClass->getProperty('config');
  41. $reflectedPropertyConfig->setAccessible(true);
  42. $reflectedPropertyConfig->setValue($this->default);
  43. $this->ref = $reflectedPropertyConfig;
  44. }
  45. public function testInit()
  46. {
  47. $config = [
  48. // cookie 名称前缀
  49. 'prefix' => 'think_',
  50. // cookie 保存时间
  51. 'expire' => 0,
  52. // cookie 保存路径
  53. 'path' => '/path/to/test/',
  54. // cookie 有效域名
  55. 'domain' => '.thinkphp.cn',
  56. // cookie 启用安全传输
  57. 'secure' => true,
  58. // httponly设置
  59. 'httponly' => '1',
  60. ];
  61. Cookie::init($config);
  62. $this->assertEquals(
  63. array_merge($this->default, array_change_key_case($config)),
  64. $this->ref->getValue()
  65. );
  66. }
  67. public function testPrefix()
  68. {
  69. $this->assertEquals($this->default['prefix'], Cookie::prefix());
  70. $prefix = '_test_';
  71. $this->assertNotEquals($prefix, Cookie::prefix());
  72. Cookie::prefix($prefix);
  73. $config = $this->ref->getValue();
  74. $this->assertEquals($prefix, $config['prefix']);
  75. }
  76. public function testSet()
  77. {
  78. $value = 'value';
  79. $name = 'name1';
  80. Cookie::set($name, $value, 10);
  81. $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
  82. $name = 'name2';
  83. Cookie::set($name, $value, null);
  84. $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
  85. $name = 'name3';
  86. Cookie::set($name, $value, 'expire=100&prefix=pre_');
  87. $this->assertEquals($value, $_COOKIE['pre_' . $name]);
  88. $name = 'name4';
  89. $value = ['_test_中文_'];
  90. Cookie::set($name, $value);
  91. $this->assertEquals('think:' . json_encode([urlencode('_test_中文_')]), $_COOKIE[$name]);
  92. }
  93. public function testGet()
  94. {
  95. $_COOKIE = [
  96. 'a' => 'b',
  97. 'pre_abc' => 'c',
  98. 'd' => 'think:' . json_encode([urlencode('_test_中文_')]),
  99. ];
  100. $this->assertEquals('b', Cookie::get('a'));
  101. $this->assertEquals(null, Cookie::get('does_not_exist'));
  102. $this->assertEquals('c', Cookie::get('abc', 'pre_'));
  103. $this->assertEquals(['_test_中文_'], Cookie::get('d'));
  104. }
  105. public function testDelete()
  106. {
  107. $_COOKIE = [
  108. 'a' => 'b',
  109. 'pre_abc' => 'c',
  110. ];
  111. $this->assertEquals('b', Cookie::get('a'));
  112. Cookie::delete('a');
  113. $this->assertEquals(null, Cookie::get('a'));
  114. $this->assertEquals('c', Cookie::get('abc', 'pre_'));
  115. Cookie::delete('abc', 'pre_');
  116. $this->assertEquals(null, Cookie::get('abc', 'pre_'));
  117. }
  118. public function testClear()
  119. {
  120. $_COOKIE = [];
  121. $this->assertEquals(null, Cookie::clear());
  122. $_COOKIE = [
  123. 'a' => 'b',
  124. 'pre_abc' => 'c',
  125. ];
  126. Cookie::clear('pre_');
  127. $this->assertEquals(['a' => 'b'], $_COOKIE);
  128. }
  129. }