Repository navigation
Expand file tree
/
Copy pathFileSessionTest.php
More file actions
334 lines (249 loc) · 10.3 KB
/
Copy pathFileSessionTest.php
File metadata and controls
334 lines (249 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
declare(strict_types=1);
namespace Tests\Tempest\Integration\Http;
use PHPUnit\Framework\Attributes\PostCondition;
use PHPUnit\Framework\Attributes\PreCondition;
use PHPUnit\Framework\Attributes\Test;
use Tempest\Clock\Clock;
use Tempest\Core\FrameworkKernel;
use Tempest\DateTime\Duration;
use Tempest\Http\Session\Config\FileSessionConfig;
use Tempest\Http\Session\Managers\FileSessionManager;
use Tempest\Http\Session\Session;
use Tempest\Http\Session\SessionCreated;
use Tempest\Http\Session\SessionDeleted;
use Tempest\Http\Session\SessionId;
use Tempest\Http\Session\SessionIdResolver;
use Tempest\Http\Session\SessionManager;
use Tempest\Support\Filesystem;
use Tempest\Support\Path;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
/**
* @internal
*/
final class FileSessionTest extends FrameworkIntegrationTestCase
{
private string $path = __DIR__ . '/Fixtures/tmp';
private SessionManager $manager {
get => $this->container->get(SessionManager::class);
}
private Session $session {
get => $this->container->get(Session::class);
}
#[PreCondition]
protected function configure(): void
{
Filesystem\ensure_directory_empty($this->path);
$this->path = realpath($this->path);
$this->container->get(FrameworkKernel::class)->internalStorage = realpath($this->path);
$this->container->config(new FileSessionConfig(
expiration: Duration::hours(2),
path: 'sessions',
));
$this->container->singleton(SessionManager::class, fn () => new FileSessionManager(
$this->container->get(Clock::class),
$this->container->get(FileSessionConfig::class),
$this->container->get(SessionIdResolver::class),
));
}
#[PostCondition]
protected function cleanup(): void
{
Filesystem\delete_directory($this->path);
}
#[Test]
public function get_or_create_creates_new_session(): void
{
$this->eventBus->preventEventHandling();
$sessionId = new SessionId('new_session');
$session = $this->manager->getOrCreate($sessionId);
$this->assertInstanceOf(Session::class, $session);
$this->assertEquals($sessionId, $session->id);
$this->eventBus->assertDispatched(
event: SessionCreated::class,
callback: function (SessionCreated $event) use ($sessionId): void {
$this->assertEquals($sessionId, $event->session->id);
},
count: 1,
);
}
#[Test]
public function get_or_create_loads_existing_session(): void
{
$sessionId = new SessionId('existing_session');
$session = $this->manager->getOrCreate($sessionId);
$session->set('key', 'value');
$this->manager->save($session);
$loaded = $this->manager->getOrCreate($sessionId);
$this->assertEquals($sessionId, $loaded->id);
$this->assertTrue($session->createdAt->isSameMinute($loaded->createdAt));
$this->assertEquals('value', $loaded->get('key'));
}
#[Test]
public function save_persists_session_to_file(): void
{
$this->session->set('test_key', 'test_value');
$this->manager->save($this->session);
$path = Path\normalize($this->path, 'sessions', (string) $this->session->id);
$this->assertFileExists($path);
$content = unserialize(file_get_contents($path));
$this->assertInstanceOf(Session::class, $content);
$this->assertEquals('test_value', $content->get('test_key'));
}
#[Test]
public function save_updates_last_active_timestamp(): void
{
$clock = $this->clock('2025-01-01 00:00:00');
$original = $this->session->lastActiveAt;
// session created with current timestamp
$this->assertTrue($this->session->lastActiveAt->equals($clock->now()));
// save it 5 minutes later
$clock->plus(Duration::minutes(5));
$this->manager->save($this->session);
// last active at has updated
$this->assertTrue($this->session->lastActiveAt->after($original));
}
#[Test]
public function delete_removes_session_file(): void
{
$this->eventBus->preventEventHandling();
$this->manager->save($this->session);
$path = Path\normalize($this->path, 'sessions', (string) $this->session->id);
$this->assertFileExists($path);
$this->manager->delete($this->session);
$this->assertFileDoesNotExist($path);
$this->eventBus->assertDispatched(
event: SessionDeleted::class,
callback: function (SessionDeleted $event): void {
$this->assertEquals($this->session->id, $event->id);
},
count: 1,
);
}
#[Test]
public function regenerate_replaces_the_session_file(): void
{
$this->session->set('key', 'value');
$this->manager->save($this->session);
$previousPath = Path\normalize($this->path, 'sessions', (string) $this->session->id);
$previousId = (string) $this->session->id;
$this->manager->regenerate($this->session);
$this->assertNotSame($previousId, (string) $this->session->id);
$this->assertFileDoesNotExist($previousPath);
$path = Path\normalize($this->path, 'sessions', (string) $this->session->id);
$this->assertFileExists($path);
$this->assertSame('value', $this->session->get('key'));
}
#[Test]
public function is_valid_checks_expiration(): void
{
$clock = $this->clock('2025-01-01 00:00:00');
$this->container->config(new FileSessionConfig(
expiration: Duration::seconds(10),
path: 'test_sessions',
));
$session = $this->manager->getOrCreate(new SessionId('expiration_test'));
$this->manager->save($session);
$this->assertTrue($this->manager->isValid($session));
$clock->plus(Duration::seconds(5));
$this->assertTrue($this->manager->isValid($session));
$clock->plus(Duration::seconds(6));
$this->assertFalse($this->manager->isValid($session));
}
#[Test]
public function delete_expired_sessions_removes_old_files(): void
{
$this->eventBus->preventEventHandling();
$clock = $this->clock('2023-01-01 00:00:00');
$this->container->config(new FileSessionConfig(
expiration: Duration::minutes(30),
path: 'test_sessions',
));
$active = $this->manager->getOrCreate(new SessionId('active'));
$this->saveAtClockTime($active, 'test_sessions');
$expired = $this->manager->getOrCreate(new SessionId('expired'));
$this->saveAtClockTime($expired, 'test_sessions');
// we expire the $expired one
$clock->plus(Duration::minutes(35));
// keep active session fresh
$this->saveAtClockTime($active, 'test_sessions');
$activePath = Path\normalize($this->path, 'test_sessions', (string) $active->id);
$expiredPath = Path\normalize($this->path, 'test_sessions', (string) $expired->id);
$this->assertFileExists($activePath);
$this->assertFileExists($expiredPath);
$this->manager->deleteExpiredSessions();
$this->assertFileExists($activePath);
$this->assertFileDoesNotExist($expiredPath);
$this->eventBus->assertDispatched(
event: SessionDeleted::class,
callback: function (SessionDeleted $event) use ($expired): void {
$this->assertEquals($expired->id, $event->id);
},
count: 1,
);
}
#[Test]
public function delete_expired_sessions_removes_unreadable_files(): void
{
$this->eventBus->preventEventHandling();
$clock = $this->clock('2023-01-01 00:00:00');
$this->container->config(new FileSessionConfig(
expiration: Duration::minutes(30),
path: 'test_sessions',
));
$corruptPath = Path\normalize($this->path, 'test_sessions', 'corrupt');
Filesystem\write_file($corruptPath, 'not a serialized session');
touch($corruptPath, $clock->now()->getTimestamp()->getSeconds());
$clock->plus(Duration::minutes(35));
$this->manager->deleteExpiredSessions();
$this->assertFileDoesNotExist($corruptPath);
$this->eventBus->assertDispatched(
event: SessionDeleted::class,
callback: function (SessionDeleted $event): void {
$this->assertEquals(new SessionId('corrupt'), $event->id);
},
count: 1,
);
}
#[Test]
public function unchanged_session_is_not_rewritten(): void
{
$clock = $this->clock('2025-01-01 00:00:00');
$id = new SessionId('unchanged');
$path = Path\normalize($this->path, 'sessions', (string) $id);
$session = $this->manager->getOrCreate($id);
$session->set('user', 'brent');
$this->manager->save($session);
$unchanged = $this->manager->getOrCreate($id);
// Another request writes to the same session in the meantime
$concurrent = $this->manager->getOrCreate($id);
$concurrent->set('cart', 'book');
$this->manager->save($concurrent);
$clock->plus(Duration::minutes(5));
$this->manager->save($unchanged);
clearstatcache();
$this->assertSame('book', $this->manager->getOrCreate($id)->get('cart'));
$this->assertSame($clock->now()->getTimestamp()->getSeconds(), filemtime($path));
}
#[Test]
public function last_active_at_is_read_from_modification_time(): void
{
$clock = $this->clock('2025-01-01 00:00:00');
$id = new SessionId('active');
$session = $this->manager->getOrCreate($id);
$session->set('user', 'brent');
$this->manager->save($session);
$clock->plus(Duration::minutes(5));
$this->manager->save($this->manager->getOrCreate($id));
clearstatcache();
$this->assertTrue($this->manager->getOrCreate($id)->lastActiveAt->equals($clock->now()));
}
private function saveAtClockTime(Session $session, string $path): void
{
$this->manager->save($session);
touch(
Path\normalize($this->path, $path, (string) $session->id),
$session->lastActiveAt->getTimestamp()->getSeconds(),
);
}
}