1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Session; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Spiral\Core\Container; |
16
|
|
|
use Spiral\Files\Files; |
17
|
|
|
use Spiral\Files\FilesInterface; |
18
|
|
|
use Spiral\Session\Config\SessionConfig; |
19
|
|
|
use Spiral\Session\Handler\FileHandler; |
20
|
|
|
use Spiral\Session\SessionSectionInterface; |
21
|
|
|
use Spiral\Session\Session; |
22
|
|
|
use Spiral\Session\SessionFactory; |
23
|
|
|
use Spiral\Session\SessionInterface; |
24
|
|
|
use Spiral\Session\SessionSection; |
25
|
|
|
|
26
|
|
|
class SessionTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SessionFactory |
31
|
|
|
*/ |
32
|
|
|
private $factory; |
33
|
|
|
|
34
|
|
|
public function setUp(): void |
35
|
|
|
{ |
36
|
|
|
$container = new Container(); |
37
|
|
|
$container->bind(FilesInterface::class, Files::class); |
38
|
|
|
|
39
|
|
|
$container->bind(SessionInterface::class, Session::class); |
40
|
|
|
$container->bind(SessionSectionInterface::class, SessionSection::class); |
41
|
|
|
|
42
|
|
|
$this->factory = new SessionFactory(new SessionConfig([ |
43
|
|
|
'lifetime' => 86400, |
44
|
|
|
'cookie' => 'SID', |
45
|
|
|
'secure' => false, |
46
|
|
|
'handler' => new Container\Autowire(FileHandler::class, [ |
47
|
|
|
'directory' => sys_get_temp_dir() |
48
|
|
|
]), |
49
|
|
|
]), $container); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function tearDown(): void |
53
|
|
|
{ |
54
|
|
|
if ((int)session_status() === PHP_SESSION_ACTIVE) { |
55
|
|
|
session_abort(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testValueDestroy(): void |
60
|
|
|
{ |
61
|
|
|
$session = $this->factory->initSession('sig'); |
62
|
|
|
$session->getSection()->set('key', 'value'); |
63
|
|
|
|
64
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
65
|
|
|
|
66
|
|
|
$session->destroy(); |
67
|
|
|
$session->resume(); |
68
|
|
|
|
69
|
|
|
$this->assertNull($session->getSection()->get('key')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public function testValueAbort(): void |
74
|
|
|
{ |
75
|
|
|
$session = $this->factory->initSession('sig'); |
76
|
|
|
$session->getSection()->set('key', 'value'); |
77
|
|
|
|
78
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
79
|
|
|
$id = $session->getID(); |
80
|
|
|
|
81
|
|
|
$session->commit(); |
82
|
|
|
|
83
|
|
|
$session = $this->factory->initSession('sig', $id); |
84
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
85
|
|
|
$session->getSection()->set('key', 'value2'); |
86
|
|
|
$this->assertSame('value2', $session->getSection()->get('key')); |
87
|
|
|
$session->abort(); |
88
|
|
|
|
89
|
|
|
$session = $this->factory->initSession('sig', $id); |
90
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
91
|
|
|
$session->destroy(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testValueRestart(): void |
95
|
|
|
{ |
96
|
|
|
$session = $this->factory->initSession('sig'); |
97
|
|
|
$session->getSection()->set('key', 'value'); |
98
|
|
|
|
99
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
100
|
|
|
$id = $session->getID(); |
101
|
|
|
$session->commit(); |
102
|
|
|
|
103
|
|
|
$session = $this->factory->initSession('sig', $id); |
104
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testValueNewID(): void |
108
|
|
|
{ |
109
|
|
|
$session = $this->factory->initSession('sig'); |
110
|
|
|
$session->getSection()->set('key', 'value'); |
111
|
|
|
|
112
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
113
|
|
|
$id = $session->regenerateID()->getID(); |
114
|
|
|
$session->commit(); |
115
|
|
|
|
116
|
|
|
$session = $this->factory->initSession('sig', $id); |
117
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testSection(): void |
121
|
|
|
{ |
122
|
|
|
$session = $this->factory->initSession('sig'); |
123
|
|
|
$section = $session->getSection('default'); |
124
|
|
|
|
125
|
|
|
$this->assertSame('default', $section->getName()); |
126
|
|
|
|
127
|
|
|
$section->set('key', 'value'); |
128
|
|
|
foreach ($section as $key => $value) { |
129
|
|
|
$this->assertSame('key', $key); |
130
|
|
|
$this->assertSame('value', $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->assertSame('key', $key); |
|
|
|
|
134
|
|
|
$this->assertSame('value', $value); |
|
|
|
|
135
|
|
|
|
136
|
|
|
$this->assertSame('value', $section->pull('key')); |
137
|
|
|
$this->assertNull($section->pull('key')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testSectionClear(): void |
141
|
|
|
{ |
142
|
|
|
$session = $this->factory->initSession('sig'); |
143
|
|
|
$section = $session->getSection('default'); |
144
|
|
|
|
145
|
|
|
$section->set('key', 'value'); |
146
|
|
|
$section->clear(); |
147
|
|
|
$this->assertNull($section->pull('key')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testSectionArrayAccess(): void |
151
|
|
|
{ |
152
|
|
|
$session = $this->factory->initSession('sig'); |
153
|
|
|
$section = $session->getSection('default'); |
154
|
|
|
|
155
|
|
|
$section['key'] = 'value'; |
156
|
|
|
$this->assertSame('value', $section['key']); |
157
|
|
|
$section->key = 'new value'; |
|
|
|
|
158
|
|
|
$this->assertSame('new value', $section->key); |
159
|
|
|
$this->assertTrue(isset($section['key'])); |
160
|
|
|
$this->assertTrue(isset($section->key)); |
161
|
|
|
|
162
|
|
|
$section->delete('key'); |
163
|
|
|
$this->assertFalse(isset($section['key'])); |
164
|
|
|
$this->assertFalse(isset($section->key)); |
165
|
|
|
|
166
|
|
|
$section->key = 'new value'; |
167
|
|
|
unset($section->key); |
168
|
|
|
$this->assertFalse(isset($section->key)); |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
$section->key = 'new value'; |
172
|
|
|
unset($section['key']); |
173
|
|
|
$this->assertFalse(isset($section->key)); |
174
|
|
|
|
175
|
|
|
$section->new = 'another'; |
|
|
|
|
176
|
|
|
|
177
|
|
|
$session->commit(); |
178
|
|
|
|
179
|
|
|
$this->assertNull($section->get('key')); |
180
|
|
|
$this->assertSame('another', $section->get('new')); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testResumeAndID(): void |
184
|
|
|
{ |
185
|
|
|
$session = $this->factory->initSession('sig'); |
186
|
|
|
$session->resume(); |
187
|
|
|
$id = $session->getID(); |
188
|
|
|
|
189
|
|
|
$this->assertTrue($session->isStarted()); |
190
|
|
|
$session->commit(); |
191
|
|
|
|
192
|
|
|
$this->assertFalse($session->isStarted()); |
193
|
|
|
$this->assertSame($id, $session->getID()); |
194
|
|
|
$this->assertFalse($session->isStarted()); |
195
|
|
|
|
196
|
|
|
$session->destroy(); |
197
|
|
|
$this->assertSame($id, $session->getID()); |
198
|
|
|
|
199
|
|
|
$this->assertSame($id, $session->__debugInfo()['id']); |
|
|
|
|
200
|
|
|
$session->regenerateID(); |
201
|
|
|
$this->assertNotSame($id, $session->getID()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testResumeNewSession(): void |
205
|
|
|
{ |
206
|
|
|
$session = $this->factory->initSession('sig'); |
207
|
|
|
$session->resume(); |
208
|
|
|
$id = $session->getID(); |
209
|
|
|
|
210
|
|
|
$this->assertTrue($session->isStarted()); |
211
|
|
|
$session->commit(); |
212
|
|
|
|
213
|
|
|
$session = $this->factory->initSession('sig'); |
214
|
|
|
$session->resume(); |
215
|
|
|
|
216
|
|
|
$this->assertNotSame($id, $session->getID()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testSignatures(): void |
220
|
|
|
{ |
221
|
|
|
$session = $this->factory->initSession('sig'); |
222
|
|
|
$session->getSection()->set('key', 'value'); |
223
|
|
|
$session->commit(); |
224
|
|
|
|
225
|
|
|
$id = $session->getID(); |
226
|
|
|
|
227
|
|
|
$session = $this->factory->initSession('sig', $id); |
228
|
|
|
$this->assertSame('value', $session->getSection()->get('key')); |
229
|
|
|
$this->assertSame($id, $session->getID()); |
230
|
|
|
$session->commit(); |
231
|
|
|
|
232
|
|
|
$session = $this->factory->initSession('different', $id); |
233
|
|
|
$this->assertNull($session->getSection()->get('key')); |
234
|
|
|
$this->assertNotSame($id, $session->getID()); |
235
|
|
|
$session->commit(); |
236
|
|
|
|
237
|
|
|
// must be dead |
238
|
|
|
$session = $this->factory->initSession('sig', $id); |
239
|
|
|
$this->assertNull($session->getSection()->get('key')); |
240
|
|
|
$this->assertNotSame($id, $session->getID()); |
241
|
|
|
$session->commit(); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|