|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use Smr\Exceptions\AllianceNotFound; |
|
6
|
|
|
use Smr\Exceptions\UserError; |
|
7
|
|
|
use SmrAlliance; |
|
8
|
|
|
use SmrTest\BaseIntegrationSpec; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers SmrAlliance |
|
12
|
|
|
*/ |
|
13
|
|
|
class SmrAllianceIntegrationTest extends BaseIntegrationSpec { |
|
14
|
|
|
|
|
15
|
|
|
protected function tablesToTruncate(): array { |
|
16
|
|
|
return ['alliance', 'irc_alliance_has_channel']; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp(): void { |
|
20
|
|
|
// Start each test with an empty alliance cache |
|
21
|
|
|
SmrAlliance::clearCache(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function test_getAlliance_throws_when_no_record_found(): void { |
|
25
|
|
|
$this->expectException(AllianceNotFound::class); |
|
26
|
|
|
$this->expectExceptionMessage('Invalid allianceID: 2 OR gameID: 3'); |
|
27
|
|
|
SmrAlliance::getAlliance(2, 3); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function test_getAllianceByIrcChannel(): void { |
|
31
|
|
|
// Create an Alliance and set its IRC channel |
|
32
|
|
|
$gameID = 1; |
|
33
|
|
|
$channel = '#ircrules'; |
|
34
|
|
|
$alliance1 = SmrAlliance::createAlliance($gameID, 'test'); |
|
35
|
|
|
$alliance1->setIrcChannel($channel); |
|
36
|
|
|
$alliance1->update(); |
|
37
|
|
|
|
|
38
|
|
|
// Test that we recover the original Alliance from the IRC channel |
|
39
|
|
|
$alliance2 = SmrAlliance::getAllianceByIrcChannel($channel); |
|
40
|
|
|
self::assertSame($alliance2, $alliance1); |
|
41
|
|
|
|
|
42
|
|
|
// Test that we raise an exception with the wrong IRC channel |
|
43
|
|
|
$this->expectException(AllianceNotFound::class); |
|
44
|
|
|
$this->expectExceptionMessage('Alliance IRC Channel not found'); |
|
45
|
|
|
SmrAlliance::getAllianceByIrcChannel('#notircrules'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function test_getAllianceByName(): void { |
|
49
|
|
|
// Create an Alliance with a specific name |
|
50
|
|
|
$gameID = 3; |
|
51
|
|
|
$name = 'test'; |
|
52
|
|
|
$alliance1 = SmrAlliance::createAlliance($gameID, $name); |
|
53
|
|
|
|
|
54
|
|
|
// Test that we recover the original Alliance from the name |
|
55
|
|
|
$alliance2 = SmrAlliance::getAllianceByName($name, $gameID); |
|
56
|
|
|
self::assertSame($alliance2, $alliance1); |
|
57
|
|
|
|
|
58
|
|
|
// Test that we raise an exception with the wrong Alliance name |
|
59
|
|
|
$this->expectException(AllianceNotFound::class); |
|
60
|
|
|
$this->expectExceptionMessage('Alliance name not found'); |
|
61
|
|
|
SmrAlliance::getAllianceByName('not' . $name, $gameID); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function test_createAlliance(): void { |
|
65
|
|
|
// Test arbitrary input |
|
66
|
|
|
$gameID = 42; |
|
67
|
|
|
$name = 'test'; |
|
68
|
|
|
|
|
69
|
|
|
$alliance = SmrAlliance::createAlliance($gameID, $name); |
|
70
|
|
|
|
|
71
|
|
|
self::assertSame($gameID, $alliance->getGameID()); |
|
72
|
|
|
self::assertSame($name, $alliance->getAllianceName()); |
|
73
|
|
|
self::assertSame(1, $alliance->getAllianceID()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function test_createAlliance_duplicate_name(): void { |
|
77
|
|
|
$name = 'test'; |
|
78
|
|
|
SmrAlliance::createAlliance(1, $name); |
|
79
|
|
|
$this->expectException(UserError::class); |
|
80
|
|
|
$this->expectExceptionMessage('That alliance name already exists.'); |
|
81
|
|
|
SmrAlliance::createAlliance(1, $name); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function test_createAlliance_with_NHA_name(): void { |
|
85
|
|
|
$this->expectException(UserError::class); |
|
86
|
|
|
$this->expectExceptionMessage('That alliance name is reserved.'); |
|
87
|
|
|
SmrAlliance::createAlliance(1, NHA_ALLIANCE_NAME); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function test_createAlliance_increment_allianceID(): void { |
|
91
|
|
|
SmrAlliance::createAlliance(1, 'test1'); |
|
92
|
|
|
$alliance = SmrAlliance::createAlliance(1, 'test2'); |
|
93
|
|
|
self::assertSame(2, $alliance->getAllianceID()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function test_isNHA(): void { |
|
97
|
|
|
// Create an alliance that is not the NHA |
|
98
|
|
|
$alliance = SmrAlliance::createAlliance(1, 'Vet Help Alliance', true); |
|
99
|
|
|
self::assertFalse($alliance->isNHA()); |
|
100
|
|
|
|
|
101
|
|
|
// Create an alliance that is the NHA |
|
102
|
|
|
$alliance = SmrAlliance::createAlliance(1, NHA_ALLIANCE_NAME, true); |
|
103
|
|
|
self::assertTrue($alliance->isNHA()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function test_isNone(): void { |
|
107
|
|
|
// Create an alliance that is not "none" |
|
108
|
|
|
$alliance = SmrAlliance::createAlliance(1, 'Some alliance'); |
|
109
|
|
|
self::assertFalse($alliance->isNone()); |
|
110
|
|
|
|
|
111
|
|
|
// Create an alliance that is "none" |
|
112
|
|
|
$alliance = SmrAlliance::getAlliance(0, 1); |
|
113
|
|
|
self::assertTrue($alliance->isNone()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|