Skip to content

Commit 489701b

Browse files
committed
Update exercise runner signature and fix tests
1 parent 9e6d113 commit 489701b

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

src/ExerciseRunner/CgiRunner.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1313
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
1414
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
15+
use PhpSchool\PhpWorkshop\Input\Input;
1516
use PhpSchool\PhpWorkshop\Output\OutputInterface;
1617
use PhpSchool\PhpWorkshop\Result\CgiOutFailure;
1718
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
@@ -233,16 +234,16 @@ private function getProcess($fileName, RequestInterface $request)
233234
* * cgi.verify.student.executing
234235
* * cgi.verify.student-execute.fail (if the student's solution fails to execute)
235236
*
236-
* @param string $fileName The absolute path to the student's solution.
237+
* @param Input $input The command line arguments passed to the command.
237238
* @return ResultInterface The result of the check.
238239
*/
239-
public function verify($fileName)
240+
public function verify(Input $input)
240241
{
241242
return new CgiOutResult(
242243
$this->getName(),
243244
array_map(
244-
function (RequestInterface $request) use ($fileName) {
245-
return $this->checkRequest($request, $fileName);
245+
function (RequestInterface $request) use ($input) {
246+
return $this->checkRequest($request, $input->getArgument('program'));
246247
},
247248
$this->exercise->getRequests()
248249
)
@@ -262,18 +263,18 @@ function (RequestInterface $request) use ($fileName) {
262263
* * cgi.run.student-execute.pre
263264
* * cgi.run.student.executing
264265
*
265-
* @param string $fileName The absolute path to the student's solution.
266+
* @param Input $input The command line arguments passed to the command.
266267
* @param OutputInterface $output A wrapper around STDOUT.
267268
* @return bool If the solution was successfully executed, eg. exit code was 0.
268269
*/
269-
public function run($fileName, OutputInterface $output)
270+
public function run(Input $input, OutputInterface $output)
270271
{
271272
$success = true;
272273
foreach ($this->exercise->getRequests() as $i => $request) {
273274
$event = $this->eventDispatcher->dispatch(
274275
new CgiExecuteEvent('cgi.run.student-execute.pre', $request)
275276
);
276-
$process = $this->getProcess($fileName, $event->getRequest());
277+
$process = $this->getProcess($input->getArgument('program'), $event->getRequest());
277278

278279
$output->writeTitle("Request");
279280
$output->emptyLine();

src/ExerciseRunner/CliRunner.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1313
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
1414
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
15+
use PhpSchool\PhpWorkshop\Input\Input;
1516
use PhpSchool\PhpWorkshop\Output\OutputInterface;
1617
use PhpSchool\PhpWorkshop\Result\Failure;
1718
use PhpSchool\PhpWorkshop\Result\ResultInterface;
@@ -124,10 +125,10 @@ private function getPhpProcess($fileName, ArrayObject $args)
124125
* * cli.verify.student.executing
125126
* * cli.verify.student-execute.fail (if the student's solution fails to execute)
126127
*
127-
* @param string $fileName The absolute path to the student's solution.
128+
* @param Input $input The command line arguments passed to the command.
128129
* @return ResultInterface The result of the check.
129130
*/
130-
public function verify($fileName)
131+
public function verify(Input $input)
131132
{
132133
//arrays are not pass-by-ref
133134
$args = new ArrayObject($this->exercise->getArgs());
@@ -146,7 +147,7 @@ public function verify($fileName)
146147

147148
try {
148149
$event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.verify.student-execute.pre', $args));
149-
$userOutput = $this->executePhpFile($fileName, $event->getArgs(), 'student');
150+
$userOutput = $this->executePhpFile($input->getArgument('program'), $event->getArgs(), 'student');
150151
} catch (CodeExecutionException $e) {
151152
$this->eventDispatcher->dispatch(new Event('cli.verify.student-execute.fail', ['exception' => $e]));
152153
return Failure::fromNameAndCodeExecutionFailure($this->getName(), $e);
@@ -170,11 +171,11 @@ public function verify($fileName)
170171
* * cli.run.student-execute.pre
171172
* * cli.run.student.executing
172173
*
173-
* @param string $fileName The absolute path to the student's solution.
174+
* @param Input $input The command line arguments passed to the command.
174175
* @param OutputInterface $output A wrapper around STDOUT.
175176
* @return bool If the solution was successfully executed, eg. exit code was 0.
176177
*/
177-
public function run($fileName, OutputInterface $output)
178+
public function run(Input $input, OutputInterface $output)
178179
{
179180
/** @var CliExecuteEvent $event */
180181
$event = $this->eventDispatcher->dispatch(
@@ -192,7 +193,7 @@ public function run($fileName, OutputInterface $output)
192193
}
193194

194195
$output->writeTitle("Output");
195-
$process = $this->getPhpProcess($fileName, $args);
196+
$process = $this->getPhpProcess($input->getArgument('program'), $args);
196197
$process->start();
197198
$this->eventDispatcher->dispatch(
198199
new CliExecuteEvent('cli.run.student.executing', $args, ['output' => $output])

src/ExerciseRunner/ExerciseRunnerInterface.php

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

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Output\OutputInterface;
89
use PhpSchool\PhpWorkshop\Result\ResultInterface;
910

@@ -42,19 +43,19 @@ public function configure(ExerciseDispatcher $exerciseDispatcher);
4243
* Other things that could go wrong include the student's solution returning a non-zero
4344
* exit code, or a notice/warning being exhibited.
4445
*
45-
* @param string $fileName The absolute path to the student's solution.
46+
* @param Input $input The command line arguments passed to the command.
4647
* @return ResultInterface The result of the check.
4748
*/
48-
public function verify($fileName);
49+
public function verify(Input $input);
4950

5051
/**
5152
* Run a solution to an exercise. This simply run's the student's solution with the correct input from the exercise
5253
* (such as the CLI arguments) and prints the output directly. This allows the student to have the environment
5354
* setup for them including getting a different set of arguments each time (if the exercise supports that).
5455
*
55-
* @param string $fileName The absolute path to the student's solution.
56+
* @param Input $input The command line arguments passed to the command.
5657
* @param OutputInterface $output A wrapper around STDOUT.
5758
* @return bool If the solution was successfully executed, eg. exit code was 0.
5859
*/
59-
public function run($fileName, OutputInterface $output);
60+
public function run(Input $input, OutputInterface $output);
6061
}

test/ExerciseRunner/CgiRunnerTest.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
88
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10+
use PhpSchool\PhpWorkshop\Input\Input;
1011
use PhpSchool\PhpWorkshop\Output\StdOutput;
1112
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
1213
use PhpSchool\PhpWorkshop\Result\CgiOutResult;
@@ -66,7 +67,7 @@ public function testVerifyThrowsExceptionIfSolutionFailsExecution()
6667
$regex = "/^PHP Code failed to execute\\. Error: \"PHP Parse error: syntax error, unexpected end of file in/";
6768
$this->expectException(SolutionExecutionException::class);
6869
$this->expectExceptionMessageRegExp($regex);
69-
$this->runner->verify('');
70+
$this->runner->verify(new Input('app', ['program' => '']));
7071
}
7172

7273
public function testVerifyReturnsSuccessIfGetSolutionOutputMatchesUserOutput()
@@ -88,7 +89,7 @@ public function testVerifyReturnsSuccessIfGetSolutionOutputMatchesUserOutput()
8889

8990
$this->assertInstanceOf(
9091
CgiOutResult::class,
91-
$this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-solution.php'))
92+
$this->runner->verify(new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/get-solution.php')]))
9293
);
9394
}
9495

@@ -114,7 +115,7 @@ public function testVerifyReturnsSuccessIfPostSolutionOutputMatchesUserOutput()
114115

115116
$this->assertInstanceOf(
116117
CgiOutResult::class,
117-
$this->runner->verify(realpath(__DIR__ . '/../res/cgi/post-solution.php'))
118+
$this->runner->verify(new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/post-solution.php')]))
118119
);
119120
}
120121

@@ -138,7 +139,10 @@ public function testVerifyReturnsSuccessIfPostSolutionOutputMatchesUserOutputWit
138139
->method('getRequests')
139140
->will($this->returnValue([$request]));
140141

141-
$result = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/post-multiple-solution.php'));
142+
$result = $this->runner->verify(
143+
new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/post-multiple-solution.php')])
144+
);
145+
142146
$this->assertInstanceOf(CgiOutResult::class, $result);
143147
}
144148

@@ -159,7 +163,10 @@ public function testVerifyReturnsFailureIfUserSolutionFailsToExecute()
159163
->method('getRequests')
160164
->will($this->returnValue([$request]));
161165

162-
$failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/user-error.php'));
166+
$failure = $this->runner->verify(
167+
new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/user-error.php')])
168+
);
169+
163170
$this->assertInstanceOf(CgiOutResult::class, $failure);
164171
$this->assertCount(1, $failure);
165172

@@ -188,7 +195,10 @@ public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput()
188195
->method('getRequests')
189196
->will($this->returnValue([$request]));
190197

191-
$failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-user-wrong.php'));
198+
$failure = $this->runner->verify(
199+
new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/get-user-wrong.php')])
200+
);
201+
192202
$this->assertInstanceOf(CgiOutResult::class, $failure);
193203
$this->assertCount(1, $failure);
194204

@@ -217,7 +227,9 @@ public function testVerifyReturnsFailureIfSolutionOutputHeadersDoesNotMatchUserO
217227
->method('getRequests')
218228
->will($this->returnValue([$request]));
219229

220-
$failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-user-header-wrong.php'));
230+
$failure = $this->runner->verify(
231+
new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/get-user-header-wrong.php')])
232+
);
221233

222234
$this->assertInstanceOf(CgiOutResult::class, $failure);
223235
$this->assertCount(1, $failure);
@@ -283,7 +295,11 @@ public function testRunPassesOutputAndReturnsSuccessIfAllRequestsAreSuccessful()
283295

284296
$this->expectOutputString($exp);
285297

286-
$success = $this->runner->run(realpath(__DIR__ . '/../res/cgi/get-solution.php'), $output);
298+
$success = $this->runner->run(
299+
new Input('app', ['program' => realpath(__DIR__ . '/../res/cgi/get-solution.php')]),
300+
$output
301+
);
302+
287303
$this->assertTrue($success);
288304
}
289305

@@ -315,7 +331,10 @@ public function testRunPassesOutputAndReturnsFailureIfARequestFails()
315331

316332
$this->expectOutputString($exp);
317333

318-
$success = $this->runner->run('', $output);
334+
$success = $this->runner->run(
335+
new Input('app', ['program' => '']),
336+
$output
337+
);
319338
$this->assertFalse($success);
320339
}
321340
}

test/ExerciseRunner/CliRunnerTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
99
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11+
use PhpSchool\PhpWorkshop\Input\Input;
1112
use PhpSchool\PhpWorkshop\Output\StdOutput;
1213
use PhpSchool\PhpWorkshop\Result\Failure;
1314
use PhpSchool\PhpWorkshop\Result\StdOutFailure;
@@ -62,7 +63,7 @@ public function testVerifyThrowsExceptionIfSolutionFailsExecution()
6263
$regex .= ", expecting ',' or ';'/";
6364
$this->expectException(SolutionExecutionException::class);
6465
$this->expectExceptionMessageRegExp($regex);
65-
$this->runner->verify('');
66+
$this->runner->verify(new Input('app', ['program' => '']));
6667
}
6768

6869
public function testVerifyReturnsSuccessIfSolutionOutputMatchesUserOutput()
@@ -80,7 +81,7 @@ public function testVerifyReturnsSuccessIfSolutionOutputMatchesUserOutput()
8081

8182
$this->assertInstanceOf(
8283
Success::class,
83-
$this->runner->verify(__DIR__ . '/../res/cli/user.php')
84+
$this->runner->verify(new Input('app', ['program' => __DIR__ . '/../res/cli/user.php']))
8485
);
8586
}
8687

@@ -97,7 +98,7 @@ public function testVerifyReturnsFailureIfUserSolutionFailsToExecute()
9798
->method('getArgs')
9899
->will($this->returnValue([1, 2, 3]));
99100

100-
$failure = $this->runner->verify(__DIR__ . '/../res/cli/user-error.php');
101+
$failure = $this->runner->verify(new Input('app', ['program' => __DIR__ . '/../res/cli/user-error.php']));
101102

102103
$failureMsg = "/^PHP Code failed to execute. Error: \"PHP Parse error: syntax error, ";
103104
$failureMsg .= "unexpected end of file, expecting ',' or ';'/";
@@ -119,7 +120,7 @@ public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput()
119120
->method('getArgs')
120121
->will($this->returnValue([1, 2, 3]));
121122

122-
$failure = $this->runner->verify(__DIR__ . '/../res/cli/user-wrong.php');
123+
$failure = $this->runner->verify(new Input('app', ['program' => __DIR__ . '/../res/cli/user-wrong.php']));
123124

124125
$this->assertInstanceOf(StdOutFailure::class, $failure);
125126
$this->assertEquals('6', $failure->getExpectedOutput());
@@ -143,7 +144,7 @@ public function testRunPassesOutputAndReturnsSuccessIfScriptIsSuccessful()
143144

144145
$this->expectOutputString($exp);
145146

146-
$success = $this->runner->run(__DIR__ . '/../res/cli/user.php', $output);
147+
$success = $this->runner->run(new Input('app', ['program' => __DIR__ . '/../res/cli/user.php']), $output);
147148
$this->assertTrue($success);
148149
}
149150

@@ -158,7 +159,7 @@ public function testRunPassesOutputAndReturnsFailureIfScriptFails()
158159

159160
$this->expectOutputRegex('/PHP Parse error: syntax error, unexpected end of file, expecting \',\' or \';\' /');
160161

161-
$success = $this->runner->run(__DIR__ . '/../res/cli/user-error.php', $output);
162+
$success = $this->runner->run(new Input('app', ['program' => __DIR__ . '/../res/cli/user-error.php']), $output);
162163
$this->assertFalse($success);
163164
}
164165
}

0 commit comments

Comments
 (0)