1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame\Routes; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Smr\Path; |
7
|
|
|
use Smr\Routes\MultiplePortRoute; |
8
|
|
|
use Smr\Routes\OneWayRoute; |
9
|
|
|
use Smr\Routes\RouteIterator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers Smr\Routes\RouteIterator |
13
|
|
|
*/ |
14
|
|
|
class RouteIteratorTest extends TestCase { |
15
|
|
|
|
16
|
|
|
public function test_iterator_states(): void { |
17
|
|
|
// Create a 2-port route from 1->2, 2->1 |
18
|
|
|
$path1 = new Path(1); |
19
|
|
|
$path1->addLink(2); |
20
|
|
|
$route1 = new OneWayRoute(1, 2, RACE_HUMAN, RACE_HUMAN, 1, 1, $path1, GOODS_WOOD); |
21
|
|
|
$path2 = new Path(2); |
22
|
|
|
$path2->addLink(1); |
23
|
|
|
$route2 = new OneWayRoute(2, 1, RACE_HUMAN, RACE_HUMAN, 1, 1, $path2, GOODS_ORE); |
24
|
|
|
$mpr = new MultiplePortRoute($route1, $route2); |
25
|
|
|
|
26
|
|
|
$iterator = new RouteIterator($mpr); |
27
|
|
|
|
28
|
|
|
// Check that the input route can be returned |
29
|
|
|
self::assertSame($mpr, $iterator->getEntireRoute()); |
30
|
|
|
|
31
|
|
|
// Check each state of the iterator until it rewinds |
32
|
|
|
$expectedStates = [ |
33
|
|
|
[$route1, $route1->getBuySectorId(), TRADER_BUYS], // initial state |
34
|
|
|
[$route1, $route1->getSellSectorId(), TRADER_SELLS], |
35
|
|
|
[$route2, $route2->getBuySectorId(), TRADER_BUYS], |
36
|
|
|
[$route2, $route2->getSellSectorId(), TRADER_SELLS], |
37
|
|
|
[$route1, $route1->getBuySectorId(), TRADER_BUYS], // return to initial |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
foreach ($expectedStates as [$route, $sectorID, $transaction]) { |
41
|
|
|
// Check the state of the iterator |
42
|
|
|
self::assertSame($route, $iterator->getCurrentRoute()); |
43
|
|
|
self::assertSame($sectorID, $iterator->getCurrentSectorID()); |
44
|
|
|
self::assertSame($transaction, $iterator->getCurrentTransaction()); |
45
|
|
|
|
46
|
|
|
// Advance the iterator |
47
|
|
|
$iterator->next(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|