-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeSessionContextTest.php
More file actions
152 lines (123 loc) · 4.88 KB
/
Copy pathNativeSessionContextTest.php
File metadata and controls
152 lines (123 loc) · 4.88 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
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Session package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Polymorphine\Session\Tests\SessionContext;
use PHPUnit\Framework\TestCase;
use Polymorphine\Session\SessionContext;
use Polymorphine\Session\SessionStorageProvider;
use Polymorphine\Session\Tests\Fixtures\SessionGlobalState;
use Psr\Http\Server\MiddlewareInterface;
use Polymorphine\Session\Tests\Doubles;
use RuntimeException;
require_once dirname(__DIR__) . '/Fixtures/session-functions.php';
class NativeSessionContextTest extends TestCase
{
protected function tearDown(): void
{
SessionGlobalState::reset();
}
public function test_Instantiation()
{
$context = $this->context();
$this->assertInstanceOf(MiddlewareInterface::class, $context);
$this->assertInstanceOf(SessionContext::class, $context);
$this->assertInstanceOf(SessionStorageProvider::class, $context);
}
public function test_SessionName_IsSynchronizedWithCookieName()
{
$cookie = new Doubles\MockedCookie('MySESSION');
$this->context($cookie)->process($this->request(), $this->handler());
$this->assertSame('MySESSION', SessionGlobalState::$name);
}
public function test_SessionInitialization()
{
$context = $this->context($cookie);
$handler = $this->handler(function () use ($context) {
$context->storage()->set('foo', 'bar');
});
$context->process($this->request(), $handler);
$this->assertSame(['foo' => 'bar'], SessionGlobalState::$data);
$this->assertSame('DefaultSessionId', $cookie->value);
}
public function test_SessionResume()
{
SessionGlobalState::$data = ['foo' => 'bar'];
$context = $this->context($cookie);
$handler = $this->handler(function () use ($context) {
$session = $context->storage();
$session->set('foo', $session->get('foo') . '-baz');
});
$context->process($this->request(true), $handler);
$this->assertSame(['foo' => 'bar-baz'], SessionGlobalState::$data);
$this->assertNull($cookie->value);
}
public function test_SessionRegenerateId()
{
SessionGlobalState::$data = ['foo' => 'bar'];
$context = $this->context($cookie);
$handler = $this->handler(function () use ($context) {
$context->reset();
});
$context->process($this->request(true), $handler);
$this->assertSame(['foo' => 'bar'], SessionGlobalState::$data);
$this->assertSame('RegeneratedSessionId', $cookie->value);
}
public function test_SessionDestroy()
{
SessionGlobalState::$data = ['foo' => 'bar'];
$context = $this->context($cookie);
$handler = $this->handler(function () use ($context) {
$context->storage()->clear();
});
$context->process($this->request(true), $handler);
$this->assertSame([], SessionGlobalState::$data);
$this->assertTrue($cookie->deleted);
}
public function test_ClearedSession_WithNewData_RegeneratesId()
{
SessionGlobalState::$data = ['foo' => 'bar'];
$context = $this->context($cookie);
$handler = $this->handler(function () use ($context) {
$storage = $context->storage();
$storage->clear();
$storage->set('baz', 'qux');
});
$context->process($this->request(true), $handler);
$this->assertSame('RegeneratedSessionId', SessionGlobalState::$id);
$this->assertSame('RegeneratedSessionId', $cookie->value);
}
public function test_ProcessingWhileSessionStarted_ThrowsException()
{
SessionGlobalState::$status = PHP_SESSION_ACTIVE;
$context = $this->context();
$this->expectException(RuntimeException::class);
$context->process($this->request(true), $this->handler());
}
public function test_CallingSessionWithoutContextProcessing_ThrowsException()
{
$context = $this->context();
$this->expectException(RuntimeException::class);
$context->storage();
}
private function request($cookie = false): Doubles\FakeServerRequest
{
return $cookie
? Doubles\FakeServerRequest::withSessionCookie(SessionGlobalState::$name, SessionGlobalState::$id)
: new Doubles\FakeServerRequest();
}
private function handler(?callable $process = null): Doubles\FakeRequestHandler
{
return new Doubles\FakeRequestHandler($process);
}
private function context(?Doubles\MockedCookie &$cookie = null): SessionContext\NativeSessionContext
{
$cookie ??= new Doubles\MockedCookie(SessionGlobalState::$name);
return new SessionContext\NativeSessionContext($cookie);
}
}