diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 71c68b2f..adcff9df 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -4,10 +4,12 @@ namespace PhpSchool\PhpWorkshop\Command; +use PhpSchool\PhpWorkshop\Check\FileExistsCheck; use PhpSchool\PhpWorkshop\ExerciseDispatcher; use PhpSchool\PhpWorkshop\ExerciseRepository; use PhpSchool\PhpWorkshop\Input\Input; use PhpSchool\PhpWorkshop\Output\OutputInterface; +use PhpSchool\PhpWorkshop\Result\Success; use PhpSchool\PhpWorkshop\UserState; /** @@ -60,6 +62,11 @@ public function __construct( */ public function __invoke(Input $input): void { + if (!file_exists($input->getRequiredArgument('program'))) { + $this->output->printError(sprintf('File: "%s" does not exist', $input->getRequiredArgument('program'))); + return; + } + $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->exerciseDispatcher->run($exercise, $input, $this->output); } diff --git a/test/Command/RunCommandTest.php b/test/Command/RunCommandTest.php index 0c08a787..73de9544 100644 --- a/test/Command/RunCommandTest.php +++ b/test/Command/RunCommandTest.php @@ -3,6 +3,8 @@ namespace PhpSchool\PhpWorkshopTest\Command; use Colors\Color; +use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; +use PhpSchool\PhpWorkshopTest\BaseTest; use PhpSchool\Terminal\Terminal; use PhpSchool\PhpWorkshop\Command\RunCommand; use PhpSchool\PhpWorkshop\ExerciseDispatcher; @@ -13,11 +15,11 @@ use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl; use PHPUnit\Framework\TestCase; -class RunCommandTest extends TestCase +class RunCommandTest extends BaseTest { public function test(): void { - $input = new Input('appName', ['program' => 'solution.php']); + $input = new Input('appName', ['program' => $this->getTemporaryFile('solution.php')]); $exercise = new CliExerciseImpl(); $repo = new ExerciseRepository([$exercise]); @@ -37,4 +39,28 @@ public function test(): void $command = new RunCommand($repo, $dispatcher, $state, $output); $command->__invoke($input); } + + public function testWithNonExistingFile(): void + { + $input = new Input('appName', ['program' => 'solution.php']); + + $exercise = new CliExerciseImpl(); + $repo = new ExerciseRepository([$exercise]); + + $state = new UserState(); + $state->setCurrentExercise('my-exercise'); + $color = new Color(); + $color->setForceStyle(true); + $output = new StdOutput($color, $this->createMock(Terminal::class)); + + $dispatcher = $this->createMock(ExerciseDispatcher::class); + $dispatcher + ->expects($this->never()) + ->method('run'); + + $this->expectOutputString(file_get_contents(__DIR__ . '/../res/app-run-missing-solution-expected.txt')); + + $command = new RunCommand($repo, $dispatcher, $state, $output); + $command->__invoke($input); + } } diff --git a/test/res/app-run-missing-solution-expected.txt b/test/res/app-run-missing-solution-expected.txt new file mode 100644 index 00000000..25c86feb --- /dev/null +++ b/test/res/app-run-missing-solution-expected.txt @@ -0,0 +1,5 @@ + +   +  File: "solution.php" does not exist  +   +