Skip to content

Lint exercises before running #269

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 1 commit into from
Jan 25, 2024
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
28 changes: 28 additions & 0 deletions src/Exception/CouldNotRunException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Exception;

use PhpSchool\PhpWorkshop\Result\FailureInterface;

class CouldNotRunException extends RuntimeException
{
private FailureInterface $failure;

public function __construct(FailureInterface $failure)
{
$this->failure = $failure;
parent::__construct('Could not run exercise');
}

public static function fromFailure(FailureInterface $failure): self
{
return new self($failure);
}

public function getFailure(): FailureInterface
{
return $this->failure;
}
}
12 changes: 12 additions & 0 deletions src/ExerciseDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
Expand All @@ -16,6 +17,8 @@
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\FailureInterface;
use PhpSchool\PhpWorkshop\Exception\CouldNotRunException;

/**
* This class is used to verify/run a student's solution to an exercise. It routes to the correct
Expand Down Expand Up @@ -179,6 +182,15 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
{
$exercise->configure($this);

/** @var PhpLintCheck $lint */
$lint = $this->checkRepository->getByClass(PhpLintCheck::class);
$result = $lint->check($exercise, $input);

if ($result instanceof FailureInterface) {
throw CouldNotRunException::fromFailure($result);
}

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $exercise, $input));

try {
Expand Down
3 changes: 2 additions & 1 deletion test/ExerciseDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpSchool\PhpWorkshop\Check\CheckInterface;
use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\EventInterface;
Expand Down Expand Up @@ -538,7 +539,7 @@ public function testRun(): void
$runnerManager,
new ResultAggregator(),
new EventDispatcher(new ResultAggregator()),
new CheckRepository()
new CheckRepository([new PhpLintCheck()])
);

$this->assertTrue($exerciseDispatcher->run($exercise, $input, $output));
Expand Down