Skip to content

Commit a4029d1

Browse files
author
Greg Bowler
committed
Initialise session within GlobalSessionContainer
1 parent 8b5737d commit a4029d1

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/Authenticator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@ public function __construct(
1616
if(is_null($session)) {
1717
$session = new GlobalSessionContainer();
1818
}
19+
20+
if(!$session->contains(GlobalSessionContainer::SESSION_KEY)) {
21+
$session->set(
22+
GlobalSessionContainer::SESSION_KEY,
23+
new SessionData()
24+
);
25+
}
1926
}
2027
}

src/GlobalSessionContainer.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,12 @@
44
use Gt\Session\SessionArrayWrapper;
55

66
class GlobalSessionContainer extends SessionArrayWrapper {
7-
const DEFAULT_SESSION_KEY = "AUTHWAVE_SESSION";
7+
const SESSION_KEY = "AUTHWAVE_SESSION_DATA";
88

9-
public function __construct(
10-
string $baseSessionKey = self::DEFAULT_SESSION_KEY
11-
) {
12-
if(session_status() !== PHP_SESSION_ACTIVE) {
13-
session_start();
9+
public function __construct() {
10+
if(!isset($_SESSION)) {
11+
throw new SessionNotStartedException();
1412
}
15-
16-
if(!isset($_SESSION[$baseSessionKey])) {
17-
$_SESSION[$baseSessionKey] = new SessionData();
18-
}
19-
20-
// TODO: Should this implement SessionContainer interface and define
21-
// the getters and setters to $_SESSION itself, rather than being an
22-
// instance of SessionArrayWrapper?
23-
24-
parent::__construct($_SESSION[$baseSessionKey]);
13+
parent::__construct($_SESSION);
2514
}
2615
}

src/SessionNotStartedException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class SessionNotStartedException extends AuthwaveException {}

test/phpunit/AuthenticatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,30 @@
22
namespace Authwave\Test;
33

44
use Authwave\Authenticator;
5+
use Authwave\GlobalSessionContainer;
6+
use Authwave\SessionNotStartedException;
57
use PHPUnit\Framework\TestCase;
68

79
class AuthenticatorTest extends TestCase {
10+
public function testConstructWithDefaultSessionNotStarted() {
11+
self::expectException(SessionNotStartedException::class);
12+
new Authenticator(
13+
"test-key",
14+
"test-secret",
15+
"/"
16+
);
17+
}
18+
819
public function testConstructWithDefaultSession() {
20+
$_SESSION = [];
921
$sut = new Authenticator(
1022
"test-key",
1123
"test-secret",
1224
"/"
1325
);
26+
self::assertArrayHasKey(
27+
GlobalSessionContainer::SESSION_KEY,
28+
$_SESSION
29+
);
1430
}
1531
}

0 commit comments

Comments
 (0)