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,10 @@ public function runExercise(string $submissionFile): void
8185 );
8286 }
8387
88+ if ($ exercise instanceof ComposerExerciseCheck) {
89+ $ this ->installDeps ($ exercise , dirname ($ submissionFileAbsolute ));
90+ }
91+
8492 $ input = new Input ($ this ->container ->get ('appName ' ), [
8593 'program ' => $ submissionFileAbsolute
8694 ]);
@@ -89,6 +97,21 @@ public function runExercise(string $submissionFile): void
8997 ->verify ($ exercise , $ input );
9098 }
9199
100+ /**
101+ * @param ExerciseInterface&ProvidesSolution $exercise
102+ * @param string $directory
103+ */
104+ private function installDeps (ExerciseInterface $ exercise , string $ directory ): void
105+ {
106+ if (file_exists ("$ directory/composer.json " ) && !file_exists ("$ directory/vendor " )) {
107+ $ process = new Process (
108+ [PrepareSolutionListener::locateComposer (), 'install ' , '--no-interaction ' ],
109+ $ directory
110+ );
111+ $ process ->run ();
112+ }
113+ }
114+
92115 public function assertVerifyWasSuccessful (): void
93116 {
94117 $ failures = (new Collection ($ this ->results ->getIterator ()->getArrayCopy ()))
0 commit comments