|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use SmrSector; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @covers SmrSector |
|
9
|
|
|
*/ |
|
10
|
|
|
class SmrSectorTest extends \PHPUnit\Framework\TestCase { |
|
11
|
|
|
|
|
12
|
|
|
protected function setUp() : void { |
|
13
|
|
|
SmrSector::clearCache(); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function test_setLink() : void { |
|
17
|
|
|
// Construct a new sector |
|
18
|
|
|
$sector = SmrSector::createSector(1, 1); |
|
19
|
|
|
|
|
20
|
|
|
// Test that there are no links to begin with |
|
21
|
|
|
$dir = 'Up'; |
|
22
|
|
|
self::assertSame([], $sector->getLinks()); |
|
23
|
|
|
self::assertSame(0, $sector->getNumberOfLinks()); |
|
24
|
|
|
self::assertSame(0, $sector->getLink($dir)); |
|
25
|
|
|
self::assertFalse($sector->hasLink($dir)); |
|
26
|
|
|
|
|
27
|
|
|
// When we add a single link |
|
28
|
|
|
$sector->setLink($dir, 2); |
|
29
|
|
|
|
|
30
|
|
|
// Then we should have updated the links |
|
31
|
|
|
self::assertSame([$dir => 2], $sector->getLinks()); |
|
32
|
|
|
self::assertSame(1, $sector->getNumberOfLinks()); |
|
33
|
|
|
self::assertSame(2, $sector->getLink($dir)); |
|
34
|
|
|
self::assertTrue($sector->hasLink($dir)); |
|
35
|
|
|
|
|
36
|
|
|
// When we remove the link |
|
37
|
|
|
$sector->setLink($dir, 0); |
|
38
|
|
|
|
|
39
|
|
|
// Then we should have updated the links |
|
40
|
|
|
self::assertSame([], $sector->getLinks()); |
|
41
|
|
|
self::assertSame(0, $sector->getNumberOfLinks()); |
|
42
|
|
|
self::assertSame(0, $sector->getLink($dir)); |
|
43
|
|
|
self::assertFalse($sector->hasLink($dir)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function test_setLinkSector() : void { |
|
47
|
|
|
// Construct two new sectors |
|
48
|
|
|
$sector1 = SmrSector::createSector(1, 1); |
|
49
|
|
|
$sector2 = SmrSector::createSector(1, 2); |
|
50
|
|
|
|
|
51
|
|
|
// Test that there is no link to start |
|
52
|
|
|
$dir = 'Left'; |
|
53
|
|
|
$oppositeDir = SmrSector::oppositeDir($dir); |
|
54
|
|
|
self::assertFalse($sector1->getLinkSector($dir)); |
|
55
|
|
|
self::assertFalse($sector2->getLinkSector($oppositeDir)); |
|
56
|
|
|
|
|
57
|
|
|
// When we link the two sectors |
|
58
|
|
|
$sector1->setLinkSector($dir, $sector2); |
|
59
|
|
|
|
|
60
|
|
|
// Then both sectors should have updated links |
|
61
|
|
|
self::assertSame($sector2, $sector1->getLinkSector($dir)); |
|
62
|
|
|
self::assertSame($sector1, $sector2->getLinkSector($oppositeDir)); |
|
63
|
|
|
self::assertSame($sector2->getSectorID(), $sector1->getLink($dir)); |
|
64
|
|
|
self::assertSame($sector1->getSectorID(), $sector2->getLink($oppositeDir)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_cannot_link_to_self() : void { |
|
68
|
|
|
$sector = SmrSector::createSector(1, 1); |
|
69
|
|
|
$this->expectException(\Exception::class); |
|
70
|
|
|
$this->expectExceptionMessage('Sector must not link to itself!'); |
|
71
|
|
|
$sector->setLink('Up', $sector->getSectorID()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function test_setWarp() : void { |
|
75
|
|
|
// Construct two new sectors |
|
76
|
|
|
$sector1 = SmrSector::createSector(1, 1); |
|
77
|
|
|
$sector2 = SmrSector::createSector(1, 2); |
|
78
|
|
|
|
|
79
|
|
|
// Test that there are no warps to begin with |
|
80
|
|
|
foreach ([$sector1, $sector2] as $sector) { |
|
81
|
|
|
self::assertFalse($sector->hasWarp()); |
|
82
|
|
|
self::assertSame(0, $sector->getWarp()); |
|
83
|
|
|
self::assertSame(0, $sector->getNumberOfConnections()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// When we add a warp between the two sectors |
|
87
|
|
|
$sector1->setWarp($sector2); |
|
88
|
|
|
|
|
89
|
|
|
// Then we should have updated warps |
|
90
|
|
|
foreach ([[$sector1, $sector2], [$sector2, $sector1]] as list($sectorA, $sectorB)) { |
|
91
|
|
|
self::assertTrue($sectorA->hasWarp()); |
|
92
|
|
|
self::assertSame($sectorB->getSectorID(), $sectorA->getWarp()); |
|
93
|
|
|
self::assertSame(1, $sectorA->getNumberOfConnections()); |
|
94
|
|
|
self::assertSame($sectorB, $sectorA->getWarpSector()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// When we remove the warp |
|
98
|
|
|
$sector1->removeWarp(); |
|
99
|
|
|
|
|
100
|
|
|
// Then we should have updated warps |
|
101
|
|
|
foreach ([$sector1, $sector2] as $sector) { |
|
102
|
|
|
self::assertFalse($sector->hasWarp()); |
|
103
|
|
|
self::assertSame(0, $sector->getWarp()); |
|
104
|
|
|
self::assertSame(0, $sector->getNumberOfConnections()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function test_cannot_warp_to_self() : void { |
|
109
|
|
|
$sector = SmrSector::createSector(1, 1); |
|
110
|
|
|
$this->expectException(\Exception::class); |
|
111
|
|
|
$this->expectExceptionMessage('Sector must not warp to itself!'); |
|
112
|
|
|
$sector->setWarp($sector); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function test_cannot_have_multiple_warps() : void { |
|
116
|
|
|
$sector1 = SmrSector::createSector(1, 1); |
|
117
|
|
|
$sector2 = SmrSector::createSector(1, 2); |
|
118
|
|
|
$sector3 = SmrSector::createSector(1, 3); |
|
119
|
|
|
|
|
120
|
|
|
// When we already have 1 warp set |
|
121
|
|
|
$sector1->setWarp($sector2); |
|
122
|
|
|
|
|
123
|
|
|
// Then we cannot set a 2nd warp |
|
124
|
|
|
$this->expectException(\Exception::class); |
|
125
|
|
|
$this->expectExceptionMessage('Sector 1 already has a warp (to 2)'); |
|
126
|
|
|
$sector1->setWarp($sector3); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function test_getSectorDirection() : void { |
|
130
|
|
|
$sector1 = SmrSector::createSector(1, 1); |
|
131
|
|
|
$sector2 = SmrSector::createSector(1, 2); |
|
132
|
|
|
|
|
133
|
|
|
// Test when we pass the same sector ID |
|
134
|
|
|
self::assertSame('Current', $sector1->getSectorDirection($sector1->getSectorID())); |
|
135
|
|
|
|
|
136
|
|
|
// Test when we pass an unconnected sector ID |
|
137
|
|
|
self::assertSame('None', $sector1->getSectorDirection($sector2->getSectorID())); |
|
138
|
|
|
|
|
139
|
|
|
// Test when we set a warp |
|
140
|
|
|
$sector1->setWarp($sector2); |
|
141
|
|
|
self::assertSame('Warp', $sector1->getSectorDirection($sector2->getSectorID())); |
|
142
|
|
|
$sector1->removeWarp(); |
|
143
|
|
|
|
|
144
|
|
|
// Test when we set a link |
|
145
|
|
|
$dir = 'Down'; |
|
146
|
|
|
$sector1->setLink($dir, $sector2->getSectorID()); |
|
147
|
|
|
self::assertSame($dir, $sector1->getSectorDirection($sector2->getSectorID())); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|