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\Framework\Http; |
13
|
|
|
|
14
|
|
|
use Spiral\Session\SessionInterface; |
15
|
|
|
use Spiral\Tests\Framework\HttpTest; |
16
|
|
|
|
17
|
|
|
class SessionTest extends HttpTest |
18
|
|
|
{ |
19
|
|
|
public function testSetSid(): void |
20
|
|
|
{ |
21
|
|
|
$this->http->setHandler(function () { |
22
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
$result = $this->get('/'); |
26
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
27
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
28
|
|
|
|
29
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
30
|
|
|
$this->assertArrayHasKey('sid', $cookies); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testSessionResume(): void |
34
|
|
|
{ |
35
|
|
|
$this->http->setHandler(function () { |
36
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$result = $this->get('/'); |
40
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
41
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
42
|
|
|
|
43
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
44
|
|
|
$this->assertArrayHasKey('sid', $cookies); |
45
|
|
|
$result = $this->get('/', [], [], [ |
46
|
|
|
'sid' => $cookies['sid'] |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
50
|
|
|
$this->assertSame('2', $result->getBody()->__toString()); |
51
|
|
|
|
52
|
|
|
$result = $this->get('/', [], [], ['sid' => $cookies['sid']]); |
53
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
54
|
|
|
$this->assertSame('3', $result->getBody()->__toString()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testSessionRegenerateId(): void |
58
|
|
|
{ |
59
|
|
|
$this->http->setHandler(function () { |
60
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$result = $this->get('/'); |
64
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
65
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
66
|
|
|
|
67
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
68
|
|
|
$this->assertArrayHasKey('sid', $cookies); |
69
|
|
|
|
70
|
|
|
$result = $this->get('/', [], [], [ |
71
|
|
|
'sid' => $cookies['sid'] |
72
|
|
|
]); |
73
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
74
|
|
|
$this->assertSame('2', $result->getBody()->__toString()); |
75
|
|
|
|
76
|
|
|
$this->http->setHandler(function () { |
77
|
|
|
$this->session()->regenerateID(false); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$result = $this->get('/', [], [], [ |
83
|
|
|
'sid' => $cookies['sid'] |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$newCookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
87
|
|
|
$this->assertArrayHasKey('sid', $newCookies); |
88
|
|
|
$this->assertNotEquals($cookies['sid'], $newCookies['sid']); |
89
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
90
|
|
|
$this->assertSame('3', $result->getBody()->__toString()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testDestroySession(): void |
94
|
|
|
{ |
95
|
|
|
$this->http->setHandler(function () { |
96
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$result = $this->get('/'); |
100
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
101
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
102
|
|
|
$this->assertArrayHasKey('sid', $cookies); |
103
|
|
|
$result = $this->get('/', [], [], [ |
104
|
|
|
'sid' => $cookies['sid'] |
105
|
|
|
]); |
106
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
107
|
|
|
$this->assertSame('2', $result->getBody()->__toString()); |
108
|
|
|
$this->http->setHandler(function () { |
109
|
|
|
$this->session()->destroy(); |
110
|
|
|
$this->assertFalse($this->session()->isStarted()); |
111
|
|
|
|
112
|
|
|
return ++$this->session()->getSection('cli')->value; |
|
|
|
|
113
|
|
|
}); |
114
|
|
|
$result = $this->get('/', [], [], [ |
115
|
|
|
'sid' => $cookies['sid'] |
116
|
|
|
]); |
117
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
118
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function session(): SessionInterface |
122
|
|
|
{ |
123
|
|
|
return $this->app->get(SessionInterface::class); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|