|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use SmrSession; |
|
6
|
|
|
use SmrTest\BaseIntegrationSpec; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers SmrSession |
|
10
|
|
|
*/ |
|
11
|
|
|
class SmrSessionIntegrationTest extends BaseIntegrationSpec { |
|
12
|
|
|
|
|
13
|
|
|
private SmrSession $session; |
|
14
|
|
|
|
|
15
|
|
|
protected function setUp() : void { |
|
16
|
|
|
// Start each test with a fresh container (and SmrSession). |
|
17
|
|
|
// This ensures the independence of each test. |
|
18
|
|
|
\Smr\Container\DiContainer::initializeContainer(); |
|
19
|
|
|
$this->session = SmrSession::getInstance(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Test that the updateTime function works properly when NPC_SCRIPT is set. |
|
24
|
|
|
* We run in a separate process so that the constant doesn't propagate into |
|
25
|
|
|
* other tests. |
|
26
|
|
|
* @runInSeparateProcess |
|
27
|
|
|
*/ |
|
28
|
|
|
public function test_updateTime_cli() { |
|
29
|
|
|
// Set the NPC_SCRIPT variable as if this were a CLI program |
|
30
|
|
|
define('NPC_SCRIPT', true); |
|
31
|
|
|
|
|
32
|
|
|
$time = $this->session->time(); |
|
33
|
|
|
$microtime = $this->session->microtime(); |
|
34
|
|
|
|
|
35
|
|
|
// Sleep 1 second to ensure that the integer time has incremented |
|
36
|
|
|
sleep(1); |
|
37
|
|
|
$this->session->updateTime(); |
|
38
|
|
|
|
|
39
|
|
|
// Make sure the times have changed |
|
40
|
|
|
self::assertNotEquals($time, $this->session->time()); |
|
41
|
|
|
self::assertNotEquals($microtime, $this->session->microtime()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* updateTime should throw if called without NPC_SCRIPT defined. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function test_updateTime() { |
|
48
|
|
|
self::expectException(\Exception::class); |
|
|
|
|
|
|
49
|
|
|
self::expectExceptionMessage('Only call this function from CLI programs'); |
|
|
|
|
|
|
50
|
|
|
$this->session->updateTime(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* We can't check the time/microtime values, but we can ensure that |
|
55
|
|
|
* the rounded values are identical. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function test_time_microtime_equality() { |
|
58
|
|
|
self::assertEquals($this->session->time(), floor($this->session->microtime())); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function test_game() { |
|
62
|
|
|
// Sessions are initialized with no game |
|
63
|
|
|
self::assertFalse($this->session->hasGame()); |
|
64
|
|
|
self::assertSame(0, $this->session->getGameID()); |
|
65
|
|
|
|
|
66
|
|
|
// Now update the game |
|
67
|
|
|
$gameID = 3; |
|
68
|
|
|
$this->session->updateGame($gameID); |
|
69
|
|
|
self::assertTrue($this->session->hasGame()); |
|
70
|
|
|
self::assertSame($gameID, $this->session->getGameID()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function test_account() { |
|
74
|
|
|
// Sessions are initialized with no account |
|
75
|
|
|
self::assertFalse($this->session->hasAccount()); |
|
76
|
|
|
self::assertSame(0, $this->session->getAccountID()); |
|
77
|
|
|
|
|
78
|
|
|
// Now update the account |
|
79
|
|
|
$account = $this->createMock(\AbstractSmrAccount::class); |
|
80
|
|
|
$account |
|
81
|
|
|
->method('getAccountID') |
|
82
|
|
|
->willReturn(7); |
|
83
|
|
|
$this->session->setAccount($account); |
|
84
|
|
|
self::assertTrue($this->session->hasAccount()); |
|
85
|
|
|
self::assertSame(7, $this->session->getAccountID()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|