|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace think\tests; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery as m; |
|
6
|
|
|
use Mockery\MockInterface; |
|
7
|
|
|
use org\bovigo\vfs\vfsStream; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use think\App; |
|
10
|
|
|
use think\cache\Driver; |
|
11
|
|
|
use think\Config; |
|
12
|
|
|
use think\Container; |
|
13
|
|
|
use think\contract\SessionHandlerInterface; |
|
14
|
|
|
use think\helper\Str; |
|
15
|
|
|
use think\Session; |
|
16
|
|
|
use think\session\driver\Cache; |
|
17
|
|
|
use think\session\driver\File; |
|
18
|
|
|
|
|
19
|
|
|
class SessionTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var App|MockInterface */ |
|
22
|
|
|
protected $app; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Session|MockInterface */ |
|
25
|
|
|
protected $session; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Config|MockInterface */ |
|
28
|
|
|
protected $config; |
|
29
|
|
|
|
|
30
|
|
|
protected $handler; |
|
31
|
|
|
|
|
32
|
|
|
protected function tearDown(): void |
|
33
|
|
|
{ |
|
34
|
|
|
m::close(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function setUp() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->app = m::mock(App::class)->makePartial(); |
|
40
|
|
|
Container::setInstance($this->app); |
|
41
|
|
|
|
|
42
|
|
|
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app); |
|
43
|
|
|
$this->config = m::mock(Config::class)->makePartial(); |
|
44
|
|
|
|
|
45
|
|
|
$this->app->shouldReceive('get')->with('config')->andReturn($this->config); |
|
46
|
|
|
$handlerClass = "\\think\\session\\driver\\Test" . Str::random(10); |
|
47
|
|
|
$this->config->shouldReceive("get")->with("session.type", "file")->andReturn($handlerClass); |
|
48
|
|
|
$this->session = new Session($this->app); |
|
49
|
|
|
|
|
50
|
|
|
$this->handler = m::mock('overload:' . $handlerClass, SessionHandlerInterface::class); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testLoadData() |
|
54
|
|
|
{ |
|
55
|
|
|
$data = [ |
|
56
|
|
|
"bar" => 'foo', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$id = md5(uniqid()); |
|
60
|
|
|
|
|
61
|
|
|
$this->handler->shouldReceive("read")->once()->with($id)->andReturn(serialize($data)); |
|
62
|
|
|
|
|
63
|
|
|
$this->session->setId($id); |
|
64
|
|
|
$this->session->init(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals('foo', $this->session->get('bar')); |
|
67
|
|
|
$this->assertTrue($this->session->has('bar')); |
|
68
|
|
|
$this->assertFalse($this->session->has('foo')); |
|
69
|
|
|
|
|
70
|
|
|
$this->session->set('foo', 'bar'); |
|
71
|
|
|
$this->assertTrue($this->session->has('foo')); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals('bar', $this->session->pull('foo')); |
|
74
|
|
|
$this->assertFalse($this->session->has('foo')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testSave() |
|
78
|
|
|
{ |
|
79
|
|
|
|
|
80
|
|
|
$id = md5(uniqid()); |
|
81
|
|
|
|
|
82
|
|
|
$this->handler->shouldReceive('read')->once()->with($id)->andReturn(""); |
|
83
|
|
|
|
|
84
|
|
|
$this->handler->shouldReceive('write')->once()->with($id, serialize([ |
|
85
|
|
|
"bar" => 'foo', |
|
86
|
|
|
]))->andReturnTrue(); |
|
87
|
|
|
|
|
88
|
|
|
$this->session->setId($id); |
|
89
|
|
|
$this->session->init(); |
|
90
|
|
|
|
|
91
|
|
|
$this->session->set('bar', 'foo'); |
|
92
|
|
|
|
|
93
|
|
|
$this->session->save(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testFlash() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->session->flash('foo', 'bar'); |
|
99
|
|
|
$this->session->flash('bar', 0); |
|
100
|
|
|
$this->session->flash('baz', true); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertTrue($this->session->has('foo')); |
|
103
|
|
|
$this->assertEquals('bar', $this->session->get('foo')); |
|
104
|
|
|
$this->assertEquals(0, $this->session->get('bar')); |
|
105
|
|
|
$this->assertTrue($this->session->get('baz')); |
|
106
|
|
|
|
|
107
|
|
|
$this->session->clearFlashData(); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertTrue($this->session->has('foo')); |
|
110
|
|
|
$this->assertEquals('bar', $this->session->get('foo')); |
|
111
|
|
|
$this->assertEquals(0, $this->session->get('bar')); |
|
112
|
|
|
|
|
113
|
|
|
$this->session->clearFlashData(); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertFalse($this->session->has('foo')); |
|
116
|
|
|
$this->assertNull($this->session->get('foo')); |
|
117
|
|
|
|
|
118
|
|
|
$this->session->flash('foo', 'bar'); |
|
119
|
|
|
$this->assertTrue($this->session->has('foo')); |
|
120
|
|
|
$this->session->clearFlashData(); |
|
121
|
|
|
$this->session->reflash(); |
|
122
|
|
|
$this->session->clearFlashData(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertTrue($this->session->has('foo')); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testClear() |
|
128
|
|
|
{ |
|
129
|
|
|
$this->session->set('bar', 'foo'); |
|
130
|
|
|
$this->assertEquals('foo', $this->session->get('bar')); |
|
131
|
|
|
$this->session->clear(); |
|
132
|
|
|
$this->assertFalse($this->session->has('foo')); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testSetName() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->session->setName('foo'); |
|
138
|
|
|
$this->assertEquals('foo', $this->session->getName()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testDestroy() |
|
142
|
|
|
{ |
|
143
|
|
|
$id = md5(uniqid()); |
|
144
|
|
|
|
|
145
|
|
|
$this->handler->shouldReceive('read')->once()->with($id)->andReturn(""); |
|
146
|
|
|
$this->handler->shouldReceive('delete')->once()->with($id)->andReturnTrue(); |
|
147
|
|
|
|
|
148
|
|
|
$this->session->setId($id); |
|
149
|
|
|
$this->session->init(); |
|
150
|
|
|
|
|
151
|
|
|
$this->session->set('bar', 'foo'); |
|
152
|
|
|
|
|
153
|
|
|
$this->session->destroy(); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertFalse($this->session->has('bar')); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertNotEquals($id, $this->session->getId()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testFileHandler() |
|
161
|
|
|
{ |
|
162
|
|
|
$root = vfsStream::setup(); |
|
163
|
|
|
|
|
164
|
|
|
vfsStream::newFile('bar') |
|
165
|
|
|
->at($root) |
|
166
|
|
|
->lastModified(time()); |
|
167
|
|
|
|
|
168
|
|
|
vfsStream::newFile('bar') |
|
169
|
|
|
->at(vfsStream::newDirectory("foo")->at($root)) |
|
170
|
|
|
->lastModified(100); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertTrue($root->hasChild("bar")); |
|
173
|
|
|
$this->assertTrue($root->hasChild("foo/bar")); |
|
174
|
|
|
|
|
175
|
|
|
$handler = new TestFileHandle($this->app, [ |
|
176
|
|
|
'path' => $root->url(), |
|
177
|
|
|
'gc_probability' => 1, |
|
178
|
|
|
'gc_divisor' => 1, |
|
179
|
|
|
]); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertTrue($root->hasChild("bar")); |
|
182
|
|
|
$this->assertFalse($root->hasChild("foo/bar")); |
|
183
|
|
|
|
|
184
|
|
|
$id = md5(uniqid()); |
|
185
|
|
|
$handler->write($id, "bar"); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertTrue($root->hasChild("sess_{$id}")); |
|
188
|
|
|
|
|
189
|
|
|
$this->assertEquals("bar", $handler->read($id)); |
|
190
|
|
|
|
|
191
|
|
|
$handler->delete($id); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertFalse($root->hasChild("sess_{$id}")); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function testCacheHandler() |
|
197
|
|
|
{ |
|
198
|
|
|
$id = md5(uniqid()); |
|
199
|
|
|
|
|
200
|
|
|
$cache = m::mock(\think\Cache::class); |
|
201
|
|
|
|
|
202
|
|
|
$store = m::mock(Driver::class); |
|
203
|
|
|
|
|
204
|
|
|
$cache->shouldReceive('store')->once()->with('redis')->andReturn($store); |
|
205
|
|
|
|
|
206
|
|
|
$handler = new Cache($cache, ['store' => 'redis']); |
|
207
|
|
|
|
|
208
|
|
|
$store->shouldReceive("set")->with($id, "bar", 1440)->once()->andReturnTrue(); |
|
209
|
|
|
$handler->write($id, "bar"); |
|
210
|
|
|
|
|
211
|
|
|
$store->shouldReceive("get")->with($id)->once()->andReturn("bar"); |
|
212
|
|
|
$this->assertEquals("bar", $handler->read($id)); |
|
213
|
|
|
|
|
214
|
|
|
$store->shouldReceive("delete")->with($id)->once()->andReturnTrue(); |
|
215
|
|
|
$handler->delete($id); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
class TestFileHandle extends File |
|
220
|
|
|
{ |
|
221
|
|
|
protected function writeFile($path, $content): bool |
|
222
|
|
|
{ |
|
223
|
|
|
return (bool) file_put_contents($path, $content); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|