-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSessionHandlerTest.php
More file actions
163 lines (146 loc) · 4.55 KB
/
SessionHandlerTest.php
File metadata and controls
163 lines (146 loc) · 4.55 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
<?php
/**
* PHPUnit tests for models/Session/Handler.php
*
* Test errors in PHPUnit 6.4.0 make sure to upgrade to 6.4.1
*
* @package php-simple-sessions
* @author Liam Kelly <https://github.com/likel>
* @copyright 2017 Liam Kelly
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
* @link https://github.com/likel/php-simple-sessions
* @version 1.0.0
*/
use PHPUnit\Framework\TestCase;
// Require the autoloader to load the models when required
require_once(__DIR__ . '/../src/autoload.php');
/**
* @runTestsInSeparateProcesses
*/
final class SessionHandlerTest extends TestCase
{
/**
* Destroy the session at the end of each test
*/
protected function tearDown()
{
if (session_status() == PHP_SESSION_ACTIVE) {
session_destroy();
}
}
/**
* No parameters supplied to constructor
*/
public function testConstructorNoParameters()
{
$session = new \Likel\Session\Handler();
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Non-array parameter supplied to constructor
*/
public function testConstructorNonArray()
{
$session = new \Likel\Session\Handler("a");
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Supply a correct session_name parameter
*/
public function testConstructorSessionNameSet()
{
$session = new \Likel\Session\Handler(array(
'session_name' => "test_session"
));
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
$this->assertEquals(session_name(), "test_session");
}
/**
* Supply a correct secure parameter
*/
public function testConstructorSecureSet()
{
$session = new \Likel\Session\Handler(array(
'secure' => "true"
));
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Supply a correct credentials_location parameter
*/
public function testConstructorCredentialsLocationSet()
{
$session = new \Likel\Session\Handler(array(
'credentials_location' => __DIR__ . '/../src/ini/credentials.ini'
));
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Supply a parameter that doesn't exist
*/
public function testConstructorNonExistantParameter()
{
$session = new \Likel\Session\Handler(array(
'foo' => 'bar'
));
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Incorrect secure parameter type supplied
*/
public function testConstructorIncorrectSecureType()
{
$session = new \Likel\Session\Handler(array(
'secure' => "false"
));
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
}
/**
* Incorrect credentials_location file path supplied
*/
public function testConstructorIncorrectCredentialsLocation()
{
$this->expectOutputString('The credential file could not be located.');
$session = new \Likel\Session\Handler(array(
'credentials_location' => "path"
));
$this->assertEquals(session_status(), PHP_SESSION_NONE);
}
/**
* Check session is in the database
*/
public function testSessionInDatabase()
{
$session = new \Likel\Session\Handler();
$db = new \Likel\DB(__DIR__ . '/../src/ini/credentials.ini');
$db->query("
SELECT * FROM {$db->getTableName("sessions")}
WHERE id = :id
");
$db->bind(":id", session_id());
$this->assertNotNull($db->result());
}
/**
* Test ArrayAccess implementation
*/
public function testArrayAccessImplementation()
{
$session = new \Likel\Session\Handler();
// offsetSet test
$session["set"] = "foo";
$this->assertEquals($session["set"], "foo");
// offsetGet tests
$bar = $session["set"];
$this->assertEquals($session["set"], $bar);
$this->assertNull($session["not_set"]);
// offsetExists tests
$this->assertTrue(isset($session["set"]));
$this->assertFalse(isset($session["not_set"]));
// offsetUnset test
unset($session["set"]);
$this->assertNull($session["set"]);
// __debugInfo test
$session["set"] = "foo";
$this->assertEquals('Likel\Session\HandlerObject([set]=>foo)', preg_replace('/\s+/', '', print_r($session, true)));
}
}