Skip to content

Install composer deps for test solutions #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Listener/PrepareSolutionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PrepareSolutionListener
*
* @var array<string>
*/
private $composerLocations = [
private static $composerLocations = [
'composer',
'composer.phar',
'/usr/local/bin/composer',
Expand All @@ -45,7 +45,7 @@ public function __invoke(ExerciseRunnerEvent $event): void

if (!file_exists(sprintf('%s/vendor', $solution->getBaseDirectory()))) {
$process = new Process(
[$this->locateComposer(), 'install', '--no-interaction'],
[self::locateComposer(), 'install', '--no-interaction'],
$solution->getBaseDirectory()
);
$process->run();
Expand All @@ -56,9 +56,9 @@ public function __invoke(ExerciseRunnerEvent $event): void
/**
* @return string
*/
private function locateComposer(): string
public static function locateComposer(): string
{
foreach ($this->composerLocations as $location) {
foreach (self::$composerLocations as $location) {
if (file_exists($location) && is_executable($location)) {
return $location;
}
Expand Down
23 changes: 23 additions & 0 deletions src/TestUtils/WorkshopExerciseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\Result\Cgi\CgiResult;
use PhpSchool\PhpWorkshop\Result\Cli\CliResult;
use PhpSchool\PhpWorkshop\Result\Failure;
Expand All @@ -23,6 +26,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use PhpSchool\PhpWorkshop\Input\Input;
use Symfony\Component\Process\Process;

abstract class WorkshopExerciseTest extends TestCase
{
Expand Down Expand Up @@ -81,6 +85,10 @@ public function runExercise(string $submissionFile): void
);
}

if ($exercise instanceof ComposerExerciseCheck) {
$this->installDeps($exercise, dirname($submissionFileAbsolute));
}

$input = new Input($this->container->get('appName'), [
'program' => $submissionFileAbsolute
]);
Expand All @@ -89,6 +97,21 @@ public function runExercise(string $submissionFile): void
->verify($exercise, $input);
}

/**
* @param ExerciseInterface&ProvidesSolution $exercise
* @param string $directory
*/
private function installDeps(ExerciseInterface $exercise, string $directory): void
{
if (file_exists("$directory/composer.json") && !file_exists("$directory/vendor")) {
$process = new Process(
[PrepareSolutionListener::locateComposer(), 'install', '--no-interaction'],
$directory
);
$process->run();
}
}

public function assertVerifyWasSuccessful(): void
{
$failures = (new Collection($this->results->getIterator()->getArrayCopy()))
Expand Down
3 changes: 3 additions & 0 deletions test/Listener/PrepareSolutionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function setUp(): void
touch($this->file);
}

/**
* @runInSeparateProcess
*/
public function testIfSolutionRequiresComposerButComposerCannotBeLocatedExceptionIsThrown(): void
{
$refProp = new ReflectionProperty(PrepareSolutionListener::class, 'composerLocations');
Expand Down