zhaojs
2023-05-16 ea24ddd0b978cbd3b0a900711b49b8a9c2db4186
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
 
/**
 * Cookie测试
 * @author    Haotong Lin <lofanmi@gmail.com>
 */
 
namespace tests\thinkphp\library\think;
 
use ReflectionClass;
use think\Cookie;
 
class cookieTest extends \PHPUnit_Framework_TestCase
{
    protected $ref;
 
    protected $default = [
        // cookie 名称前缀
        'prefix'    => '',
        // cookie 保存时间
        'expire'    => 0,
        // cookie 保存路径
        'path'      => '/',
        // cookie 有效域名
        'domain'    => '',
        //  cookie 启用安全传输
        'secure'    => false,
        // httponly设置
        'httponly'  => '',
        // 是否使用 setcookie
        'setcookie' => false,
    ];
 
    protected function setUp()
    {
        $reflectedClass          = new ReflectionClass('\think\Cookie');
        $reflectedPropertyConfig = $reflectedClass->getProperty('config');
        $reflectedPropertyConfig->setAccessible(true);
        $reflectedPropertyConfig->setValue($this->default);
        $this->ref = $reflectedPropertyConfig;
    }
 
    public function testInit()
    {
        $config = [
            // cookie 名称前缀
            'prefix'   => 'think_',
            // cookie 保存时间
            'expire'   => 0,
            // cookie 保存路径
            'path'     => '/path/to/test/',
            // cookie 有效域名
            'domain'   => '.thinkphp.cn',
            //  cookie 启用安全传输
            'secure'   => true,
            // httponly设置
            'httponly' => '1',
        ];
        Cookie::init($config);
 
        $this->assertEquals(
            array_merge($this->default, array_change_key_case($config)),
            $this->ref->getValue()
        );
    }
 
    public function testPrefix()
    {
        $this->assertEquals($this->default['prefix'], Cookie::prefix());
 
        $prefix = '_test_';
        $this->assertNotEquals($prefix, Cookie::prefix());
        Cookie::prefix($prefix);
 
        $config = $this->ref->getValue();
        $this->assertEquals($prefix, $config['prefix']);
    }
 
    public function testSet()
    {
        $value = 'value';
 
        $name = 'name1';
        Cookie::set($name, $value, 10);
        $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
 
        $name = 'name2';
        Cookie::set($name, $value, null);
        $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
 
        $name = 'name3';
        Cookie::set($name, $value, 'expire=100&prefix=pre_');
        $this->assertEquals($value, $_COOKIE['pre_' . $name]);
 
        $name  = 'name4';
        $value = ['_test_中文_'];
        Cookie::set($name, $value);
        $this->assertEquals('think:' . json_encode([urlencode('_test_中文_')]), $_COOKIE[$name]);
    }
 
    public function testGet()
    {
        $_COOKIE = [
            'a'       => 'b',
            'pre_abc' => 'c',
            'd'       => 'think:' . json_encode([urlencode('_test_中文_')]),
        ];
        $this->assertEquals('b', Cookie::get('a'));
        $this->assertEquals(null, Cookie::get('does_not_exist'));
        $this->assertEquals('c', Cookie::get('abc', 'pre_'));
        $this->assertEquals(['_test_中文_'], Cookie::get('d'));
    }
 
    public function testDelete()
    {
        $_COOKIE = [
            'a'       => 'b',
            'pre_abc' => 'c',
        ];
        $this->assertEquals('b', Cookie::get('a'));
        Cookie::delete('a');
        $this->assertEquals(null, Cookie::get('a'));
 
        $this->assertEquals('c', Cookie::get('abc', 'pre_'));
        Cookie::delete('abc', 'pre_');
        $this->assertEquals(null, Cookie::get('abc', 'pre_'));
    }
 
    public function testClear()
    {
        $_COOKIE = [];
        $this->assertEquals(null, Cookie::clear());
 
        $_COOKIE = [
            'a'       => 'b',
            'pre_abc' => 'c',
        ];
        Cookie::clear('pre_');
        $this->assertEquals(['a' => 'b'], $_COOKIE);
    }
}