Skip to content

Commit 844483f

Browse files
committed
Update command signature and fix tests
1 parent 489701b commit 844483f

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/Command/RunCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
66
use PhpSchool\PhpWorkshop\ExerciseRepository;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Output\OutputInterface;
89
use PhpSchool\PhpWorkshop\UserState;
910
use PhpSchool\PhpWorkshop\UserStateSerializer;
@@ -62,13 +63,13 @@ public function __construct(
6263
}
6364

6465
/**
65-
* @param string $appName
66-
* @param string $program
66+
* @param Input $input The command line arguments passed to the command.
6767
*
6868
* @return int|void
6969
*/
70-
public function __invoke($appName, $program)
70+
public function __invoke(Input $input)
7171
{
72+
$program = $input->getArgument('program');
7273
if (!file_exists($program)) {
7374
$this->output->printError(
7475
sprintf('Could not run. File: "%s" does not exist', $program)

src/Command/VerifyCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
66
use PhpSchool\PhpWorkshop\ExerciseRepository;
77
use PhpSchool\PhpWorkshop\ExerciseRunner;
8+
use PhpSchool\PhpWorkshop\Input\Input;
89
use PhpSchool\PhpWorkshop\Output\OutputInterface;
910
use PhpSchool\PhpWorkshop\ResultRenderer\ResultsRenderer;
1011
use PhpSchool\PhpWorkshop\UserState;
@@ -72,13 +73,13 @@ public function __construct(
7273
}
7374

7475
/**
75-
* @param string $appName
76-
* @param string $program
76+
* @param Input $input The command line arguments passed to the command.
7777
*
7878
* @return int|void
7979
*/
80-
public function __invoke($appName, $program)
80+
public function __invoke(Input $input)
8181
{
82+
$program = $input->getArgument('program');
8283
if (!file_exists($program)) {
8384
$this->output->printError(
8485
sprintf('Could not verify. File: "%s" does not exist', $program)
@@ -93,7 +94,7 @@ public function __invoke($appName, $program)
9394
}
9495

9596
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
96-
$results = $this->exerciseDispatcher->verify($exercise, $program);
97+
$results = $this->exerciseDispatcher->verify($exercise, $input);
9798

9899
if ($results->isSuccessful()) {
99100
$this->userState->addCompletedExercise($exercise->getName());

test/Command/VerifyCommandTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpSchool\CliMenu\Terminal\TerminalInterface;
77
use PhpSchool\PhpWorkshop\Check\CheckInterface;
88
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
9+
use PhpSchool\PhpWorkshop\Input\Input;
910
use PhpSchool\PhpWorkshop\Output\OutputInterface;
1011
use PhpSchool\PhpWorkshop\Output\StdOutput;
1112
use PHPUnit_Framework_TestCase;
@@ -58,7 +59,7 @@ public function testVerifyPrintsErrorIfProgramDoesNotExist()
5859
$renderer = $this->createMock(ResultsRenderer::class);
5960

6061
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
61-
$this->assertSame(1, $command->__invoke('appname', $programFile));
62+
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $programFile])));
6263
}
6364

6465
public function testVerifyPrintsErrorIfNoExerciseAssigned()
@@ -80,7 +81,7 @@ public function testVerifyPrintsErrorIfNoExerciseAssigned()
8081
$renderer = $this->createMock(ResultsRenderer::class);
8182

8283
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
83-
$this->assertSame(1, $command->__invoke('appname', $file));
84+
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $file])));
8485

8586
unlink($file);
8687
}
@@ -90,6 +91,8 @@ public function testVerifyAddsCompletedExerciseAndReturnsCorrectCodeOnSuccess()
9091
$file = tempnam(sys_get_temp_dir(), 'pws');
9192
touch($file);
9293

94+
$input = new Input('appName', ['program' => $file]);
95+
9396
$e = $this->createMock(ExerciseInterface::class);
9497
$e->expects($this->any())
9598
->method('getName')
@@ -117,17 +120,17 @@ public function testVerifyAddsCompletedExerciseAndReturnsCorrectCodeOnSuccess()
117120
$dispatcher
118121
->expects($this->once())
119122
->method('verify')
120-
->with($e, $file)
123+
->with($e, $input)
121124
->will($this->returnValue($results));
122125

123126
$renderer
124127
->expects($this->once())
125128
->method('render')
126129
->with($results, $e, $state, $output);
127-
130+
128131

129132
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
130-
$this->assertEquals(0, $command->__invoke('appname', $file));
133+
$this->assertEquals(0, $command->__invoke($input));
131134
$this->assertEquals(['exercise1'], $state->getCompletedExercises());
132135
unlink($file);
133136
}
@@ -137,6 +140,8 @@ public function testVerifyDoesNotAddCompletedExerciseAndReturnsCorrectCodeOnFail
137140
$file = tempnam(sys_get_temp_dir(), 'pws');
138141
touch($file);
139142

143+
$input = new Input('appName', ['program' => $file]);
144+
140145
$e = $this->createMock(ExerciseInterface::class);
141146
$e->expects($this->any())
142147
->method('getName')
@@ -165,7 +170,7 @@ public function testVerifyDoesNotAddCompletedExerciseAndReturnsCorrectCodeOnFail
165170
$dispatcher
166171
->expects($this->once())
167172
->method('verify')
168-
->with($e, $file)
173+
->with($e, $input)
169174
->will($this->returnValue($results));
170175

171176
$renderer
@@ -174,7 +179,7 @@ public function testVerifyDoesNotAddCompletedExerciseAndReturnsCorrectCodeOnFail
174179
->with($results, $e, $state, $output);
175180

176181
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
177-
$this->assertEquals(1, $command->__invoke('appname', $file));
182+
$this->assertEquals(1, $command->__invoke($input));
178183
$this->assertEquals([], $state->getCompletedExercises());
179184
unlink($file);
180185
}

0 commit comments

Comments
 (0)