Skip to content

Commit 57f5e9a

Browse files
authored
Merge pull request #221 from php-school/composer-install-for-test-solutions
Install composer deps for test solutions
2 parents 738dd5c + a9a5caa commit 57f5e9a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Listener/PrepareSolutionListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PrepareSolutionListener
1919
*
2020
* @var array<string>
2121
*/
22-
private $composerLocations = [
22+
private static $composerLocations = [
2323
'composer',
2424
'composer.phar',
2525
'/usr/local/bin/composer',
@@ -45,7 +45,7 @@ public function __invoke(ExerciseRunnerEvent $event): void
4545

4646
if (!file_exists(sprintf('%s/vendor', $solution->getBaseDirectory()))) {
4747
$process = new Process(
48-
[$this->locateComposer(), 'install', '--no-interaction'],
48+
[self::locateComposer(), 'install', '--no-interaction'],
4949
$solution->getBaseDirectory()
5050
);
5151
$process->run();
@@ -56,9 +56,9 @@ public function __invoke(ExerciseRunnerEvent $event): void
5656
/**
5757
* @return string
5858
*/
59-
private function locateComposer(): string
59+
public static function locateComposer(): string
6060
{
61-
foreach ($this->composerLocations as $location) {
61+
foreach (self::$composerLocations as $location) {
6262
if (file_exists($location) && is_executable($location)) {
6363
return $location;
6464
}

src/TestUtils/WorkshopExerciseTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1111
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
12+
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
13+
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
1214
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
1315
use PhpSchool\PhpWorkshop\ExerciseRepository;
16+
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
1417
use PhpSchool\PhpWorkshop\Result\Cgi\CgiResult;
1518
use PhpSchool\PhpWorkshop\Result\Cli\CliResult;
1619
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -23,6 +26,7 @@
2326
use PHPUnit\Framework\TestCase;
2427
use Psr\Container\ContainerInterface;
2528
use PhpSchool\PhpWorkshop\Input\Input;
29+
use Symfony\Component\Process\Process;
2630

2731
abstract 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()))

test/Listener/PrepareSolutionListenerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function setUp(): void
4444
touch($this->file);
4545
}
4646

47+
/**
48+
* @runInSeparateProcess
49+
*/
4750
public function testIfSolutionRequiresComposerButComposerCannotBeLocatedExceptionIsThrown(): void
4851
{
4952
$refProp = new ReflectionProperty(PrepareSolutionListener::class, 'composerLocations');

0 commit comments

Comments
 (0)