99use PhpSchool \PhpWorkshop \Exercise \AbstractExercise ;
1010use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
1111use PhpSchool \PhpWorkshop \Exercise \ExerciseType ;
12+ use PhpSchool \PhpWorkshop \Exercise \ProvidesSolution ;
13+ use PhpSchool \PhpWorkshop \ExerciseCheck \ComposerExerciseCheck ;
1214use PhpSchool \PhpWorkshop \ExerciseDispatcher ;
1315use PhpSchool \PhpWorkshop \ExerciseRepository ;
16+ use PhpSchool \PhpWorkshop \Listener \PrepareSolutionListener ;
1417use PhpSchool \PhpWorkshop \Result \Cgi \CgiResult ;
1518use PhpSchool \PhpWorkshop \Result \Cli \CliResult ;
1619use PhpSchool \PhpWorkshop \Result \Failure ;
2326use PHPUnit \Framework \TestCase ;
2427use Psr \Container \ContainerInterface ;
2528use PhpSchool \PhpWorkshop \Input \Input ;
29+ use Symfony \Component \Process \Process ;
2630
2731abstract class WorkshopExerciseTest extends TestCase
2832{
@@ -81,6 +85,11 @@ public function runExercise(string $submissionFile): void
8185 );
8286 }
8387
88+ if ($ exercise instanceof ComposerExerciseCheck) {
89+ $ this ->installDeps ($ exercise , dirname ($ submissionFileAbsolute ));
90+ }
91+
92+
8493 $ input = new Input ($ this ->container ->get ('appName ' ), [
8594 'program ' => $ submissionFileAbsolute
8695 ]);
@@ -89,6 +98,21 @@ public function runExercise(string $submissionFile): void
8998 ->verify ($ exercise , $ input );
9099 }
91100
101+ /**
102+ * @param ExerciseInterface&ProvidesSolution $exercise
103+ * @param string $directory
104+ */
105+ private function installDeps (ExerciseInterface $ exercise , string $ directory ): void
106+ {
107+ if (file_exists ("$ directory/composer.json " ) && !file_exists ("$ directory/vendor " )) {
108+ $ process = new Process (
109+ [PrepareSolutionListener::locateComposer (), 'install ' , '--no-interaction ' ],
110+ $ directory
111+ );
112+ $ process ->run ();
113+ }
114+ }
115+
92116 public function assertVerifyWasSuccessful (): void
93117 {
94118 $ failures = (new Collection ($ this ->results ->getIterator ()->getArrayCopy ()))
0 commit comments