Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Pull Request — main (#1487)
by Dan
06:01
created

tearDownAfterClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace SmrTest\lib\DefaultGame;
4
5
use AbstractSmrPlayer;
6
use Smr\Exceptions\PlayerNotFound;
7
use Smr\Exceptions\UserError;
8
use SmrGame;
9
use SmrTest\BaseIntegrationSpec;
10
11
/**
12
 * @covers AbstractSmrPlayer
13
 */
14
class AbstractSmrPlayerIntegrationTest extends BaseIntegrationSpec {
15
16
	private static int $gameID = 42;
17
18
	protected function tablesToTruncate(): array {
19
		return ['player'];
20
	}
21
22
	protected function tearDown(): void {
23
		AbstractSmrPlayer::clearCache();
24
	}
25
26
	public static function setUpBeforeClass(): void {
27
		// Make objects that must be accessed statically (can't be mocked)
28
		SmrGame::createGame(self::$gameID)->setGameTypeID(SmrGame::GAME_TYPE_DEFAULT);
29
	}
30
31
	public static function tearDownAfterClass(): void {
32
		SmrGame::clearCache();
33
	}
34
35
	public function test_createPlayer(): void {
36
		// Test arbitrary input
37
		$accountID = 2;
38
		$name = 'test';
39
		$raceID = RACE_HUMAN;
40
		$isNewbie = true;
41
		$isNpc = false;
42
43
		$player = AbstractSmrPlayer::createPlayer($accountID, self::$gameID, $name, $raceID, $isNewbie, $isNpc);
44
45
		$this->assertSame($accountID, $player->getAccountID());
46
		$this->assertSame(self::$gameID, $player->getGameID());
47
		$this->assertSame($name, $player->getPlayerName());
48
		$this->assertSame($raceID, $player->getRaceID());
49
		$this->assertSame($isNewbie, $player->hasNewbieStatus());
50
		$this->assertSame($isNpc, $player->isNPC());
51
		$this->assertSame(1, $player->getSectorID());
52
		$this->assertSame(1, $player->getPlayerID());
53
	}
54
55
	public function test_createPlayer_duplicate_name(): void {
56
		$name = 'test';
57
		AbstractSmrPlayer::createPlayer(1, self::$gameID, $name, RACE_HUMAN, false);
58
		$this->expectException(UserError::class);
59
		$this->expectExceptionMessage('That player name already exists.');
60
		AbstractSmrPlayer::createPlayer(2, self::$gameID, $name, RACE_HUMAN, false);
61
	}
62
63
	public function test_createPlayer_increment_playerid(): void {
64
		AbstractSmrPlayer::createPlayer(1, self::$gameID, 'test1', RACE_HUMAN, false);
65
		$player = AbstractSmrPlayer::createPlayer(2, self::$gameID, 'test2', RACE_HUMAN, false);
66
		$this->assertSame(2, $player->getPlayerID());
67
	}
68
69
	public function test_getPlayer_returns_created_player(): void {
70
		// Given a player that is created
71
		$player1 = AbstractSmrPlayer::createPlayer(1, self::$gameID, 'test1', RACE_HUMAN, false);
72
		// When we get the same player
73
		$player2 = AbstractSmrPlayer::getPlayer(1, self::$gameID);
74
		// Then they should be the same object
75
		self::assertSame($player1, $player2);
76
77
		// When we get the same player forcing a re-query from the database
78
		$player3 = AbstractSmrPlayer::getPlayer(1, self::$gameID, true);
79
		// Then they are not the same, but they are equal
80
		self::assertNotSame($player1, $player3);
81
		self::assertTrue($player1->equals($player3));
82
	}
83
84
	public function test_getPlayer_throws_when_no_record_found(): void {
85
		$this->expectException(PlayerNotFound::class);
86
		$this->expectExceptionMessage('Invalid accountID: 123 OR gameID: 321');
87
		AbstractSmrPlayer::getPlayer(123, 321);
88
	}
89
90
	public function test_changePlayerName_throws_when_name_unchanged(): void {
91
		// Try changing name to the same name
92
		$name = 'test';
93
		$player = AbstractSmrPlayer::createPlayer(1, self::$gameID, $name, RACE_HUMAN, false);
94
		$this->expectException(UserError::class);
95
		$this->expectExceptionMessage('Your player already has that name!');
96
		$player->changePlayerName($name);
97
	}
98
99
	public function test_changePlayerName_throws_when_name_is_in_use(): void {
100
		// Try changing name to a name that is already taken
101
		$name1 = 'test1';
102
		AbstractSmrPlayer::createPlayer(1, self::$gameID, $name1, RACE_HUMAN, false);
103
		$player2 = AbstractSmrPlayer::createPlayer(2, self::$gameID, 'test2', RACE_HUMAN, false);
104
		$this->expectException(UserError::class);
105
		$this->expectExceptionMessage('That name is already being used in this game!');
106
		$player2->changePlayerName($name1);
107
	}
108
109
	public function test_changePlayerName_allows_case_change(): void {
110
		// Try changing name from 'test' to 'TEST'
111
		$name = 'test';
112
		$player = AbstractSmrPlayer::createPlayer(1, self::$gameID, $name, RACE_HUMAN, false);
113
		$newName = strtoupper($name);
114
		self::assertNotEquals($name, $newName); // sanity check
115
		$player->changePlayerName($newName);
116
		self::assertSame($newName, $player->getPlayerName());
117
	}
118
119
	public function test_changePlayerName(): void {
120
		// Try changing name from 'test' to 'Wall-E' (as an admin)
121
		$player = AbstractSmrPlayer::createPlayer(1, self::$gameID, 'test', RACE_HUMAN, false);
122
		$player->changePlayerName('Wall-E');
123
		self::assertSame('Wall-E', $player->getPlayerName());
124
		// Make sure we have NOT used the name change token
125
		self::assertFalse($player->isNameChanged());
126
	}
127
128
	public function test_changePlayerNameByPlayer(): void {
129
		// Try changing name from 'test' to 'Wall-E' (as a player)
130
		$player = AbstractSmrPlayer::createPlayer(1, self::$gameID, 'test', RACE_HUMAN, false);
131
		$player->changePlayerNameByPlayer('Wall-E');
132
		self::assertSame('Wall-E', $player->getPlayerName());
133
		// Make sure we *have* used the name change token
134
		self::assertTrue($player->isNameChanged());
135
	}
136
137
	/**
138
	 * @dataProvider dataProvider_alignment
139
	 */
140
	public function test_alignment(int $alignment, bool $isGood, bool $isEvil, bool $isNeutral): void {
141
		// Create a player with a specific alignment
142
		$player = AbstractSmrPlayer::createPlayer(1, self::$gameID, 'test', RACE_HUMAN, false);
143
		$player->setAlignment($alignment);
144
145
		// Test the alignment querying methods
146
		self::assertSame($alignment, $player->getAlignment());
147
		self::assertSame($isGood, $player->hasGoodAlignment());
148
		self::assertSame($isEvil, $player->hasEvilAlignment());
149
		self::assertSame($isNeutral, $player->hasNeutralAlignment());
150
	}
151
152
	/**
153
	 * @return array<array{int, bool, bool, bool}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{int, bool, bool, bool}> at position 4 could not be parsed: Expected ':' at position 4, but found 'int'.
Loading history...
154
	 */
155
	public function dataProvider_alignment(): array {
156
		// Test at, above, and below alignment thresholds
157
		return [
158
			[0, false, false, true],
159
			[ALIGNMENT_GOOD, true, false, false],
160
			[ALIGNMENT_GOOD + 1, true, false, false],
161
			[ALIGNMENT_GOOD - 1, false, false, true],
162
			[ALIGNMENT_EVIL, false, true, false],
163
			[ALIGNMENT_EVIL + 1, false, false, true],
164
			[ALIGNMENT_EVIL - 1, false, true, false],
165
		];
166
	}
167
168
}
169