Skip to content

Commit 808a37a

Browse files
committed
Complete Handler tests, update autoload
Completed SessionHandlerTest, added SessionDBTest and set the autoloader to PSR-4 standard
1 parent cbe0172 commit 808a37a

File tree

4 files changed

+110
-17
lines changed

4 files changed

+110
-17
lines changed

src/autoload.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
/**
3-
* Load the models
4-
*
5-
* Sadly we can't use an autoloader here in the case that the end-user
6-
* is using one. Multiple autoloaders can cause conflicts
3+
* PSR-4 autoload
74
*
5+
* After registering this autoload function with require_once()
86
* Likel/Session/Handler can be called like this:
97
*
108
* $session = new Likel\Session\Handler();
@@ -17,6 +15,25 @@
1715
* @version 1.0.0
1816
*/
1917

20-
// Require the models
21-
require_once(__DIR__ . '/models/DB.php');
22-
require_once(__DIR__ . '/models/Session/Handler.php');
18+
// Require the models when called
19+
spl_autoload_register(function ($class_name) {
20+
// Change these depending on the project
21+
$project_prefix = 'Likel\\';
22+
$models_dir = __DIR__ . '/models/';
23+
24+
// Helper variables used in the autoloader
25+
$project_prefix_length = strlen($project_prefix);
26+
$relative_class = substr($class_name, $project_prefix_length);
27+
28+
// Return if the requested class does not include the prefix
29+
if (strncmp($project_prefix, $class_name, $project_prefix_length) !== 0) {
30+
return;
31+
}
32+
33+
// Replace the namespace prefix with the base directory, replace namespace
34+
// separators with directory separators in the class name and append with .php
35+
$file = $models_dir . str_replace('\\', '/', $relative_class) . '.php';
36+
if (file_exists($file)) {
37+
require_once($file);
38+
}
39+
});

src/models/Session/Handler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ private function start_session($session_name, $secure)
305305
session_name($session_name);
306306
session_start();
307307

308+
// Put it into the DB so we don't delay
309+
$this->_write(session_id(), '');
310+
308311
// Regenerate ID is recommended to reset the session every reload
309312
// Bug occurs if set to true that causes the current session to
310313
// be removed if loading pages too quickly

test/SessionDBTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* PHPUnit tests for models/DB.php
4+
*
5+
* Test errors in PHPUnit 6.4.0 make sure to upgrade to 6.4.1
6+
*
7+
* @package php-simple-sessions
8+
* @author Liam Kelly <https://github.com/likel>
9+
* @copyright 2017 Liam Kelly
10+
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
11+
* @link https://github.com/likel/php-simple-sessions
12+
* @version 1.0.0
13+
*/
14+
use PHPUnit\Framework\TestCase;
15+
16+
// Require the autoloader to load the models when required
17+
require_once(__DIR__ . '/../src/autoload.php');
18+
19+
/**
20+
* @runTestsInSeparateProcesses
21+
*/
22+
final class SessionDBTest extends TestCase
23+
{
24+
/**
25+
* No parameters supplied to constructor
26+
*/
27+
public function testConstructorNoParameters()
28+
{
29+
30+
}
31+
}

test/SessionHandlerTest.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
final class SessionHandlerTest extends TestCase
2323
{
24-
private $session;
25-
2624
/**
2725
* Destroy the session at the end of each test
2826
*/
@@ -38,7 +36,7 @@ protected function tearDown()
3836
*/
3937
public function testConstructorNoParameters()
4038
{
41-
$this->session = new \Likel\Session\Handler();
39+
$session = new \Likel\Session\Handler();
4240
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
4341
}
4442

@@ -47,7 +45,7 @@ public function testConstructorNoParameters()
4745
*/
4846
public function testConstructorNonArray()
4947
{
50-
$this->session = new \Likel\Session\Handler("a");
48+
$session = new \Likel\Session\Handler("a");
5149
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
5250
}
5351

@@ -56,7 +54,7 @@ public function testConstructorNonArray()
5654
*/
5755
public function testConstructorSessionNameSet()
5856
{
59-
$this->session = new \Likel\Session\Handler(array(
57+
$session = new \Likel\Session\Handler(array(
6058
'session_name' => "test_session"
6159
));
6260
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
@@ -68,7 +66,7 @@ public function testConstructorSessionNameSet()
6866
*/
6967
public function testConstructorSecureSet()
7068
{
71-
$this->session = new \Likel\Session\Handler(array(
69+
$session = new \Likel\Session\Handler(array(
7270
'secure' => "true"
7371
));
7472
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
@@ -79,7 +77,7 @@ public function testConstructorSecureSet()
7977
*/
8078
public function testConstructorCredentialsLocationSet()
8179
{
82-
$this->session = new \Likel\Session\Handler(array(
80+
$session = new \Likel\Session\Handler(array(
8381
'credentials_location' => __DIR__ . '/../src/ini/credentials.ini'
8482
));
8583
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
@@ -90,7 +88,7 @@ public function testConstructorCredentialsLocationSet()
9088
*/
9189
public function testConstructorNonExistantParameter()
9290
{
93-
$this->session = new \Likel\Session\Handler(array(
91+
$session = new \Likel\Session\Handler(array(
9492
'foo' => 'bar'
9593
));
9694
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
@@ -101,7 +99,7 @@ public function testConstructorNonExistantParameter()
10199
*/
102100
public function testConstructorIncorrectSecureType()
103101
{
104-
$this->session = new \Likel\Session\Handler(array(
102+
$session = new \Likel\Session\Handler(array(
105103
'secure' => "false"
106104
));
107105
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
@@ -113,9 +111,53 @@ public function testConstructorIncorrectSecureType()
113111
public function testConstructorIncorrectCredentialsLocation()
114112
{
115113
$this->expectOutputString('The credential file could not be located.');
116-
$this->session = new \Likel\Session\Handler(array(
114+
$session = new \Likel\Session\Handler(array(
117115
'credentials_location' => "path"
118116
));
119117
$this->assertEquals(session_status(), PHP_SESSION_NONE);
120118
}
119+
120+
/**
121+
* Check session is in the database
122+
*/
123+
public function testSessionInDatabase()
124+
{
125+
$session = new \Likel\Session\Handler();
126+
$db = new \Likel\DB(__DIR__ . '/../src/ini/credentials.ini');
127+
$db->query("
128+
SELECT * FROM {$db->getTableName("sessions")}
129+
WHERE id = :id
130+
");
131+
$db->bind(":id", session_id());
132+
$this->assertNotNull($db->result());
133+
}
134+
135+
/**
136+
* Test ArrayAccess implementation
137+
*/
138+
public function testArrayAccessImplementation()
139+
{
140+
$session = new \Likel\Session\Handler();
141+
142+
// offsetSet test
143+
$session["set"] = "foo";
144+
$this->assertEquals($session["set"], "foo");
145+
146+
// offsetGet tests
147+
$bar = $session["set"];
148+
$this->assertEquals($session["set"], $bar);
149+
$this->assertNull($session["not_set"]);
150+
151+
// offsetExists tests
152+
$this->assertTrue(isset($session["set"]));
153+
$this->assertFalse(isset($session["not_set"]));
154+
155+
// offsetUnset test
156+
unset($session["set"]);
157+
$this->assertNull($session["set"]);
158+
159+
// __debugInfo test
160+
$session["set"] = "foo";
161+
$this->assertEquals('Likel\Session\HandlerObject([set]=>foo)', preg_replace('/\s+/', '', print_r($session, true)));
162+
}
121163
}

0 commit comments

Comments
 (0)