Completed
Pull Request — 5.1 (#1327)
by
unknown
05:23
created

sessionTest::testInit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 63
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 39
nc 2
nop 0
dl 0
loc 63
rs 9.296
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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 <[email protected]>
10
// +----------------------------------------------------------------------
11
12
/**
13
 * Session测试
14
 * @author    大漠 <[email protected]>
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
15
 */
16
17
namespace tests\thinkphp\library\think;
18
19
use think\Session;
0 ignored issues
show
Bug introduced by
The type think\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
class sessionTest extends \PHPUnit_Framework_TestCase
1 ignored issue
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Missing class doc comment
Loading history...
Coding Style introduced by
Class name must begin with a capital letter
Loading history...
22
{
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     *
26
     * @var \think\Session
27
     */
28
    protected $object;
29
30
    /**
31
     * Sets up the fixture, for example, opens a network connection.
32
     * This method is called before a test is executed.
33
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
34
    protected function setUp()
35
    {
36
        // $this->object = new Session ();
37
        // register_shutdown_function ( function () {
38
        // } ); // 此功能无法取消,需要回调函数配合。
39
        set_exception_handler(function () {});
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
40
        set_error_handler(function () {});
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
41
    }
42
43
    /**
44
     * Tears down the fixture, for example, closes a network connection.
45
     * This method is called after a test is executed.
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    protected function tearDown()
48
    {
49
        register_shutdown_function('think\Error::appShutdown');
50
        set_error_handler('think\Error::appError');
51
        set_exception_handler('think\Error::appException');
52
    }
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @covers think\Session::prefix
56
     *
57
     * @todo Implement testPrefix().
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59
    public function testPrefix()
60
    {
61
        Session::prefix(null);
62
        Session::prefix('think_');
63
64
        $this->assertEquals('think_', Session::prefix());
65
    }
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @covers think\Session::init
69
     *
70
     * @todo Implement testInit().
71
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
72
    public function testInit()
73
    {
74
        Session::prefix(null);
75
        $config = [
76
            // cookie 名称前缀
77
            'prefix'         => 'think_',
78
            // cookie 保存时间
79
            'expire'         => 60,
80
            // cookie 保存路径
81
            'path'           => '/path/to/test/session/',
82
            // cookie 有效域名
83
            'domain'         => '.thinkphp.cn',
84
            'var_session_id' => 'sessionidtest',
85
            'id'             => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
86
            'name'           => 'session_name',
87
            'use_trans_sid'  => '1',
88
            'use_cookies'    => '1',
89
            'cache_limiter'  => '60',
90
            'cache_expire'   => '60',
91
            'type'           => '', // memcache
92
            'namespace'      => '\\think\\session\\driver\\', // ?
93
            'auto_start'     => '1',
94
        ];
95
96
        $_REQUEST[$config['var_session_id']] = $config['id'];
97
        Session::init($config);
98
99
        // 开始断言
100
        $this->assertEquals($config['prefix'], Session::prefix());
101
        $this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]);
102
        $this->assertEquals($config['name'], session_name());
103
104
        $this->assertEquals($config['path'], session_save_path());
105
        $this->assertEquals($config['use_cookies'], ini_get('session.use_cookies'));
106
        $this->assertEquals($config['domain'], ini_get('session.cookie_domain'));
107
        $this->assertEquals($config['expire'], ini_get('session.gc_maxlifetime'));
108
        $this->assertEquals($config['expire'], ini_get('session.cookie_lifetime'));
109
110
        $this->assertEquals($config['cache_limiter'], session_cache_limiter($config['cache_limiter']));
111
        $this->assertEquals($config['cache_expire'], session_cache_expire($config['cache_expire']));
112
113
        // 检测分支
114
        $_REQUEST[$config['var_session_id']] = null;
115
        session_write_close();
116
        session_destroy();
117
118
        Session::init($config);
119
120
        // 测试auto_start
121
        // PHP_SESSION_DISABLED
122
        // PHP_SESSION_NONE
123
        // PHP_SESSION_ACTIVE
124
        // session_status()
125
        if (strstr(PHP_VERSION, 'hhvm')) {
126
            $this->assertEquals('', ini_get('session.auto_start'));
127
        } else {
128
            $this->assertEquals(0, ini_get('session.auto_start'));
129
        }
130
131
        $this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
132
133
        Session::init($config);
134
        $this->assertEquals($config['id'], session_id());
135
    }
136
137
    /**
138
     * 单独重现异常
139
     * @expectedException \think\Exception
140
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
141
    public function testException()
142
    {
143
        $config = [
144
            // cookie 名称前缀
145
            'prefix'         => 'think_',
146
            // cookie 保存时间
147
            'expire'         => 0,
148
            // cookie 保存路径
149
            'path'           => '/path/to/test/session/',
150
            // cookie 有效域名
151
            'domain'         => '.thinkphp.cn',
152
            'var_session_id' => 'sessionidtest',
153
            'id'             => 'sess_8fhgkjuakhatbeg2fa14lo84q1',
154
            'name'           => 'session_name',
155
            'use_trans_sid'  => '1',
156
            'use_cookies'    => '1',
157
            'cache_limiter'  => '60',
158
            'cache_expire'   => '60',
159
            'type'           => '\\think\\session\\driver\\Memcache', //
160
            'auto_start'     => '1',
161
        ];
162
163
        // 测试session驱动是否存在
164
        // @expectedException 异常类名
165
        $this->setExpectedException('\think\exception\ClassNotFoundException', 'error session handler');
166
167
        Session::init($config);
168
    }
169
170
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
171
     * @covers think\Session::set
172
     *
173
     * @todo Implement testSet().
174
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
175
    public function testSet()
176
    {
177
        Session::prefix(null);
178
        Session::set('sessionname', 'sessionvalue');
179
        $this->assertEquals('sessionvalue', $_SESSION['sessionname']);
180
181
        Session::set('sessionnamearr.subname', 'sessionvalue');
182
        $this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']);
183
184
        Session::set('sessionnameper', 'sessionvalue', 'think_');
185
        $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']);
186
187
        Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_');
188
        $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']);
189
    }
190
191
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
192
     * @covers think\Session::get
193
     *
194
     * @todo Implement testGet().
195
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
196
    public function testGet()
197
    {
198
        Session::prefix(null);
199
200
        Session::set('sessionnameget', 'sessionvalue');
201
        $this->assertEquals(Session::get('sessionnameget'), $_SESSION['sessionnameget']);
202
203
        Session::set('sessionnamegetarr.subname', 'sessionvalue');
204
        $this->assertEquals(Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']);
205
206
        Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_');
207
        $this->assertEquals(Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']);
208
209
        Session::set('sessionnamegetper', 'sessionvalue', 'think_');
210
        $this->assertEquals(Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']);
211
212
        Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_');
213
        $this->assertEquals(Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']);
214
    }
215
216
    public function testPull()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
217
    {
218
        Session::prefix(null);
219
        Session::set('sessionnamedel', 'sessionvalue');
220
        $this->assertEquals('sessionvalue', Session::pull('sessionnameget'));
221
        $this->assertNull(Session::get('sessionnameget'));
222
    }
223
224
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
225
     * @covers think\Session::delete
226
     *
227
     * @todo Implement testDelete().
228
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
229
    public function testDelete()
230
    {
231
        Session::prefix(null);
232
        Session::set('sessionnamedel', 'sessionvalue');
233
        Session::delete('sessionnamedel');
234
        $this->assertEmpty($_SESSION['sessionnamedel']);
235
236
        Session::set('sessionnamedelarr.subname', 'sessionvalue');
237
        Session::delete('sessionnamedelarr.subname');
238
        $this->assertEmpty($_SESSION['sessionnamedelarr']['subname']);
239
240
        Session::set('sessionnamedelper', 'sessionvalue', 'think_');
241
        Session::delete('sessionnamedelper', 'think_');
242
        $this->assertEmpty($_SESSION['think_']['sessionnamedelper']);
243
244
        Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_');
245
        Session::delete('sessionnamedelperarr.subname', 'think_');
246
        $this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']);
247
    }
248
249
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
250
     * @covers think\Session::clear
251
     *
252
     * @todo Implement testClear().
253
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
254
    public function testClear()
255
    {
256
        Session::prefix(null);
257
258
        Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
259
        Session::clear('think_');
260
        $this->assertNull($_SESSION['think_']);
261
262
        Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
263
        Session::clear();
264
        $this->assertEmpty($_SESSION);
265
    }
266
267
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
268
     * @covers think\Session::has
269
     *
270
     * @todo Implement testHas().
271
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
272
    public function testHas()
273
    {
274
        Session::prefix(null);
275
        Session::set('sessionnamehas', 'sessionvalue');
276
        $this->assertTrue(Session::has('sessionnamehas'));
277
278
        Session::set('sessionnamehasarr.subname', 'sessionvalue');
279
        $this->assertTrue(Session::has('sessionnamehasarr.subname'));
280
281
        Session::set('sessionnamehasper', 'sessionvalue', 'think_');
282
        $this->assertTrue(Session::has('sessionnamehasper', 'think_'));
283
284
        Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_');
285
        $this->assertTrue(Session::has('sessionnamehasarrper.subname', 'think_'));
286
    }
287
288
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
289
     * @covers think\Session::pause
290
     *
291
     * @todo Implement testPause().
292
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
293
    public function testPause()
294
    {
295
        Session::pause();
296
    }
297
298
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
299
     * @covers think\Session::start
300
     *
301
     * @todo Implement testStart().
302
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
303
    public function testStart()
304
    {
305
        Session::start();
306
    }
307
308
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
309
     * @covers think\Session::destroy
310
     *
311
     * @todo Implement testDestroy().
312
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
313
    public function testDestroy()
314
    {
315
        Session::set('sessionnamedestroy', 'sessionvalue');
316
        Session::destroy();
317
        $this->assertEmpty($_SESSION['sessionnamedestroy']);
318
    }
319
}
320