Skip to content

Commit fc46d88

Browse files
committed
Begin PHPUnit tests
Began creating PHPUnit tests, are we unit testing or are we integration testing?
1 parent d049877 commit fc46d88

File tree

6 files changed

+147
-35
lines changed

6 files changed

+147
-35
lines changed

src/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @package php-simple-sessions
1313
* @author Liam Kelly <https://github.com/likel>
1414
* @copyright 2017 Liam Kelly
15-
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
15+
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1616
* @link https://github.com/likel/php-simple-sessions
1717
* @version 1.0.0
1818
*/

src/example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @package php-simple-sessions
66
* @author Liam Kelly <https://github.com/likel>
77
* @copyright 2017 Liam Kelly
8-
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
8+
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
99
* @link https://github.com/likel/php-simple-sessions
1010
* @version 1.0.0
1111
*/

src/ini/credentials.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
; @package php-simple-sessions
1414
; @author Liam Kelly <https://github.com/likel>
1515
; @copyright 2017 Liam Kelly
16-
; @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
16+
; @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1717
; @link https://github.com/likel/php-simple-sessions
1818
; @version 1.0.0
1919

src/models/DB.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @package php-simple-sessions
88
* @author Liam Kelly <https://github.com/likel>
99
* @copyright 2017 Liam Kelly
10-
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
10+
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1111
* @link https://github.com/likel/php-simple-sessions
1212
* @version 1.0.0
1313
*/
@@ -28,11 +28,9 @@ class DB
2828
public function __construct($credentials_location)
2929
{
3030
try {
31-
$db_credentials = parse_ini_file($credentials_location, true);
32-
$this->database_handler = $this->loadDatabase($db_credentials["likel_db"]);
33-
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
31+
$this->database_handler = $this->loadDatabase($credentials_location);
3432
} catch (\Exception $ex) {
35-
throw $ex;
33+
echo $ex->getMessage();
3634
}
3735
}
3836

@@ -44,27 +42,35 @@ public function __construct($credentials_location)
4442
* @throws \Exception If credentials empty or not found
4543
* @throws \PDOException If PDO connection is unsuccessful
4644
*/
47-
private function loadDatabase($credentials)
45+
private function loadDatabase($credentials_location)
4846
{
49-
if(!empty($credentials)){
50-
try {
51-
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
47+
if(file_exists($credentials_location)) {
48+
$db_credentials = parse_ini_file($credentials_location, true);
49+
$credentials = $db_credentials["likel_db"];
5250

53-
$options = array(
54-
\PDO::ATTR_PERSISTENT => true,
55-
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
56-
);
51+
if(!empty($credentials)){
52+
try {
53+
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
5754

58-
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
55+
$options = array(
56+
\PDO::ATTR_PERSISTENT => true,
57+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
58+
);
5959

60-
return $pdo_object;
60+
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
6161

62-
} catch(\PDOException $e) {
63-
throw new \Exception($e->getMessage());
64-
}
62+
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
63+
64+
return $pdo_object;
6565

66+
} catch(\PDOException $e) {
67+
throw new \Exception($e->getMessage());
68+
}
69+
} else {
70+
throw new \Exception('The likel_db parameter in the credentials file cannot be found.');
71+
}
6672
} else {
67-
throw new \Exception('The credential file could not be located or is empty.');
73+
throw new \Exception('The credential file could not be located.');
6874
}
6975
}
7076

@@ -222,4 +228,9 @@ public function dumpStatement()
222228
{
223229
$this->statement->debugDumpParams();
224230
}
231+
232+
public function databaseInitialised()
233+
{
234+
return !empty($this->database_handler);
235+
}
225236
}

