1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Smr\Exceptions\GalaxyNotFound; |
6
|
|
|
use SmrGalaxy; |
7
|
|
|
use SmrSector; |
8
|
|
|
use SmrTest\BaseIntegrationSpec; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers SmrGalaxy |
12
|
|
|
*/ |
13
|
|
|
class SmrGalaxyTest extends BaseIntegrationSpec { |
14
|
|
|
|
15
|
|
|
protected function tablesToTruncate(): array { |
16
|
|
|
return ['game_galaxy']; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function setUp(): void { |
20
|
|
|
// Start each test with an empty galaxy cache |
21
|
|
|
SmrGalaxy::clearCache(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function test_getGalaxy_throws_if_galaxy_does_not_exist(): void { |
25
|
|
|
// Test that we raise an exception with the wrong Alliance name |
26
|
|
|
$this->expectException(GalaxyNotFound::class); |
27
|
|
|
$this->expectExceptionMessage('No such galaxy: 1-2'); |
28
|
|
|
SmrGalaxy::getGalaxy(1, 2); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_createGalaxy(): void { |
32
|
|
|
// Test arbitrary input |
33
|
|
|
$gameID = 42; |
34
|
|
|
$galaxyID = 3; |
35
|
|
|
|
36
|
|
|
$galaxy = SmrGalaxy::createGalaxy($gameID, $galaxyID); |
37
|
|
|
|
38
|
|
|
$this->assertSame($gameID, $galaxy->getGameID()); |
39
|
|
|
$this->assertSame($galaxyID, $galaxy->getGalaxyID()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_set_galaxy_dimensions(): void { |
43
|
|
|
$galaxy = SmrGalaxy::createGalaxy(1, 1); |
44
|
|
|
$galaxy->setWidth(3); |
45
|
|
|
$galaxy->setHeight(7); |
46
|
|
|
|
47
|
|
|
self::assertSame(3, $galaxy->getWidth()); |
48
|
|
|
self::assertSame(7, $galaxy->getHeight()); |
49
|
|
|
self::assertSame(21, $galaxy->getSize()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_get_galaxy_sector_range(): void { |
53
|
|
|
// Create two sequential galaxies |
54
|
|
|
$galaxy1 = SmrGalaxy::createGalaxy(1, 1); |
55
|
|
|
$galaxy2 = SmrGalaxy::createGalaxy(1, 2); |
56
|
|
|
|
57
|
|
|
// Due to getStartSector calling getGameGalaxies, we need the galaxies |
58
|
|
|
// to be complete so that they can be saved/loaded from the database. |
59
|
|
|
foreach ([$galaxy1, $galaxy2] as $galaxy) { |
60
|
|
|
$galaxy->setWidth(3); |
61
|
|
|
$galaxy->setHeight(7); |
62
|
|
|
$galaxy->setName('A'); |
63
|
|
|
$galaxy->setGalaxyType(SmrGalaxy::TYPE_NEUTRAL); |
64
|
|
|
$galaxy->setMaxForceTime(0); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// We need to save to database due to how getStartSector works |
68
|
|
|
SmrGalaxy::saveGalaxies(); |
69
|
|
|
|
70
|
|
|
// Check the galaxy start and end sectors |
71
|
|
|
self::assertSame(1, $galaxy1->getStartSector()); |
72
|
|
|
self::assertSame(21, $galaxy1->getEndSector()); |
73
|
|
|
self::assertSame(22, $galaxy2->getStartSector()); |
74
|
|
|
self::assertSame(42, $galaxy2->getEndSector()); |
75
|
|
|
|
76
|
|
|
// While we have start sectors calculated, check contains |
77
|
|
|
self::assertFalse($galaxy1->contains(22)); |
78
|
|
|
self::assertTrue($galaxy2->contains(22)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function test_contains_when_passed_an_SmrSector(): void { |
82
|
|
|
$galaxyID = 3; |
83
|
|
|
$galaxy = SmrGalaxy::createGalaxy(1, $galaxyID); |
84
|
|
|
|
85
|
|
|
// Test a sector that should be in the galaxy |
86
|
|
|
$sector1 = $this->createPartialMock(SmrSector::class, ['getGalaxyID']); |
87
|
|
|
$sector1->method('getGalaxyID')->willReturn($galaxyID); |
88
|
|
|
self::assertTrue($galaxy->contains($sector1)); |
89
|
|
|
|
90
|
|
|
// Test a sector that should NOT be in the galaxy |
91
|
|
|
$sector2 = $this->createPartialMock(SmrSector::class, ['getGalaxyID']); |
92
|
|
|
$sector2->method('getGalaxyID')->willReturn($galaxyID + 1); |
93
|
|
|
self::assertFalse($galaxy->contains($sector2)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|