|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Globals; |
|
7
|
|
|
use SmrPlanet; |
|
8
|
|
|
use SmrTest\BaseIntegrationSpec; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers SmrPlanet |
|
12
|
|
|
*/ |
|
13
|
|
|
class SmrPlanetIntegrationTest extends BaseIntegrationSpec { |
|
14
|
|
|
|
|
15
|
|
|
protected function tablesToTruncate(): array { |
|
16
|
|
|
return ['planet']; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function tearDown(): void { |
|
20
|
|
|
SmrPlanet::clearCache(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function test_createPlanet(): void { |
|
24
|
|
|
// Test arbitrary input |
|
25
|
|
|
$sectorID = 2; |
|
26
|
|
|
$gameID = 42; |
|
27
|
|
|
$typeID = 3; |
|
28
|
|
|
$inhabitableTime = 5; |
|
29
|
|
|
|
|
30
|
|
|
$planet = SmrPlanet::createPlanet($gameID, $sectorID, $typeID, $inhabitableTime); |
|
31
|
|
|
$this->assertTrue($planet->exists()); |
|
32
|
|
|
|
|
33
|
|
|
// Check properties set explicitly |
|
34
|
|
|
$this->assertSame($gameID, $planet->getGameID()); |
|
35
|
|
|
$this->assertSame($sectorID, $planet->getSectorID()); |
|
36
|
|
|
$this->assertSame($typeID, $planet->getTypeID()); |
|
37
|
|
|
$this->assertSame($inhabitableTime, $planet->getInhabitableTime()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function test_createPlanet_already_exists(): void { |
|
41
|
|
|
SmrPlanet::createPlanet(1, 1, 1, 1); |
|
42
|
|
|
$this->expectException(Exception::class); |
|
43
|
|
|
$this->expectExceptionMessage('Planet already exists'); |
|
44
|
|
|
SmrPlanet::createPlanet(1, 1, 1, 1); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function test_removePlanet(): void { |
|
48
|
|
|
// Check that planet exists |
|
49
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
50
|
|
|
$this->assertTrue($planet->exists()); |
|
51
|
|
|
|
|
52
|
|
|
SmrPlanet::removePlanet(1, 1); |
|
53
|
|
|
$planet = SmrPlanet::getPlanet(1, 1, true); |
|
54
|
|
|
$this->assertFalse($planet->exists()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function test_name(): void { |
|
58
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
59
|
|
|
// Check default name |
|
60
|
|
|
$this->assertSame('Unknown', $planet->getDisplayName()); |
|
61
|
|
|
|
|
62
|
|
|
// Set a new name (include non-HTML-safe character) |
|
63
|
|
|
$planet->setName('Test&'); |
|
64
|
|
|
$this->assertSame('Test&', $planet->getDisplayName()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_owner(): void { |
|
68
|
|
|
// Check default owner |
|
69
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
70
|
|
|
$this->assertFalse($planet->hasOwner()); |
|
71
|
|
|
$this->assertSame(0, $planet->getOwnerID()); |
|
72
|
|
|
|
|
73
|
|
|
// Set a new owner |
|
74
|
|
|
$ownerID = 3; |
|
75
|
|
|
$planet->setOwnerID($ownerID); |
|
76
|
|
|
$this->assertTrue($planet->hasOwner()); |
|
77
|
|
|
$this->assertSame($ownerID, $planet->getOwnerID()); |
|
78
|
|
|
|
|
79
|
|
|
// Remove the owner again |
|
80
|
|
|
$planet->removeOwner(); |
|
81
|
|
|
$this->assertFalse($planet->hasOwner()); |
|
82
|
|
|
$this->assertSame(0, $planet->getOwnerID()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_password(): void { |
|
86
|
|
|
// Check default password |
|
87
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
88
|
|
|
$this->assertSame('', $planet->getPassword()); |
|
89
|
|
|
|
|
90
|
|
|
// Set a new password |
|
91
|
|
|
$password = 'test'; |
|
92
|
|
|
$planet->setPassword($password); |
|
93
|
|
|
$this->assertSame($password, $planet->getPassword()); |
|
94
|
|
|
|
|
95
|
|
|
// Remove the password again |
|
96
|
|
|
$planet->removePassword(); |
|
97
|
|
|
$this->assertSame('', $planet->getPassword()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function test_credits(): void { |
|
101
|
|
|
// Check default credits |
|
102
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
103
|
|
|
$this->assertSame(0, $planet->getCredits()); |
|
104
|
|
|
|
|
105
|
|
|
// Check increase/decrease credits |
|
106
|
|
|
$planet->increaseCredits(100); |
|
107
|
|
|
$this->assertSame(100, $planet->getCredits()); |
|
108
|
|
|
$planet->increaseCredits(50); |
|
109
|
|
|
$this->assertSame(150, $planet->getCredits()); |
|
110
|
|
|
$planet->decreaseCredits(50); |
|
111
|
|
|
$this->assertSame(100, $planet->getCredits()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function test_bonds(): void { |
|
115
|
|
|
// Check default bond |
|
116
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
117
|
|
|
$this->assertSame(0, $planet->getBonds()); |
|
118
|
|
|
|
|
119
|
|
|
// Check increase/decrease bonds |
|
120
|
|
|
$planet->increaseBonds(100); |
|
121
|
|
|
$this->assertSame(100, $planet->getBonds()); |
|
122
|
|
|
$planet->increaseBonds(50); |
|
123
|
|
|
$this->assertSame(150, $planet->getBonds()); |
|
124
|
|
|
$planet->decreaseBonds(50); |
|
125
|
|
|
$this->assertSame(100, $planet->getBonds()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function test_bond_maturity(): void { |
|
129
|
|
|
// Check default maturity |
|
130
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
131
|
|
|
$this->assertSame(0, $planet->getMaturity()); |
|
132
|
|
|
|
|
133
|
|
|
// Set a new bond maturity |
|
134
|
|
|
$maturity = time(); |
|
135
|
|
|
$planet->setMaturity($maturity); |
|
136
|
|
|
$this->assertSame($maturity, $planet->getMaturity()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function test_stockpile(): void { |
|
140
|
|
|
// Check default stockpile |
|
141
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
142
|
|
|
$this->assertFalse($planet->hasStockpile()); |
|
143
|
|
|
$this->assertSame([], $planet->getStockpile()); |
|
144
|
|
|
foreach (array_keys(Globals::getGoods()) as $goodID) { |
|
145
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
|
146
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// Setting 0 still counts as empty |
|
150
|
|
|
$planet->setStockpile(GOODS_ORE, 0); |
|
151
|
|
|
$this->assertFalse($planet->hasStockpile()); |
|
152
|
|
|
$this->assertFalse($planet->hasStockpile(GOODS_ORE)); |
|
153
|
|
|
|
|
154
|
|
|
// Check increase stockpile |
|
155
|
|
|
$planet->increaseStockpile(GOODS_ORE, 50); |
|
156
|
|
|
$this->assertTrue($planet->hasStockpile()); |
|
157
|
|
|
$this->assertSame([GOODS_ORE => 50], $planet->getStockpile()); |
|
158
|
|
|
foreach (array_keys(Globals::getGoods()) as $goodID) { |
|
159
|
|
|
if ($goodID === GOODS_ORE) { |
|
160
|
|
|
$this->assertTrue($planet->hasStockpile($goodID)); |
|
161
|
|
|
$this->assertSame(50, $planet->getStockpile($goodID)); |
|
162
|
|
|
} else { |
|
163
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
|
164
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Check decrease stockpile |
|
169
|
|
|
$planet->decreaseStockpile(GOODS_ORE, 10); |
|
170
|
|
|
$this->assertTrue($planet->hasStockpile()); |
|
171
|
|
|
$this->assertSame([GOODS_ORE => 40], $planet->getStockpile()); |
|
172
|
|
|
foreach (array_keys(Globals::getGoods()) as $goodID) { |
|
173
|
|
|
if ($goodID === GOODS_ORE) { |
|
174
|
|
|
$this->assertTrue($planet->hasStockpile($goodID)); |
|
175
|
|
|
$this->assertSame(40, $planet->getStockpile($goodID)); |
|
176
|
|
|
} else { |
|
177
|
|
|
$this->assertFalse($planet->hasStockpile($goodID)); |
|
178
|
|
|
$this->assertSame(0, $planet->getStockpile($goodID)); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Check remaining stockpile (ore: 600 - 40) |
|
183
|
|
|
$this->assertSame(560, $planet->getRemainingStockpile(GOODS_ORE)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function test_setStockpile_throws_when_negative(): void { |
|
187
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
188
|
|
|
$this->expectException(Exception::class); |
|
189
|
|
|
$this->expectExceptionMessage('Trying to set negative stockpile'); |
|
190
|
|
|
$planet->setStockpile(GOODS_ORE, -20); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function test_setBuilding_throws_when_negative(): void { |
|
194
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
195
|
|
|
$this->expectException(Exception::class); |
|
196
|
|
|
$this->expectExceptionMessage('Cannot set negative number of buildings'); |
|
197
|
|
|
$planet->setBuilding(PLANET_HANGAR, -1); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function test_destroyBuilding_throws_when_invalid(): void { |
|
201
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
202
|
|
|
$this->expectException(Exception::class); |
|
203
|
|
|
$this->expectExceptionMessage('Cannot set negative number of buildings'); |
|
204
|
|
|
$planet->destroyBuilding(PLANET_TURRET, 1); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function test_checkForDowngrade(): void { |
|
208
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
209
|
|
|
|
|
210
|
|
|
// If we don't do enough damage, we should never downgrade |
|
211
|
|
|
$this->assertSame([], $planet->checkForDowngrade(0)); |
|
212
|
|
|
|
|
213
|
|
|
// With no buildings, this should always return empty |
|
214
|
|
|
$this->assertSame([], $planet->checkForDowngrade(100 * SmrPlanet::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE)); |
|
215
|
|
|
|
|
216
|
|
|
// Give the planet 2 structures, and destroy them both |
|
217
|
|
|
$planet->setBuilding(PLANET_GENERATOR, 2); |
|
218
|
|
|
srand(95); // seed rand for reproducibility |
|
219
|
|
|
$result = $planet->checkForDowngrade(2 * SmrPlanet::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE); |
|
220
|
|
|
$this->assertSame([PLANET_GENERATOR => 2], $result); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function test_buildings(): void { |
|
224
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 1, 1); |
|
225
|
|
|
|
|
226
|
|
|
// Tests with no buildings |
|
227
|
|
|
$this->assertFalse($planet->hasBuilding(PLANET_HANGAR)); |
|
228
|
|
|
$this->assertSame(0, $planet->getBuilding(PLANET_HANGAR)); |
|
229
|
|
|
$this->assertSame(0.0, $planet->getLevel()); |
|
230
|
|
|
|
|
231
|
|
|
// Add some hangars |
|
232
|
|
|
$planet->increaseBuilding(PLANET_HANGAR, 4); |
|
233
|
|
|
$this->assertTrue($planet->hasBuilding(PLANET_HANGAR)); |
|
234
|
|
|
$this->assertSame(4, $planet->getBuilding(PLANET_HANGAR)); |
|
235
|
|
|
$this->assertSame(4 / 3, $planet->getLevel()); |
|
236
|
|
|
|
|
237
|
|
|
// Destroy some hangars |
|
238
|
|
|
$planet->destroyBuilding(PLANET_HANGAR, 2); |
|
239
|
|
|
$this->assertTrue($planet->hasBuilding(PLANET_HANGAR)); |
|
240
|
|
|
$this->assertSame(2, $planet->getBuilding(PLANET_HANGAR)); |
|
241
|
|
|
$this->assertSame(2 / 3, $planet->getLevel()); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function test_defenses(): void { |
|
245
|
|
|
// Make a Defense World planet |
|
246
|
|
|
$planet = SmrPlanet::createPlanet(1, 1, 4, 1); |
|
247
|
|
|
|
|
248
|
|
|
// Add buildings so that we can add defenses |
|
249
|
|
|
$planet->increaseBuilding(PLANET_GENERATOR, 1); |
|
250
|
|
|
$planet->increaseBuilding(PLANET_HANGAR, 1); |
|
251
|
|
|
$planet->increaseBuilding(PLANET_BUNKER, 1); |
|
252
|
|
|
|
|
253
|
|
|
// Make sure there are no defenses to start |
|
254
|
|
|
self::assertSame(0, $planet->getShields()); |
|
255
|
|
|
self::assertFalse($planet->hasShields()); |
|
256
|
|
|
self::assertSame(0, $planet->getCDs()); |
|
257
|
|
|
self::assertFalse($planet->hasCDs()); |
|
258
|
|
|
self::assertSame(0, $planet->getArmour()); |
|
259
|
|
|
self::assertFalse($planet->hasArmour()); |
|
260
|
|
|
|
|
261
|
|
|
// Increase shields |
|
262
|
|
|
$planet->increaseShields(10); |
|
263
|
|
|
self::assertSame(10, $planet->getShields()); |
|
264
|
|
|
self::assertTrue($planet->hasShields()); |
|
265
|
|
|
|
|
266
|
|
|
// Don't increase shields |
|
267
|
|
|
$planet->increaseShields(0); |
|
268
|
|
|
self::assertSame(10, $planet->getShields()); |
|
269
|
|
|
|
|
270
|
|
|
// Decrease shields |
|
271
|
|
|
$planet->decreaseShields(2); |
|
272
|
|
|
self::assertSame(8, $planet->getShields()); |
|
273
|
|
|
|
|
274
|
|
|
// Make sure we can't go above the Generator limit |
|
275
|
|
|
$planet->setShields(PLANET_GENERATOR_SHIELDS + 1); |
|
276
|
|
|
self::assertSame(PLANET_GENERATOR_SHIELDS, $planet->getShields()); |
|
277
|
|
|
|
|
278
|
|
|
// Make sure we can't go below 0 shields |
|
279
|
|
|
$planet->setShields(-1); |
|
280
|
|
|
self::assertSame(0, $planet->getShields()); |
|
281
|
|
|
|
|
282
|
|
|
// Increase CDs |
|
283
|
|
|
$planet->increaseCDs(5); |
|
284
|
|
|
self::assertSame(5, $planet->getCDs()); |
|
285
|
|
|
self::assertTrue($planet->hasCDs()); |
|
286
|
|
|
|
|
287
|
|
|
// Don't increase CDs |
|
288
|
|
|
$planet->increaseCDs(0); |
|
289
|
|
|
self::assertSame(5, $planet->getCDs()); |
|
290
|
|
|
|
|
291
|
|
|
// Decrease CDs |
|
292
|
|
|
$planet->decreaseCDs(3); |
|
293
|
|
|
self::assertSame(2, $planet->getCDs()); |
|
294
|
|
|
|
|
295
|
|
|
// Make sure we can't go above the Hangar limit |
|
296
|
|
|
$planet->setCDs(PLANET_HANGAR_DRONES + 1); |
|
297
|
|
|
self::assertSame(PLANET_HANGAR_DRONES, $planet->getCDs()); |
|
298
|
|
|
|
|
299
|
|
|
// Make sure we can't go below 0 CDs |
|
300
|
|
|
$planet->setCDs(-1); |
|
301
|
|
|
self::assertSame(0, $planet->getCDs()); |
|
302
|
|
|
|
|
303
|
|
|
// Increase armour |
|
304
|
|
|
$planet->increaseArmour(15); |
|
305
|
|
|
self::assertSame(15, $planet->getArmour()); |
|
306
|
|
|
self::assertTrue($planet->hasArmour()); |
|
307
|
|
|
|
|
308
|
|
|
// Don't increase armour |
|
309
|
|
|
$planet->increaseArmour(0); |
|
310
|
|
|
self::assertSame(15, $planet->getArmour()); |
|
311
|
|
|
|
|
312
|
|
|
// Decrease armour |
|
313
|
|
|
$planet->decreaseArmour(4); |
|
314
|
|
|
self::assertSame(11, $planet->getArmour()); |
|
315
|
|
|
|
|
316
|
|
|
// Make sure we can't go above the Bunker limit |
|
317
|
|
|
$planet->setArmour(PLANET_BUNKER_ARMOUR + 1); |
|
318
|
|
|
self::assertSame(PLANET_BUNKER_ARMOUR, $planet->getArmour()); |
|
319
|
|
|
|
|
320
|
|
|
// Make sure we can't go below 0 armour |
|
321
|
|
|
$planet->setArmour(-1); |
|
322
|
|
|
self::assertSame(0, $planet->getArmour()); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
} |
|
326
|
|
|
|