1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) 2018 Douglas Reith. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Symfony\Component\Console\Application; |
13
|
|
|
use Symfony\Component\Console\Tester\ApplicationTester; |
14
|
|
|
use Reith\ToyRobot\Domain\Space\Table; |
15
|
|
|
|
16
|
|
|
// Persistence |
17
|
|
|
use Reith\ToyRobot\Infrastructure\Persistence\InMemoryRobotStore; |
18
|
|
|
use Reith\ToyRobot\Infrastructure\Persistence\RobotRepository; |
19
|
|
|
|
20
|
|
|
// Console tasks |
21
|
|
|
use Reith\ToyRobot\Console\Place; |
22
|
|
|
use Reith\ToyRobot\Console\Report; |
23
|
|
|
use Reith\ToyRobot\Console\Left; |
24
|
|
|
use Reith\ToyRobot\Console\Right; |
25
|
|
|
use Reith\ToyRobot\Console\Move; |
26
|
|
|
use Reith\ToyRobot\Console\BusHelper; |
27
|
|
|
|
28
|
|
|
// Buses |
29
|
|
|
use Reith\ToyRobot\Infrastructure\Bus\CommandBus; |
30
|
|
|
use Reith\ToyRobot\Infrastructure\Bus\QueryBus; |
31
|
|
|
|
32
|
|
|
// Command and query handlers |
33
|
|
|
use Reith\ToyRobot\CommandHandler\RobotPlacer; |
34
|
|
|
use Reith\ToyRobot\CommandHandler\RobotMover; |
35
|
|
|
use Reith\ToyRobot\QueryHandler\RobotReporter; |
36
|
|
|
|
37
|
|
|
class toyrobotTest extends TestCase |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
private $testApp; |
40
|
|
|
|
41
|
|
|
protected function setUp() |
42
|
|
|
{ |
43
|
|
|
$mockLogger = self::createMock(LoggerInterface::class); |
44
|
|
|
|
45
|
|
|
$store = InMemoryRobotStore::getStore($mockLogger); |
|
|
|
|
46
|
|
|
|
47
|
|
|
// Pass to the repository |
48
|
|
|
$repository = new RobotRepository($store); |
49
|
|
|
$table = Table::create(5); |
50
|
|
|
|
51
|
|
|
// Create the command and query handlers |
52
|
|
|
$robotPlacer = new RobotPlacer($table, $repository, $mockLogger); |
|
|
|
|
53
|
|
|
$robotMover = new RobotMover($repository, $mockLogger); |
|
|
|
|
54
|
|
|
$robotReporter = new RobotReporter($repository, $mockLogger); |
|
|
|
|
55
|
|
|
|
56
|
|
|
// Create the busses and register the handlers |
57
|
|
|
$commandBus = (new CommandBus()) |
58
|
|
|
->registerHandler($robotPlacer) |
59
|
|
|
->registerHandler($robotMover) |
60
|
|
|
; |
61
|
|
|
|
62
|
|
|
$queryBus = (new QueryBus()) |
63
|
|
|
->registerHandler($robotReporter) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
// Set up the console |
67
|
|
|
$application = new Application(); |
68
|
|
|
$application->setAutoExit(false); |
69
|
|
|
$application->addCommands([ |
70
|
|
|
new Place(), |
71
|
|
|
new Report(), |
72
|
|
|
new Left(), |
73
|
|
|
new Right(), |
74
|
|
|
new Move(), |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$busHelper = (new BusHelper()) |
78
|
|
|
->setCommandBus($commandBus) |
79
|
|
|
->setQueryBus($queryBus) |
80
|
|
|
; |
81
|
|
|
|
82
|
|
|
$application->getHelperSet()->set($busHelper); |
83
|
|
|
|
84
|
|
|
$this->testApp = new ApplicationTester($application); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testToyrobotApp() |
88
|
|
|
{ |
89
|
|
|
$this->testApp->run([]); |
90
|
|
|
|
91
|
|
|
self::assertContains( |
92
|
|
|
'Place the robot on the table', |
93
|
|
|
$this->testApp->getDisplay() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testPlaceRobot() |
100
|
|
|
{ |
101
|
|
|
$this->testApp->run(['place']); |
102
|
|
|
|
103
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testRobotReport() |
107
|
|
|
{ |
108
|
|
|
$this->testApp->run(['report']); |
109
|
|
|
|
110
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testPlacingAndReporting() |
114
|
|
|
{ |
115
|
|
|
$instruction = '2,2,S'; |
116
|
|
|
|
117
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]); |
118
|
|
|
|
119
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
120
|
|
|
|
121
|
|
|
$this->testApp->run(['report']); |
122
|
|
|
|
123
|
|
|
self::assertContains($instruction, $this->testApp->getDisplay()); |
124
|
|
|
|
125
|
|
|
// |
126
|
|
|
// An incorrect placement won't move the robot, it'll report |
127
|
|
|
// the same position |
128
|
|
|
// |
129
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => '12,4,W']); |
130
|
|
|
|
131
|
|
|
self::assertNotEquals(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
132
|
|
|
|
133
|
|
|
$this->testApp->run(['report']); |
134
|
|
|
|
135
|
|
|
self::assertContains($instruction, $this->testApp->getDisplay()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testTurningLeft() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$instruction = '3,2,W'; |
141
|
|
|
|
142
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]); |
143
|
|
|
|
144
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
145
|
|
|
|
146
|
|
|
$this->testApp->run(['left']); |
147
|
|
|
|
148
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
149
|
|
|
|
150
|
|
|
// Now face south |
151
|
|
|
self::assertContains('3,2,S', $this->testApp->getDisplay()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
View Code Duplication |
public function testTurningRight() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$instruction = '1,2,E'; |
157
|
|
|
|
158
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]); |
159
|
|
|
|
160
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
161
|
|
|
|
162
|
|
|
$this->testApp->run(['right']); |
163
|
|
|
|
164
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
165
|
|
|
|
166
|
|
|
// Now face south |
167
|
|
|
self::assertContains('1,2,S', $this->testApp->getDisplay()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
public function testMovingForward() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$instruction = '3,3,W'; |
173
|
|
|
|
174
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]); |
175
|
|
|
|
176
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
177
|
|
|
|
178
|
|
|
$this->testApp->run(['move']); |
179
|
|
|
|
180
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
181
|
|
|
|
182
|
|
|
// Now x has decreased |
183
|
|
|
self::assertContains('2,3,W', $this->testApp->getDisplay()); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.