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\BusHelper; |
24
|
|
|
|
25
|
|
|
// Buses |
26
|
|
|
use Reith\ToyRobot\Infrastructure\Bus\CommandBus; |
27
|
|
|
use Reith\ToyRobot\Infrastructure\Bus\QueryBus; |
28
|
|
|
|
29
|
|
|
// Command and query handlers |
30
|
|
|
use Reith\ToyRobot\CommandHandler\RobotPlacer; |
31
|
|
|
use Reith\ToyRobot\QueryHandler\RobotReporter; |
32
|
|
|
|
33
|
|
|
class toyrobotTest extends TestCase |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
private $testApp; |
36
|
|
|
|
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
$mockLogger = self::createMock(LoggerInterface::class); |
40
|
|
|
|
41
|
|
|
$store = InMemoryRobotStore::getStore($mockLogger); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Pass to the repository |
44
|
|
|
$repository = new RobotRepository($store); |
45
|
|
|
$table = Table::create(5); |
46
|
|
|
|
47
|
|
|
// Create the command and query handlers |
48
|
|
|
$robotPlacer = new RobotPlacer($table, $repository, $mockLogger); |
|
|
|
|
49
|
|
|
$robotReporter = new RobotReporter($repository, $mockLogger); |
|
|
|
|
50
|
|
|
|
51
|
|
|
// Now the buses |
52
|
|
|
$commandBus = (new CommandBus()) |
53
|
|
|
->registerHandler($robotPlacer) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$queryBus = (new QueryBus()) |
57
|
|
|
->registerHandler($robotReporter) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
// Set up the console |
61
|
|
|
$application = new Application(); |
62
|
|
|
$application->setAutoExit(false); |
63
|
|
|
$application->addCommands([new Place(), new Report()]); |
64
|
|
|
|
65
|
|
|
$busHelper = (new BusHelper()) |
66
|
|
|
->setCommandBus($commandBus) |
67
|
|
|
->setQueryBus($queryBus) |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
$application->getHelperSet()->set($busHelper); |
71
|
|
|
|
72
|
|
|
$this->testApp = new ApplicationTester($application); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testToyrobotApp() |
76
|
|
|
{ |
77
|
|
|
$this->testApp->run([]); |
78
|
|
|
|
79
|
|
|
self::assertContains( |
80
|
|
|
'Place the robot on the table', |
81
|
|
|
$this->testApp->getDisplay() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testPlaceRobot() |
88
|
|
|
{ |
89
|
|
|
$this->testApp->run(['place']); |
90
|
|
|
|
91
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testRobotReport() |
95
|
|
|
{ |
96
|
|
|
$this->testApp->run(['report']); |
97
|
|
|
|
98
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testPlacingAndReporting() |
102
|
|
|
{ |
103
|
|
|
$instruction = '2,2,S'; |
104
|
|
|
|
105
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => '2,2,S']); |
106
|
|
|
|
107
|
|
|
self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
108
|
|
|
|
109
|
|
|
$this->testApp->run(['report']); |
110
|
|
|
|
111
|
|
|
self::assertContains($instruction, $this->testApp->getDisplay()); |
112
|
|
|
|
113
|
|
|
// |
114
|
|
|
// An incorrect placement won't move the robot, it'll report |
115
|
|
|
// the same position |
116
|
|
|
// |
117
|
|
|
$this->testApp->run(['command' => 'place', 'X,Y,F' => '12,4,W']); |
118
|
|
|
|
119
|
|
|
self::assertNotEquals(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay()); |
120
|
|
|
|
121
|
|
|
$this->testApp->run(['report']); |
122
|
|
|
|
123
|
|
|
self::assertContains($instruction, $this->testApp->getDisplay()); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.