9
9
use PhpSchool \PhpWorkshop \Exercise \AbstractExercise ;
10
10
use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
11
11
use PhpSchool \PhpWorkshop \Exercise \ExerciseType ;
12
+ use PhpSchool \PhpWorkshop \Exercise \ProvidesSolution ;
13
+ use PhpSchool \PhpWorkshop \ExerciseCheck \ComposerExerciseCheck ;
12
14
use PhpSchool \PhpWorkshop \ExerciseDispatcher ;
13
15
use PhpSchool \PhpWorkshop \ExerciseRepository ;
16
+ use PhpSchool \PhpWorkshop \Listener \PrepareSolutionListener ;
14
17
use PhpSchool \PhpWorkshop \Result \Cgi \CgiResult ;
15
18
use PhpSchool \PhpWorkshop \Result \Cli \CliResult ;
16
19
use PhpSchool \PhpWorkshop \Result \Failure ;
23
26
use PHPUnit \Framework \TestCase ;
24
27
use Psr \Container \ContainerInterface ;
25
28
use PhpSchool \PhpWorkshop \Input \Input ;
29
+ use Symfony \Component \Process \Process ;
26
30
27
31
abstract class WorkshopExerciseTest extends TestCase
28
32
{
@@ -81,6 +85,10 @@ public function runExercise(string $submissionFile): void
81
85
);
82
86
}
83
87
88
+ if ($ exercise instanceof ComposerExerciseCheck) {
89
+ $ this ->installDeps ($ exercise , dirname ($ submissionFileAbsolute ));
90
+ }
91
+
84
92
$ input = new Input ($ this ->container ->get ('appName ' ), [
85
93
'program ' => $ submissionFileAbsolute
86
94
]);
@@ -89,6 +97,21 @@ public function runExercise(string $submissionFile): void
89
97
->verify ($ exercise , $ input );
90
98
}
91
99
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
+
92
115
public function assertVerifyWasSuccessful (): void
93
116
{
94
117
$ failures = (new Collection ($ this ->results ->getIterator ()->getArrayCopy ()))
0 commit comments