|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Session; |
|
4
|
|
|
|
|
5
|
|
|
trait SessionTestCaseTrait |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var Session |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $SUT; |
|
11
|
|
|
|
|
12
|
|
|
public function test_constructor() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->assertFalse($this->SUT->accessed()); |
|
15
|
|
|
$this->assertFalse($this->SUT->modified()); |
|
16
|
|
|
$this->assertNotEmpty($this->SUT->token()); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function test_get() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->assertSame('bar', $this->SUT->get('foo')); |
|
22
|
|
|
$this->assertTrue($this->SUT->accessed()); |
|
23
|
|
|
$this->assertNull($this->SUT->fubar, 'Non-existing keys returns NULL'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function test_set() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->SUT->set('qux', 'zim'); |
|
29
|
|
|
$this->assertSame('zim', $this->SUT->qux); |
|
30
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
31
|
|
|
|
|
32
|
|
|
$_SESSION['qux'] = 'shmux'; |
|
33
|
|
|
$this->assertSame('shmux', $this->SUT->get('qux')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function test_add() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->SUT->add('foo', 42); |
|
39
|
|
|
$this->assertSame('bar', $this->SUT->foo, 'With add() the value is not replaced if already set'); |
|
40
|
|
|
$this->assertFalse($this->SUT->modified()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_remove() |
|
44
|
|
|
{ |
|
45
|
|
|
// Ensure 2 items |
|
46
|
|
|
$this->SUT->replace(['foo' => 'bar', 'qux' => 'zim']); |
|
47
|
|
|
$this->assertEquals(2, $this->SUT->count(), 'Expecting 2 items in the session'); |
|
48
|
|
|
|
|
49
|
|
|
$this->SUT->remove('foo'); |
|
50
|
|
|
$this->assertEquals(1, $this->SUT->count(), 'Should be 1 item in the session'); |
|
51
|
|
|
$this->assertFalse($this->SUT->has('foo'), 'Only qux should be set'); |
|
52
|
|
|
$this->assertTrue($this->SUT->has('qux')); |
|
53
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_all() |
|
57
|
|
|
{ |
|
58
|
|
|
$data = $this->SUT->all(); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertArrayHasKey('foo', $data); |
|
61
|
|
|
$this->assertArrayHasKey(Session::STAMP, $data); |
|
62
|
|
|
$this->assertArrayHasKey(Session::AGENT, $data); |
|
63
|
|
|
$this->assertArrayHasKey(Session::TOKEN, $data); |
|
64
|
|
|
$this->assertTrue($this->SUT->accessed()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_has() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->assertTrue($this->SUT->has('foo')); |
|
70
|
|
|
$this->assertFalse($this->SUT->has('this_is_not_set')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function test_toData() |
|
74
|
|
|
{ |
|
75
|
|
|
$data = $this->SUT->toData(); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals('bar', $data->foo); |
|
78
|
|
|
$this->assertNull($data->get('_stamp')); |
|
79
|
|
|
$this->assertNull($data->get('_agent')); |
|
80
|
|
|
$this->assertNull($data->get('_token')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/* |
|
84
|
|
|
* |
|
85
|
|
|
* (mutator methods) |
|
86
|
|
|
* |
|
87
|
|
|
*/ |
|
88
|
|
|
|
|
89
|
|
|
public function test_clear() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->assertEquals(['foo' => 'bar'], $this->SUT->toArray()); |
|
92
|
|
|
|
|
93
|
|
|
$this->SUT->clear(); |
|
94
|
|
|
$this->assertEmpty($this->SUT->toArray()); |
|
95
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function test_destroy() |
|
99
|
|
|
{ |
|
100
|
|
|
$sessionId = $this->SUT->id(); |
|
101
|
|
|
$token = $this->SUT->token(); |
|
102
|
|
|
$timestamp = $this->SUT->starttime(); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertNotEmpty($sessionId); |
|
105
|
|
|
$this->assertTrue($this->SUT->destroy()); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertNotEquals($sessionId, $this->SUT->id(), 'Session id is regenerated'); |
|
108
|
|
|
$this->assertNotEquals($token, $this->SUT->token(), 'Session token is regenerated'); |
|
109
|
|
|
$this->assertNotEquals($timestamp, $this->SUT->starttime(), 'Session timestamp is regenerated'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function test_regenerate() |
|
113
|
|
|
{ |
|
114
|
|
|
$sessionId = $this->SUT->id(); |
|
115
|
|
|
$token = $this->SUT->token(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertTrue($this->SUT->regenerate()); |
|
118
|
|
|
$this->assertNotEquals($this->SUT->id(), $sessionId); |
|
119
|
|
|
$this->assertNotEquals($this->SUT->token(), $token, 'The session token is regenerated'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function test_flash() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->assertNull($this->SUT->flash('foo')); |
|
125
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
126
|
|
|
|
|
127
|
|
|
// Set flash data |
|
128
|
|
|
$this->SUT->flash('foo', '123'); |
|
129
|
|
|
$this->assertArrayHasKey(Session::FLASH, $_SESSION); |
|
130
|
|
|
|
|
131
|
|
|
// Get flash data |
|
132
|
|
|
$this->assertSame(['foo' => '123'], $this->SUT->flash('foo'), 'After flash, the key is unset'); |
|
133
|
|
|
$this->assertArrayNotHasKey(Session::FLASH, $_SESSION); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function test_replace() |
|
137
|
|
|
{ |
|
138
|
|
|
$newData = ['name' => 'value']; |
|
139
|
|
|
$oldData = $this->SUT->replace($newData); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertSame($newData, $this->SUT->toArray(), 'The session data is replaced'); |
|
142
|
|
|
$this->assertArraySubset(['foo' => 'bar'], $oldData); |
|
143
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function test_import_ignores_non_string_keys() |
|
147
|
|
|
{ |
|
148
|
|
|
$oldData = $this->SUT->toArray(); |
|
149
|
|
|
$newData = [1 => 2]; |
|
150
|
|
|
|
|
151
|
|
|
$this->SUT->import($newData); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertSame($oldData, $this->SUT->toArray(), 'The non-string keys are ignored'); |
|
154
|
|
|
$this->assertArraySubset(['foo' => 'bar'], $oldData); |
|
155
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function test_import_should_import() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->SUT->import(['name' => 'changed']); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals([ |
|
163
|
|
|
'name' => 'changed', |
|
164
|
|
|
'foo' => 'bar' |
|
165
|
|
|
], $this->SUT->toArray(), 'The existing session variables are replaced'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/* |
|
169
|
|
|
* |
|
170
|
|
|
* (support methods) |
|
171
|
|
|
* |
|
172
|
|
|
*/ |
|
173
|
|
|
|
|
174
|
|
|
public function test_id() |
|
175
|
|
|
{ |
|
176
|
|
|
$this->assertNotEmpty($this->SUT->id()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function test_accessed() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->assertFalse($this->SUT->accessed()); |
|
182
|
|
|
$value = $this->SUT->foo; |
|
183
|
|
|
$this->assertTrue($this->SUT->accessed()); |
|
184
|
|
|
$this->assertSame('bar', $value); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function test_modified() |
|
188
|
|
|
{ |
|
189
|
|
|
$this->assertFalse($this->SUT->modified()); |
|
190
|
|
|
|
|
191
|
|
|
$this->SUT->foo = 'zim'; |
|
192
|
|
|
$this->assertTrue($this->SUT->modified()); |
|
193
|
|
|
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function test_starttime() |
|
197
|
|
|
{ |
|
198
|
|
|
$starttime = time(); |
|
199
|
|
|
$this->assertGreaterThanOrEqual($starttime, $this->SUT->starttime()); |
|
200
|
|
|
$this->assertInternalType('integer', $starttime); |
|
201
|
|
|
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function test_useragent() |
|
205
|
|
|
{ |
|
206
|
|
|
$this->assertSame('Koded/Session', $this->SUT->agent()); |
|
207
|
|
|
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function test_isEmpty() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->assertFalse($this->SUT->isEmpty()); |
|
213
|
|
|
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
protected function tearDown() |
|
217
|
|
|
{ |
|
218
|
|
|
session_write_close(); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|