Skip to content

Commit f92e2c8

Browse files
authored
Merge pull request #231 from php-school/run-check-file-exists
Check solution file exists in run command
2 parents 27f65b6 + cb0a456 commit f92e2c8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Command/RunCommand.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace PhpSchool\PhpWorkshop\Command;
66

7+
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
78
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
89
use PhpSchool\PhpWorkshop\ExerciseRepository;
910
use PhpSchool\PhpWorkshop\Input\Input;
1011
use PhpSchool\PhpWorkshop\Output\OutputInterface;
12+
use PhpSchool\PhpWorkshop\Result\Success;
1113
use PhpSchool\PhpWorkshop\UserState;
1214

1315
/**
@@ -60,6 +62,11 @@ public function __construct(
6062
*/
6163
public function __invoke(Input $input): void
6264
{
65+
if (!file_exists($input->getRequiredArgument('program'))) {
66+
$this->output->printError(sprintf('File: "%s" does not exist', $input->getRequiredArgument('program')));
67+
return;
68+
}
69+
6370
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
6471
$this->exerciseDispatcher->run($exercise, $input, $this->output);
6572
}

test/Command/RunCommandTest.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PhpSchool\PhpWorkshopTest\Command;
44

55
use Colors\Color;
6+
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
7+
use PhpSchool\PhpWorkshopTest\BaseTest;
68
use PhpSchool\Terminal\Terminal;
79
use PhpSchool\PhpWorkshop\Command\RunCommand;
810
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
@@ -13,11 +15,11 @@
1315
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
1416
use PHPUnit\Framework\TestCase;
1517

16-
class RunCommandTest extends TestCase
18+
class RunCommandTest extends BaseTest
1719
{
1820
public function test(): void
1921
{
20-
$input = new Input('appName', ['program' => 'solution.php']);
22+
$input = new Input('appName', ['program' => $this->getTemporaryFile('solution.php')]);
2123

2224
$exercise = new CliExerciseImpl();
2325
$repo = new ExerciseRepository([$exercise]);
@@ -37,4 +39,28 @@ public function test(): void
3739
$command = new RunCommand($repo, $dispatcher, $state, $output);
3840
$command->__invoke($input);
3941
}
42+
43+
public function testWithNonExistingFile(): void
44+
{
45+
$input = new Input('appName', ['program' => 'solution.php']);
46+
47+
$exercise = new CliExerciseImpl();
48+
$repo = new ExerciseRepository([$exercise]);
49+
50+
$state = new UserState();
51+
$state->setCurrentExercise('my-exercise');
52+
$color = new Color();
53+
$color->setForceStyle(true);
54+
$output = new StdOutput($color, $this->createMock(Terminal::class));
55+
56+
$dispatcher = $this->createMock(ExerciseDispatcher::class);
57+
$dispatcher
58+
->expects($this->never())
59+
->method('run');
60+
61+
$this->expectOutputString(file_get_contents(__DIR__ . '/../res/app-run-missing-solution-expected.txt'));
62+
63+
$command = new RunCommand($repo, $dispatcher, $state, $output);
64+
$command->__invoke($input);
65+
}
4066
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
 
3+
 File: "solution.php" does not exist 
4+
 
5+

0 commit comments

Comments
 (0)