1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
use ByJG\Session\JwtSession; |
4
|
|
|
|
5
|
|
|
ob_start(); |
6
|
|
|
define("SETCOOKIE_FORTEST", "TESTCASE"); |
7
|
|
|
|
8
|
|
|
class JwtSessionTest extends \PHPUnit\Framework\TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var JwtSession |
12
|
|
|
*/ |
13
|
|
|
protected $object; |
14
|
|
|
|
15
|
|
|
const SESSION_ID = "sessionid"; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->object = new JwtSession("example.com", "secretKey"); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tearDown() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
header_remove(); |
25
|
|
|
$_COOKIE = []; |
26
|
|
|
$this->object = null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function testDestroy() |
31
|
|
|
{ |
32
|
|
|
$this->assertTrue($this->object->destroy(self::SESSION_ID)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGc() |
36
|
|
|
{ |
37
|
|
|
$this->assertTrue($this->object->gc(0)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testClose() |
41
|
|
|
{ |
42
|
|
|
$this->assertTrue($this->object->close()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function dataProvider() |
46
|
|
|
{ |
47
|
|
|
$obj = new stdClass(); |
48
|
|
|
$obj->prop1 = "value1"; |
49
|
|
|
$obj->prop2 = "value2"; |
50
|
|
|
|
51
|
|
|
return |
52
|
|
|
[ |
53
|
|
|
[ |
54
|
|
|
[ |
55
|
|
|
"text" => "simple string" |
56
|
|
|
], |
57
|
|
|
"text|s:13:\"simple string\";" |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
[ |
61
|
|
|
"text" => "simple string", |
62
|
|
|
"text2" => "another string", |
63
|
|
|
"number" => 74 |
64
|
|
|
], |
65
|
|
|
"text|s:13:\"simple string\";text2|s:14:\"another string\";number|i:74;" |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
[ |
69
|
|
|
"text" => [ 1, 2, 3 ] |
70
|
|
|
], |
71
|
|
|
"text|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}" |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
[ |
75
|
|
|
"text" => [ "a" => 1, "b" => 2, "c" => 3 ] |
76
|
|
|
], |
77
|
|
|
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}" |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
[ |
81
|
|
|
"text" => [ "a" => 1, "b" => 2, "c" => 3 ], |
82
|
|
|
"single" => 2000 |
83
|
|
|
], |
84
|
|
|
"text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}single|i:2000;" |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
[ |
88
|
|
|
"text" => $obj |
89
|
|
|
], |
90
|
|
|
"text|O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}" |
91
|
|
|
], |
92
|
|
|
[ |
93
|
|
|
[ |
94
|
|
|
"text" => [ "a" => $obj ] |
95
|
|
|
], |
96
|
|
|
"text|a:1:{s:1:\"a\";O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}" |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
[ |
100
|
|
|
"text" => [ $obj ] |
101
|
|
|
], |
102
|
|
|
"text|a:1:{i:0;O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}" |
103
|
|
|
] |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @dataProvider dataProvider |
109
|
|
|
* @param $input |
110
|
|
|
* @param $expected |
111
|
|
|
*/ |
112
|
|
|
public function testSerializeSessionData($input, $expected) |
113
|
|
|
{ |
114
|
|
|
$result = $this->object->serializeSessionData($input); |
115
|
|
|
$this->assertEquals($expected, $result); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider dataProvider |
120
|
|
|
* @param $expected |
121
|
|
|
* @param $input |
122
|
|
|
* @throws Exception |
123
|
|
|
*/ |
124
|
|
|
public function testUnserializeData($expected, $input) |
125
|
|
|
{ |
126
|
|
|
$result = $this->object->unSerializeSessionData($input); |
127
|
|
|
$this->assertEquals($expected, $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @dataProvider dataProvider |
132
|
|
|
* @param $object |
133
|
|
|
* @param $serialize |
134
|
|
|
*/ |
135
|
|
|
public function testReadWrite($object, $serialize) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$this->object->write("SESSID", $serialize); |
138
|
|
|
$result = $this->object->read("SESSID"); |
139
|
|
|
$this->assertEquals($serialize, $result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.