-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionTestCaseTrait.php
More file actions
executable file
·191 lines (157 loc) · 5.31 KB
/
SessionTestCaseTrait.php
File metadata and controls
executable file
·191 lines (157 loc) · 5.31 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
<?php
namespace Koded\Session;
trait SessionTestCaseTrait
{
/**
* @var Session
*/
protected $SUT;
public function test_constructor()
{
$this->assertFalse($this->SUT->accessed());
$this->assertFalse($this->SUT->modified());
$this->assertNotEmpty($this->SUT->token());
}
// public function test_multiple_session_creation() {
// $this->expectException(\RuntimeException::class);
// $this->expectExceptionMessage('[BUG] The session is already created. Please fix your code.');
// new PhpSession;
// }
/*
*
* (aliases for ArrayObject methods)
*
*/
public function test_get()
{
$this->assertSame('bar', $this->SUT->get('foo'));
$this->assertTrue($this->SUT->accessed());
$this->assertNull($this->SUT->fubar, 'Non-existing keys returns NULL');
}
public function test_set()
{
$this->SUT->set('qux', 'zim');
$this->assertSame('zim', $this->SUT->qux);
$this->assertTrue($this->SUT->modified());
$_SESSION['qux'] = 'shmux';
$this->assertSame('shmux', $this->SUT->get('qux'));
}
public function test_add()
{
$this->SUT->add('foo', 42);
$this->assertSame('bar', $this->SUT->foo, 'With add() the value is not replaced if already set');
$this->assertFalse($this->SUT->modified());
}
public function test_remove()
{
// Ensure 2 items
$this->SUT->replace(['foo' => 'bar', 'qux' => 'zim']);
$this->assertEquals(2, $this->SUT->count(), 'Expecting 2 items in the session');
$this->SUT->remove('foo');
$this->assertEquals(1, $this->SUT->count(), 'Should be 1 item in the session');
$this->assertFalse($this->SUT->has('foo'), 'Only qux should be set');
$this->assertTrue($this->SUT->has('qux'));
$this->assertTrue($this->SUT->modified());
}
public function test_all()
{
$data = $this->SUT->all();
$this->assertArrayHasKey('foo', $data);
$this->assertArrayHasKey(Session::STAMP, $data);
$this->assertArrayHasKey(Session::AGENT, $data);
$this->assertArrayHasKey(Session::TOKEN, $data);
$this->assertTrue($this->SUT->accessed());
}
public function test_has()
{
$this->assertTrue($this->SUT->has('foo'));
$this->assertFalse($this->SUT->has('this_is_not_set'));
}
/*
*
* (mutator methods)
*
*/
public function test_clear()
{
$this->assertEquals(['qux' => 'zim', 'foo' => 'bar'], $this->SUT->toArray());
$this->SUT->clear();
$this->assertEmpty($this->SUT->toArray());
$this->assertTrue($this->SUT->modified());
}
public function test_destroy()
{
$sessionId = $this->SUT->id();
$this->assertTrue($this->SUT->destroy());
$this->assertNotEquals($sessionId, $this->SUT->id(), 'Session id is regenerated');
}
public function test_regenerate()
{
$sessionId = $this->SUT->id();
$this->assertTrue($this->SUT->regenerate());
$this->assertNotEquals($this->SUT->id(), $sessionId);
}
public function test_flash()
{
$this->assertNull($this->SUT->flash('foo'));
$this->assertTrue($this->SUT->modified());
// Set flash data
$this->SUT->flash('foo', '123');
$this->assertArrayHasKey(Session::FLASH, $_SESSION);
// Get flash data
$this->assertSame(['foo' => '123'], $this->SUT->flash('foo'), 'After flash, the key is unset');
$this->assertArrayNotHasKey(Session::FLASH, $_SESSION);
}
public function test_replace()
{
$newData = [1 => 2];
$oldData = $this->SUT->replace($newData);
$this->assertSame($newData, $this->SUT->toArray());
$this->assertArraySubset(['foo' => 'bar'], $oldData);
$this->assertTrue($this->SUT->modified());
}
/*
*
* (support methods)
*
*/
public function test_id()
{
$this->assertNotEmpty($this->SUT->id());
}
public function test_accessed()
{
$this->assertFalse($this->SUT->accessed());
$value = $this->SUT->foo;
$this->assertTrue($this->SUT->accessed());
$this->assertSame('bar', $value);
}
public function test_modified()
{
$this->assertFalse($this->SUT->modified());
$this->SUT->foo = 'zim';
$this->assertTrue($this->SUT->modified());
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed');
}
public function test_starttime()
{
$starttime = time();
$this->assertGreaterThanOrEqual($starttime, $this->SUT->starttime());
$this->assertInternalType('integer', $starttime);
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed');
}
public function test_useragent()
{
$this->assertSame('', $this->SUT->agent());
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed');
}
public function test_isEmpty()
{
$this->assertFalse($this->SUT->isEmpty());
$this->assertFalse($this->SUT->accessed(), 'This method does not flag the session accessed');
}
protected function tearDown()
{
session_write_close();
}
}