Skip to content

Commit 2e9c305

Browse files
committed
Listener to realpath program file - only in runners which need it
1 parent c27d9f4 commit 2e9c305

File tree

6 files changed

+100
-38
lines changed

6 files changed

+100
-38
lines changed

app/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
3131
use PhpSchool\PhpWorkshop\Listener\ConfigureCommandListener;
3232
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
33+
use PhpSchool\PhpWorkshop\Listener\RealPathListener;
3334
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
3435
use PhpSchool\PhpWorkshop\MenuItem\ResetProgress;
3536
use PhpSchool\PhpWorkshop\Output\OutputInterface;
@@ -197,6 +198,7 @@
197198
$c->get(RunnerManager::class)
198199
);
199200
},
201+
RealPathListener::class => object(),
200202

201203
//checks
202204
FileExistsCheck::class => object(),
@@ -281,6 +283,20 @@
281283
],
282284
'appContributors' => [],
283285
'eventListeners' => [
286+
'realpath-student-submission' => [
287+
'cli.verify.start' => [
288+
containerListener(RealPathListener::class)
289+
],
290+
'cli.run.start' => [
291+
containerListener(RealPathListener::class)
292+
],
293+
'cgi.verify.start' => [
294+
containerListener(RealPathListener::class)
295+
],
296+
'cgi.run.start' => [
297+
containerListener(RealPathListener::class)
298+
]
299+
],
284300
'check-exercise-assigned' => [
285301
'route.pre.resolve.args' => [
286302
containerListener(CheckExerciseAssignedListener::class)

src/Command/RunCommand.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,7 @@ public function __construct(
6161
*/
6262
public function __invoke(Input $input)
6363
{
64-
$program = $input->getArgument('program');
65-
if (!file_exists($program)) {
66-
$this->output->printError(
67-
sprintf('Could not run. File: "%s" does not exist', $program)
68-
);
69-
return 1;
70-
}
71-
$program = realpath($program);
7264
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
73-
$this->exerciseDispatcher->run($exercise, $program, $this->output);
65+
$this->exerciseDispatcher->run($exercise, $input, $this->output);
7466
}
7567
}

src/Command/VerifyCommand.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ public function __construct(
7979
*/
8080
public function __invoke(Input $input)
8181
{
82-
$program = $input->getArgument('program');
83-
if (!file_exists($program)) {
84-
$this->output->printError(
85-
sprintf('Could not verify. File: "%s" does not exist', $program)
86-
);
87-
return 1;
88-
}
89-
$program = realpath($program);
90-
9182
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
9283
$results = $this->exerciseDispatcher->verify($exercise, $input);
9384

src/Listener/RealPathListener.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Listener;
4+
5+
use PhpSchool\PhpWorkshop\CommandDefinition;
6+
use PhpSchool\PhpWorkshop\Event\Event;
7+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
8+
use PhpSchool\PhpWorkshop\Input\Input;
9+
use PhpSchool\PhpWorkshop\UserState;
10+
11+
/**
12+
* @author Aydin Hassan <aydin@hotmail.co.uk>
13+
*/
14+
class RealPathListener
15+
{
16+
17+
/**
18+
* @param ExerciseRunnerEvent $event
19+
*/
20+
public function __invoke(ExerciseRunnerEvent $event)
21+
{
22+
$program = $event->getInput()->getArgument('program');
23+
24+
if (file_exists($program)) {
25+
$event->getInput()->setArgument('program', realpath($program));
26+
}
27+
}
28+
}

test/Command/VerifyCommandTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ public function setUp()
4545
->will($this->returnValue('Some Check'));
4646
}
4747

48-
public function testVerifyPrintsErrorIfProgramDoesNotExist()
49-
{
50-
$repo = new ExerciseRepository([]);
51-
$state = new UserState;
52-
$output = $this->createMock(OutputInterface::class);
53-
$dispatcher = $this->createMock(ExerciseDispatcher::class);
54-
55-
$programFile = sprintf('%s/%s/program.php', sys_get_temp_dir(), $this->getName());
56-
$output
57-
->expects($this->once())
58-
->method('printError')
59-
->with(sprintf('Could not verify. File: "%s" does not exist', $programFile));
60-
61-
$serializer = $this->createMock(UserStateSerializer::class);
62-
$renderer = $this->createMock(ResultsRenderer::class);
63-
64-
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
65-
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $programFile])));
66-
}
67-
6848
public function testVerifyAddsCompletedExerciseAndReturnsCorrectCodeOnSuccess()
6949
{
7050
$file = tempnam(sys_get_temp_dir(), 'pws');

test/RealPathListenerTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest;
4+
5+
use PhpSchool\PhpWorkshop\Event\Event;
6+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshop\Listener\RealPathListener;
9+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
10+
use PHPUnit_Framework_TestCase;
11+
12+
/**
13+
* @author Aydin Hassan <aydin@hotmail.co.uk>
14+
*/
15+
class RealPathListenerTest extends PHPUnit_Framework_TestCase
16+
{
17+
public function testInputArgumentIsReplacesWithAbsolutePathIfFileExists()
18+
{
19+
$current = getcwd();
20+
21+
$tempDirectory = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
22+
mkdir($tempDirectory, 0777, true);
23+
chdir($tempDirectory);
24+
touch('test-file.php');
25+
26+
$exercise = new CliExerciseImpl;
27+
$input = new Input('app', ['program' => 'test-file.php']);
28+
$listener = new RealPathListener;
29+
$listener->__invoke(new ExerciseRunnerEvent('some.event', $exercise, $input));
30+
31+
$this->assertEquals(sprintf('%s/test-file.php', $tempDirectory), $input->getArgument('program'));
32+
33+
unlink('test-file.php');
34+
rmdir($tempDirectory);
35+
chdir($current);
36+
}
37+
38+
public function testInputArgumentIsLeftUnchangedIfFileDoesNotExist()
39+
{
40+
$exercise = new CliExerciseImpl;
41+
$input = new Input('app', ['program' => 'test-file.php']);
42+
$listener = new RealPathListener;
43+
$listener->__invoke(new ExerciseRunnerEvent('some.event', $exercise, $input));
44+
45+
$this->assertEquals('test-file.php', $input->getArgument('program'));
46+
}
47+
48+
private function runInDir($dir, callable $callback)
49+
{
50+
$current = getcwd();
51+
chdir($dir);
52+
$callback($dir);
53+
chdir($current);
54+
}
55+
}

0 commit comments

Comments
 (0)