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