1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Page; |
6
|
|
|
use Smr\Container\DiContainer; |
7
|
|
|
use Smr\Session; |
8
|
|
|
use SmrTest\BaseIntegrationSpec; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers Smr\Session |
12
|
|
|
*/ |
13
|
|
|
class SessionIntegrationTest extends BaseIntegrationSpec { |
14
|
|
|
|
15
|
|
|
private Session $session; |
16
|
|
|
|
17
|
|
|
protected function setUp() : void { |
18
|
|
|
// Start each test with a fresh container (and Smr\Session). |
19
|
|
|
// This ensures the independence of each test. |
20
|
|
|
DiContainer::initializeContainer(); |
21
|
|
|
$this->session = Session::getInstance(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function tearDown() : void { |
25
|
|
|
parent::tearDown(); |
26
|
|
|
// Clear superglobals to avoid impacting other tests |
27
|
|
|
$_REQUEST = []; |
28
|
|
|
$_COOKIE = []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_game() { |
32
|
|
|
// Sessions are initialized with no game |
33
|
|
|
self::assertFalse($this->session->hasGame()); |
34
|
|
|
self::assertSame(0, $this->session->getGameID()); |
35
|
|
|
|
36
|
|
|
// Now update the game |
37
|
|
|
$gameID = 3; |
38
|
|
|
$this->session->updateGame($gameID); |
39
|
|
|
self::assertTrue($this->session->hasGame()); |
40
|
|
|
self::assertSame($gameID, $this->session->getGameID()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_account() { |
44
|
|
|
// Sessions are initialized with no account |
45
|
|
|
self::assertFalse($this->session->hasAccount()); |
46
|
|
|
self::assertSame(0, $this->session->getAccountID()); |
47
|
|
|
|
48
|
|
|
// Now update the account |
49
|
|
|
$account = $this->createMock(\AbstractSmrAccount::class); |
50
|
|
|
$account |
51
|
|
|
->method('getAccountID') |
52
|
|
|
->willReturn(7); |
53
|
|
|
$this->session->setAccount($account); |
54
|
|
|
self::assertTrue($this->session->hasAccount()); |
55
|
|
|
self::assertSame(7, $this->session->getAccountID()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_getSN() { |
59
|
|
|
// If there is no 'sn' parameter of the $_REQUEST superglobal, |
60
|
|
|
// then we get an empty SN. |
61
|
|
|
self::assertSame('', $this->session->getSN()); |
62
|
|
|
|
63
|
|
|
// Now create a new Session with a specific 'sn' parameter set. |
64
|
|
|
$sn = 'some_sn'; |
65
|
|
|
$_REQUEST['sn'] = $sn; |
66
|
|
|
$session = DiContainer::make(Session::class); |
67
|
|
|
self::assertSame($sn, $session->getSN()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_getSessionID() { |
71
|
|
|
// The default Session ID is a random 32-length string |
72
|
|
|
self::assertSame(32, strlen($this->session->getSessionID())); |
73
|
|
|
|
74
|
|
|
// Create a Session with a specific ID |
75
|
|
|
$sessionID = md5('hello'); |
76
|
|
|
$_COOKIE['session_id'] = $sessionID; |
77
|
|
|
$session = DiContainer::make(Session::class); |
78
|
|
|
self::assertSame($sessionID, $session->getSessionID()); |
79
|
|
|
|
80
|
|
|
// If we try to use a session ID with fewer than 32 chars, |
81
|
|
|
// we get a random ID instead |
82
|
|
|
$sessionID = 'hello'; |
83
|
|
|
$_COOKIE['session_id'] = $sessionID; |
84
|
|
|
$session = DiContainer::make(Session::class); |
85
|
|
|
self::assertNotEquals($sessionID, $session->getSessionID()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function test_current_var() { |
89
|
|
|
// With an empty session, there should be no current var |
90
|
|
|
self::assertFalse($this->session->findCurrentVar()); |
91
|
|
|
|
92
|
|
|
// Add a page to the session so that we can find it later. |
93
|
|
|
// (This mimics Page::href but with better access to the SN.) |
94
|
|
|
$page = Page::create('some_page'); |
95
|
|
|
$page['CommonID'] = 'abc'; |
96
|
|
|
$page['RemainingPageLoads'] = 1; |
97
|
|
|
$sn = $this->session->addLink($page); |
98
|
|
|
$sessionID = $this->session->getSessionID(); // needed for later |
99
|
|
|
$this->session->update(); |
100
|
|
|
|
101
|
|
|
// Create a new Session, requesting the SN we just made |
102
|
|
|
$_REQUEST['sn'] = $sn; |
103
|
|
|
$_COOKIE['session_id'] = $sessionID; |
104
|
|
|
$session = DiContainer::make(Session::class); |
105
|
|
|
|
106
|
|
|
// Now we should be able to find this sn in the var |
107
|
|
|
self::assertTrue($session->findCurrentVar()); |
108
|
|
|
|
109
|
|
|
// The current var should now be accessible |
110
|
|
|
$var = $session->getCurrentVar(); |
111
|
|
|
self::assertSame('some_page', $var['url']); |
112
|
|
|
|
113
|
|
|
// The CommonID metadata should be stripped |
114
|
|
|
self::assertFalse(isset($var['CommonID'])); |
115
|
|
|
// The RemainingPageLoads should still be 1 because we effectively |
116
|
|
|
// reloaded the page by creating a new Session. |
117
|
|
|
self::assertSame(1, $var['RemainingPageLoads']); |
118
|
|
|
|
119
|
|
|
// We can now change the current var |
120
|
|
|
$page2 = Page::create('another_page'); |
121
|
|
|
$session->setCurrentVar($page2); |
122
|
|
|
// And $var should be updated automatically |
123
|
|
|
self::assertSame('another_page', $var['url']); |
124
|
|
|
|
125
|
|
|
// If we destroy the Session, then the current var should no longer |
126
|
|
|
// be accessible to a new Session. |
127
|
|
|
$session->destroy(); |
128
|
|
|
$session = DiContainer::make(Session::class); |
129
|
|
|
self::assertFalse($session->findCurrentVar()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|