Skip to content

Commit 108ec75

Browse files
author
alutskevich
committed
Step 5: create unit test for command and fix bugs
1 parent ff7ca53 commit 108ec75

File tree

3 files changed

+150
-9
lines changed

3 files changed

+150
-9
lines changed

src/Application/Assertions/RoverCoordinatesIsValidAssertion.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class RoverCoordinatesIsValidAssertion
1010
{
11+
/**
12+
* @throws InvalidArgumentException
13+
*/
1114
public function assert(
1215
BorderCoordinatesDto $borderCoordinatesDto,
1316
int $roverXCoordinate,

src/Command/NasaRoversCommand.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ protected function execute(
8686

8787
$roversInformation = $this->getRoversInformation($input, $output);
8888

89+
if (\count($roversInformation) === 0) {
90+
$output->write('Rovers are empty');
91+
92+
return Command::SUCCESS;
93+
}
94+
8995
try {
9096
$rovers = $this->setupRovers($borderCoordinatesDto, $roversInformation);
9197
} catch (InvalidRoverInformationException $exception) {
@@ -100,12 +106,6 @@ protected function execute(
100106
return Command::INVALID;
101107
}
102108

103-
if (\count($rovers) === 0) {
104-
$output->write('Rovers are empty');
105-
106-
return Command::SUCCESS;
107-
}
108-
109109
foreach ($rovers as $roverInformation) {
110110
$output->write('Moving rover...');
111111
$rover = $this->nasaRoverService->process($roverInformation);
@@ -170,15 +170,21 @@ private function getRoversInformation(InputInterface $input, OutputInterface $ou
170170
$question = new Question('Enter rovers information: ');
171171
$question->setMultiline(true);
172172

173-
$answer = $this->questionHelper->ask($input, $output, $question);
173+
$answer = $this->questionHelper->ask($input, $output, $question) ?? '';
174+
175+
if ($answer === '') {
176+
return [];
177+
}
174178

175179
$roversInformation = \explode("\n", $answer);
176180
$countLines = \count($roversInformation);
177181

178182
if ($countLines%2 !== 0) {
179183
throw new InvalidArgumentException(
180-
'Every rover should have 2 lines: one with coordinates and one with actions. You have %d lines',
181-
$countLines
184+
\sprintf(
185+
'Every rover should have 2 lines: one with coordinates and one with actions. You have %d lines',
186+
$countLines
187+
)
182188
);
183189
}
184190

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\Unit\Command;
5+
6+
use App\Command\NasaRoversCommand;
7+
use InvalidArgumentException;
8+
use Symfony\Bundle\FrameworkBundle\Console\Application;
9+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
10+
use Symfony\Component\Console\Tester\CommandTester;
11+
12+
class NasaRoversCommandTest extends KernelTestCase
13+
{
14+
private CommandTester $commandTester;
15+
16+
public function setUp(): void
17+
{
18+
$kernel = self::bootKernel();
19+
$application = new Application($kernel);
20+
21+
/** @var NasaRoversCommand $command */
22+
$command = $application->find('app:nasa-rovers');
23+
$this->commandTester = new CommandTester($command);
24+
25+
parent::setUp();
26+
}
27+
28+
public function testSuccessExecute(): void
29+
{
30+
$this->commandTester->setInputs([
31+
'5 5',
32+
"1 2 N\nMM"
33+
]);
34+
35+
$this->commandTester->execute([]);
36+
37+
$this->commandTester->assertCommandIsSuccessful();
38+
}
39+
40+
public function testSuccessExecuteWithoutRovers(): void
41+
{
42+
$this->commandTester->setInputs([
43+
'5 5',
44+
''
45+
]);
46+
47+
$this->commandTester->execute([]);
48+
49+
$output = $this->commandTester->getDisplay();
50+
$this->assertStringContainsString('Rovers are empty', $output);
51+
52+
$this->commandTester->assertCommandIsSuccessful();
53+
}
54+
55+
public function testInvalidTopRightXCoordinate(): void
56+
{
57+
$this->commandTester->setInputs([
58+
'0 5'
59+
]);
60+
61+
$this->expectException(InvalidArgumentException::class);
62+
$this->expectExceptionMessage('Top-right X coordinate should be more than 0');
63+
64+
$this->commandTester->execute([]);
65+
}
66+
67+
public function testInvalidTopRightYCoordinate(): void
68+
{
69+
$this->commandTester->setInputs([
70+
'4 0'
71+
]);
72+
73+
$this->expectException(InvalidArgumentException::class);
74+
$this->expectExceptionMessage('Top-right Y coordinate should be more than 0');
75+
76+
$this->commandTester->execute([]);
77+
}
78+
79+
public function testInvalidRoverInformationNotEnoughLines(): void
80+
{
81+
$this->commandTester->setInputs([
82+
'5 5',
83+
'1 2 N'
84+
]);
85+
86+
$this->expectException(InvalidArgumentException::class);
87+
$this->expectExceptionMessage(
88+
'Every rover should have 2 lines: one with coordinates and one with actions. You have 1 lines'
89+
);
90+
91+
$this->commandTester->execute([]);
92+
}
93+
94+
public function testInvalidRoverXCoordinates(): void
95+
{
96+
$this->commandTester->setInputs([
97+
'5 5',
98+
"10 2 N\nMM"
99+
]);
100+
101+
$this->commandTester->execute([]);
102+
103+
$output = $this->commandTester->getDisplay();
104+
$this->assertStringContainsString('Error in the line #0', $output);
105+
}
106+
107+
public function testInvalidRoverYCoordinates(): void
108+
{
109+
$this->commandTester->setInputs([
110+
'5 5',
111+
"2 20 N\nMM"
112+
]);
113+
114+
$this->commandTester->execute([]);
115+
116+
$output = $this->commandTester->getDisplay();
117+
$this->assertStringContainsString('Error in the line #0', $output);
118+
}
119+
120+
public function testRoverLeftMars(): void
121+
{
122+
$this->commandTester->setInputs([
123+
'5 5',
124+
"4 4 N\nMM"
125+
]);
126+
127+
$this->commandTester->execute([]);
128+
129+
$output = $this->commandTester->getDisplay();
130+
$this->assertStringContainsString('Rover left Mars:', $output);
131+
}
132+
}

0 commit comments

Comments
 (0)