1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use SmrPlanet; |
6
|
|
|
use SmrTest\BaseIntegrationSpec; |
7
|
|
|
|
8
|
|
|
require_once(LIB . 'Default/smr.inc.php'); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SmrPlanetIntegrationTest |
12
|
|
|
* @covers SmrPlanet |
13
|
|
|
*/ |
14
|
|
|
class SmrPlanetIntegrationTest extends BaseIntegrationSpec { |
15
|
|
|
|
16
|
|
|
protected function tearDown() : void { |
17
|
|
|
SmrPlanet::clearCache(); |
18
|
|
|
parent::tearDown(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function test_createPlanet() : void { |
22
|
|
|
// Test arbitrary input |
23
|
|
|
$sectorID = 2; |
24
|
|
|
$gameID = 42; |
25
|
|
|
$typeID = 3; |
26
|
|
|
$inhabitableTime = 5; |
27
|
|
|
|
28
|
|
|
$planet = SmrPlanet::createPlanet($gameID, $sectorID, $typeID, $inhabitableTime); |
29
|
|
|
$this->assertTrue($planet->exists()); |
30
|
|
|
|
31
|
|
|
// Check properties set explicitly |
32
|
|
|
$this->assertSame($gameID, $planet->getGameID()); |
33
|
|
|
$this->assertSame($sectorID, $planet->getSectorID()); |
34
|
|
|
$this->assertSame($typeID, $planet->getTypeID()); |
35
|
|
|
$this->assertSame($inhabitableTime, $planet->getInhabitableTime()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function test_createPlanet_already_exists() : void { |
39
|
|
|
SmrPlanet::createPlanet(1, 1, 1, 1); |
40
|
|
|
$this->expectException(\Exception::class); |
41
|
|
|
$this->expectExceptionMessage('Planet already exists'); |
42
|
|
|
SmrPlanet::createPlanet(1, 1, 1, 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_removePlanet() : void { |
46
|
|
|
// Check that planet exists |
47
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
48
|
|
|
$this->assertTrue($planet->exists()); |
49
|
|
|
|
50
|
|
|
SmrPlanet::removePlanet(1, 1); |
51
|
|
|
$planet = SmrPlanet::getPlanet(1, 1, true); |
52
|
|
|
$this->assertFalse($planet->exists()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_name() : void { |
56
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
57
|
|
|
// Check default name |
58
|
|
|
$this->assertSame('Unknown', $planet->getDisplayName()); |
59
|
|
|
|
60
|
|
|
// Set a new name (include non-HTML-safe character) |
61
|
|
|
$planet->setName('Test&'); |
62
|
|
|
$this->assertSame('Test&', $planet->getDisplayName()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function test_owner() : void { |
66
|
|
|
// Check default owner |
67
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
68
|
|
|
$this->assertFalse($planet->hasOwner()); |
69
|
|
|
$this->assertSame(0, $planet->getOwnerID()); |
70
|
|
|
|
71
|
|
|
// Set a new owner |
72
|
|
|
$ownerID = 3; |
73
|
|
|
$planet->setOwnerID($ownerID); |
74
|
|
|
$this->assertTrue($planet->hasOwner()); |
75
|
|
|
$this->assertSame($ownerID, $planet->getOwnerID()); |
76
|
|
|
|
77
|
|
|
// Remove the owner again |
78
|
|
|
$planet->removeOwner(); |
79
|
|
|
$this->assertFalse($planet->hasOwner()); |
80
|
|
|
$this->assertSame(0, $planet->getOwnerID()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_password() : void { |
84
|
|
|
// Check default password |
85
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
86
|
|
|
$this->assertSame('', $planet->getPassword()); |
87
|
|
|
|
88
|
|
|
// Set a new password |
89
|
|
|
$password = 'test'; |
90
|
|
|
$planet->setPassword($password); |
91
|
|
|
$this->assertSame($password, $planet->getPassword()); |
92
|
|
|
|
93
|
|
|
// Remove the password again |
94
|
|
|
$planet->removePassword(); |
95
|
|
|
$this->assertSame('', $planet->getPassword()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_credits() : void { |
99
|
|
|
// Check default credits |
100
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
101
|
|
|
$this->assertSame(0, $planet->getCredits()); |
102
|
|
|
|
103
|
|
|
// Check increase/decrease credits |
104
|
|
|
$planet->increaseCredits(100); |
105
|
|
|
$this->assertSame(100, $planet->getCredits()); |
106
|
|
|
$planet->increaseCredits(50); |
107
|
|
|
$this->assertSame(150, $planet->getCredits()); |
108
|
|
|
$planet->decreaseCredits(50); |
109
|
|
|
$this->assertSame(100, $planet->getCredits()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function test_bonds() : void { |
113
|
|
|
// Check default bond |
114
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
115
|
|
|
$this->assertSame(0, $planet->getBonds()); |
116
|
|
|
|
117
|
|
|
// Check increase/decrease bonds |
118
|
|
|
$planet->increaseBonds(100); |
119
|
|
|
$this->assertSame(100, $planet->getBonds()); |
120
|
|
|
$planet->increaseBonds(50); |
121
|
|
|
$this->assertSame(150, $planet->getBonds()); |
122
|
|
|
$planet->decreaseBonds(50); |
123
|
|
|
$this->assertSame(100, $planet->getBonds()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function test_bond_maturity() : void { |
127
|
|
|
// Check default maturity |
128
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
129
|
|
|
$this->assertSame(0, $planet->getMaturity()); |
130
|
|
|
|
131
|
|
|
// Set a new bond maturity |
132
|
|
|
$maturity = time(); |
133
|
|
|
$planet->setMaturity($maturity); |
134
|
|
|
$this->assertSame($maturity, $planet->getMaturity()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function test_stockpile() : void { |
138
|
|
|
// Check default stockpile |
139
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
140
|
|
|
$this->assertFalse($planet->hasStockpile()); |
141
|
|
|
$this->assertSame([], $planet->getStockpile()); |
142
|
|
|
foreach (array_keys(\Globals::getGoods()) as $goodID) { |
143
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
144
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Setting 0 still counts as empty |
148
|
|
|
$planet->setStockpile(GOODS_ORE, 0); |
149
|
|
|
$this->assertFalse($planet->hasStockpile()); |
150
|
|
|
$this->assertFalse($planet->hasStockpile(GOODS_ORE)); |
151
|
|
|
|
152
|
|
|
// Check increase stockpile |
153
|
|
|
$planet->increaseStockpile(GOODS_ORE, 50); |
154
|
|
|
$this->assertTrue($planet->hasStockpile()); |
155
|
|
|
$this->assertSame([GOODS_ORE => 50], $planet->getStockpile()); |
156
|
|
|
foreach (array_keys(\Globals::getGoods()) as $goodID) { |
157
|
|
|
if ($goodID === GOODS_ORE) { |
158
|
|
|
$this->assertTrue($planet->hasStockpile($goodID)); |
159
|
|
|
$this->assertSame(50, $planet->getStockpile($goodID)); |
160
|
|
|
} else { |
161
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
162
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Check decrease stockpile |
167
|
|
|
$planet->decreaseStockpile(GOODS_ORE, 10); |
168
|
|
|
$this->assertTrue($planet->hasStockpile()); |
169
|
|
|
$this->assertSame([GOODS_ORE => 40], $planet->getStockpile()); |
170
|
|
|
foreach (array_keys(\Globals::getGoods()) as $goodID) { |
171
|
|
|
if ($goodID === GOODS_ORE) { |
172
|
|
|
$this->assertTrue($planet->hasStockpile($goodID)); |
173
|
|
|
$this->assertSame(40, $planet->getStockpile($goodID)); |
174
|
|
|
} else { |
175
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
176
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Check remaining stockpile (ore: 600 - 40) |
181
|
|
|
$this->assertSame(560, $planet->getRemainingStockpile(GOODS_ORE)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function test_setStockpile_throws_when_negative() : void { |
185
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
186
|
|
|
$this->expectException(\Exception::class); |
187
|
|
|
$this->expectExceptionMessage('Trying to set negative stockpile'); |
188
|
|
|
$planet->setStockpile(GOODS_ORE, -20); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function test_setBuilding_throws_when_negative() : void { |
192
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
193
|
|
|
$this->expectException(\Exception::class); |
194
|
|
|
$this->expectExceptionMessage('Cannot set negative number of buildings'); |
195
|
|
|
$planet->setBuilding(PLANET_HANGAR, -1); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function test_destroyBuilding_throws_when_invalid() : void { |
199
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
200
|
|
|
$this->expectException(\Exception::class); |
201
|
|
|
$this->expectExceptionMessage('Cannot set negative number of buildings'); |
202
|
|
|
$planet->destroyBuilding(PLANET_TURRET, 1); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function test_checkForDowngrade() : void { |
206
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
207
|
|
|
|
208
|
|
|
// If we don't do enough damage, we should never downgrade |
209
|
|
|
$this->assertSame([], $planet->checkForDowngrade(0)); |
210
|
|
|
|
211
|
|
|
// With no buildings, this should always return empty |
212
|
|
|
$this->assertSame([], $planet->checkForDowngrade(100 * SmrPlanet::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE)); |
213
|
|
|
|
214
|
|
|
// Give the planet 2 structures, and destroy them both |
215
|
|
|
$planet->setBuilding(PLANET_GENERATOR, 2); |
216
|
|
|
srand(95); // seed rand for reproducibility |
217
|
|
|
$result = $planet->checkForDowngrade(2 * SmrPlanet::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE); |
218
|
|
|
$this->assertSame([PLANET_GENERATOR => 2], $result); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function test_buildings() : void { |
222
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
223
|
|
|
|
224
|
|
|
// Tests with no buildings |
225
|
|
|
$this->assertFalse($planet->hasBuilding(PLANET_HANGAR)); |
226
|
|
|
$this->assertSame(0, $planet->getBuilding(PLANET_HANGAR)); |
227
|
|
|
$this->assertSame(0.0, $planet->getLevel()); |
228
|
|
|
|
229
|
|
|
// Add some hangars |
230
|
|
|
$planet->increaseBuilding(PLANET_HANGAR, 4); |
231
|
|
|
$this->assertTrue($planet->hasBuilding(PLANET_HANGAR)); |
232
|
|
|
$this->assertSame(4, $planet->getBuilding(PLANET_HANGAR)); |
233
|
|
|
$this->assertSame(4/3, $planet->getLevel()); |
234
|
|
|
|
235
|
|
|
// Destroy some hangars |
236
|
|
|
$planet->destroyBuilding(PLANET_HANGAR, 2); |
237
|
|
|
$this->assertTrue($planet->hasBuilding(PLANET_HANGAR)); |
238
|
|
|
$this->assertSame(2, $planet->getBuilding(PLANET_HANGAR)); |
239
|
|
|
$this->assertSame(2/3, $planet->getLevel()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|