src/models/Session/Handler.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @package php-simple-sessions
1212
* @author Liam Kelly <https://github.com/likel>
1313
* @copyright 2017 Liam Kelly
14-
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
14+
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1515
* @link https://github.com/likel/php-simple-sessions
1616
* @version 1.0.0
1717
*/
@@ -32,24 +32,30 @@ class Handler implements \ArrayAccess
3232
*/
3333
function __construct($parameters = array())
3434
{
35+
if(!is_array($parameters)) {
36+
$parameters = array();
37+
}
38+
3539
// Defaults
3640
$parameters["session_name"] = empty($parameters["session_name"]) ? "likel_session" : $parameters["session_name"];
37-
$parameters["secure"] = empty($parameters["secure"]) ? false : $parameters["secure"];
41+
$parameters["secure"] = empty($parameters["secure"]) ? false : is_bool($parameters["secure"] === true) ? true : false;
3842
$parameters["credentials_location"] = empty($parameters["credentials_location"]) ? __DIR__ . '/../../ini/credentials.ini' : $parameters["credentials_location"];
3943

4044
// Setup the database class variable
4145
$this->db = new \Likel\DB($parameters["credentials_location"]);
4246

43-
// Attempt to get the secret_hash from the credentials file
44-
try {
45-
$session_credentials = parse_ini_file($parameters["credentials_location"], true);
46-
$this->secret_hash = $this->loadSecretHash($session_credentials["likel_session"]);
47-
} catch (\Exception $ex) {
48-
throw $ex;
49-
}
47+
if($this->db->databaseInitialised()) {
48+
// Attempt to get the secret_hash from the credentials file
49+
try {
50+
$session_credentials = parse_ini_file($parameters["credentials_location"], true);
51+
$this->secret_hash = $this->loadSecretHash($session_credentials["likel_session"]);
5052

51-
// Start session
52-
$this->start_session($parameters["session_name"], $parameters["secure"]);
53+
// Start session
54+
$this->start_session($parameters["session_name"], $parameters["secure"]);
55+
} catch (\Exception $ex) {
56+
echo $ex->getMessage();
57+
}
58+
}
5359
}
5460

5561
/**

test/SessionHandlerTest.php

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/**
33
* PHPUnit tests for models/Session/Handler.php
44
*
5+
* Test errors in PHPUnit 6.4.0 make sure to upgrade to 6.4.1
6+
*
57
* @package php-simple-sessions
68
* @author Liam Kelly <https://github.com/likel>
79
* @copyright 2017 Liam Kelly
@@ -14,13 +16,106 @@
1416
// Require the autoloader to load the models when required
1517
require_once(__DIR__ . '/../src/autoload.php');
1618

19+
/**
20+
* @runTestsInSeparateProcesses
21+
*/
1722
final class SessionHandlerTest extends TestCase
1823
{
24+
private $session;
25+
26+
/**
27+
* Destroy the session at the end of each test
28+
*/
29+
protected function tearDown()
30+
{
31+
if (session_status() == PHP_SESSION_ACTIVE) {
32+
session_destroy();
33+
}
34+
}
35+
36+
/**
37+
* No parameters supplied to constructor
38+
*/
39+
public function testConstructorNoParameters()
40+
{
41+
$this->session = new \Likel\Session\Handler();
42+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
43+
}
44+
1945
/**
20-
* Test for the constructor outcomes
46+
* Non-array parameter supplied to constructor
2147
*/
22-
public function testConstruct()
48+
public function testConstructorNonArray()
2349
{
50+
$this->session = new \Likel\Session\Handler("a");
51+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
52+
}
2453

54+
/**
55+
* Supply a correct session_name parameter
56+
*/
57+
public function testConstructorSessionNameSet()
58+
{
59+
$this->session = new \Likel\Session\Handler(array(
60+
'session_name' => "test_session"
61+
));
62+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
63+
$this->assertEquals(session_name(), "test_session");
64+
}
65+
66+
/**
67+
* Supply a correct secure parameter
68+
*/
69+
public function testConstructorSecureSet()
70+
{
71+
$this->session = new \Likel\Session\Handler(array(
72+
'secure' => "true"
73+
));
74+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
75+
}
76+
77+
/**
78+
* Supply a correct credentials_location parameter
79+
*/
80+
public function testConstructorCredentialsLocationSet()
81+
{
82+
$this->session = new \Likel\Session\Handler(array(
83+
'credentials_location' => __DIR__ . '/../src/ini/credentials.ini'
84+
));
85+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
86+
}
87+
88+
/**
89+
* Supply a parameter that doesn't exist
90+
*/
91+
public function testConstructorNonExistantParameter()
92+
{
93+
$this->session = new \Likel\Session\Handler(array(
94+
'foo' => 'bar'
95+
));
96+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
97+
}
98+
99+
/**
100+
* Incorrect secure parameter type supplied
101+
*/
102+
public function testConstructorIncorrectSecureType()
103+
{
104+
$this->session = new \Likel\Session\Handler(array(
105+
'secure' => "false"
106+
));
107+
$this->assertEquals(session_status(), PHP_SESSION_ACTIVE);
108+
}
109+
110+
/**
111+
* Incorrect credentials_location file path supplied
112+
*/
113+
public function testConstructorIncorrectCredentialsLocation()
114+
{
115+
$this->expectOutputString('The credential file could not be located.');
116+
$this->session = new \Likel\Session\Handler(array(
117+
'credentials_location' => "path"
118+
));
119+
$this->assertEquals(session_status(), PHP_SESSION_NONE);
25120
}
26121
}

0 commit comments

Comments
 (0)