Skip to content

Seperate listeners and checks #285

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
May 17, 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
14 changes: 9 additions & 5 deletions src/Exercise/AbstractExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpSchool\PhpWorkshop\Exercise;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
Expand Down Expand Up @@ -78,12 +80,14 @@ public static function normaliseName(string $name): string
}

/**
* This method is implemented as empty by default, if you want to add additional checks or listen
* to events, you should override this method.
*
* @param ExerciseDispatcher $dispatcher
* @return list<class-string>
*/
public function configure(ExerciseDispatcher $dispatcher): void
public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
}
}
15 changes: 9 additions & 6 deletions src/Exercise/ExerciseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpSchool\PhpWorkshop\Exercise;

use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;

/**
Expand Down Expand Up @@ -34,14 +35,16 @@ public function getType(): ExerciseType;
public function getProblem(): string;

/**
* This is where the exercise specifies the extra checks it may require. It is also
* possible to grab the event dispatcher from the exercise dispatcher and listen to any
* events. This method is automatically invoked just before verifying/running an student's solution
* to an exercise.
* Subscribe to events triggered throughout the verification process
*/
public function defineListeners(EventDispatcher $dispatcher): void;

/**
* This is where the exercise specifies the extra checks it may require.
*
* @param ExerciseDispatcher $dispatcher
* @return array<class-string>
*/
public function configure(ExerciseDispatcher $dispatcher): void;
public function getRequiredChecks(): array;

/**
* A short description of the exercise.
Expand Down
8 changes: 4 additions & 4 deletions src/ExerciseDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public function requireCheck(string $requiredCheck): void
*/
public function verify(ExerciseInterface $exercise, Input $input): ResultAggregator
{
$exercise->configure($this);

$runner = $this->runnerManager->getRunner($exercise);

foreach ($runner->getRequiredChecks() as $requiredCheck) {
$exercise->defineListeners($this->eventDispatcher);

foreach ([...$runner->getRequiredChecks(), ...$exercise->getRequiredChecks()] as $requiredCheck) {
$this->requireCheck($requiredCheck);
}

Expand Down Expand Up @@ -181,7 +181,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
*/
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
{
$exercise->configure($this);
$exercise->defineListeners($this->eventDispatcher);

/** @var PhpLintCheck $lint */
$lint = $this->checkRepository->getByClass(PhpLintCheck::class);
Expand Down
9 changes: 8 additions & 1 deletion test/Asset/CgiExerciseImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand Down Expand Up @@ -62,7 +64,12 @@ public function getType(): ExerciseType
return ExerciseType::CGI();
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
}
}
9 changes: 8 additions & 1 deletion test/Asset/CliExerciseImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand Down Expand Up @@ -66,7 +68,12 @@ public function getType(): ExerciseType
return ExerciseType::CLI();
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
}
}
7 changes: 7 additions & 0 deletions test/Asset/CliExerciseMissingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand Down Expand Up @@ -31,4 +33,9 @@ public function getType(): ExerciseType
{
return ExerciseType::CLI();
}

public function getRequiredChecks(): array
{
return [];
}
}
9 changes: 7 additions & 2 deletions test/Asset/ComposerExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\ComposerCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
Expand Down Expand Up @@ -53,8 +54,12 @@ public function getType(): ExerciseType
return ExerciseType::CLI();
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getRequiredChecks(): array
{
return [ComposerCheck::class];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
$dispatcher->requireCheck(ComposerCheck::class);
}
}
14 changes: 10 additions & 4 deletions test/Asset/ExerciseWithInitialCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\ProvidesInitialCode;
Expand Down Expand Up @@ -41,13 +43,17 @@ public function getType(): ExerciseType
// TODO: Implement getType() method.
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getInitialCode(): SolutionInterface
{
// TODO: Implement configure() method.
return SingleFileSolution::fromFile(__DIR__ . '/initial-code/init-solution.php');
}

public function getInitialCode(): SolutionInterface
public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
return SingleFileSolution::fromFile(__DIR__ . '/initial-code/init-solution.php');
}
}
14 changes: 10 additions & 4 deletions test/Asset/FileComparisonExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\ComposerCheck;
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck;
Expand Down Expand Up @@ -66,13 +68,17 @@ public function getType(): ExerciseType
return ExerciseType::CLI();
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getFilesToCompare(): array
{
$dispatcher->requireCheck(ComposerCheck::class);
return $this->files;
}

public function getFilesToCompare(): array
public function getRequiredChecks(): array
{
return [FileComparisonCheck::class];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
return $this->files;
}
}
17 changes: 12 additions & 5 deletions test/Asset/FunctionRequirementsExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\ComposerCheck;
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
Expand Down Expand Up @@ -45,11 +48,6 @@ public function getType(): ExerciseType
return ExerciseType::CLI();
}

public function configure(ExerciseDispatcher $dispatcher): void
{
$dispatcher->requireCheck(ComposerCheck::class);
}

/**
* @return string[]
*/
Expand All @@ -65,4 +63,13 @@ public function getBannedFunctions(): array
{
return ['file'];
}

public function getRequiredChecks(): array
{
return [FunctionRequirementsCheck::class];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
}
}
10 changes: 8 additions & 2 deletions test/Asset/PatchableExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable;
Expand Down Expand Up @@ -45,8 +47,12 @@ public function getType(): ExerciseType
// TODO: Implement getType() method.
}

public function configure(ExerciseDispatcher $dispatcher): void
public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
// TODO: Implement configure() method.
}
}
16 changes: 11 additions & 5 deletions test/Asset/ProvidesSolutionExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
Expand All @@ -28,11 +30,6 @@ public function getProblem(): string
// TODO: Implement getProblem() method.
}

public function configure(ExerciseDispatcher $dispatcher): void
{
// TODO: Implement configure() method.
}

public function getDescription(): string
{
// TODO: Implement getDescription() method.
Expand All @@ -47,4 +44,13 @@ public function getSolution(): SolutionInterface
{
return SingleFileSolution::fromFile(__DIR__ . '/provided-solution/solution.php');
}

public function getRequiredChecks(): array
{
return [];
}

public function defineListeners(EventDispatcher $dispatcher): void
{
}
}
31 changes: 8 additions & 23 deletions test/Check/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ public function testIfPDOThrowsExceptionItCleansUp(): void

$this->exercise
->expects($this->once())
->method('configure')
->willReturnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireCheck(DatabaseCheck::class);
});
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);

$this->exercise
->expects($this->once())
Expand Down Expand Up @@ -172,10 +170,8 @@ public function testSuccessIsReturnedIfDatabaseVerificationPassed(): void

$this->exercise
->expects($this->once())
->method('configure')
->willReturnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireCheck(DatabaseCheck::class);
});
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);

$this->exercise
->expects($this->once())
Expand Down Expand Up @@ -207,13 +203,6 @@ public function testRunExercise(): void
->method('getArgs')
->willReturn([]);

$this->exercise
->expects($this->once())
->method('configure')
->willReturnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireCheck(DatabaseCheck::class);
});

$this->checkRepository->registerCheck($this->check);

$results = new ResultAggregator();
Expand Down Expand Up @@ -248,10 +237,8 @@ public function testFailureIsReturnedIfDatabaseVerificationFails(): void

$this->exercise
->expects($this->once())
->method('configure')
->willReturnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireCheck(DatabaseCheck::class);
});
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);

$this->exercise
->expects($this->once())
Expand Down Expand Up @@ -296,10 +283,8 @@ public function testAlteringDatabaseInSolutionDoesNotEffectDatabaseInUserSolutio

$this->exercise
->expects($this->once())
->method('configure')
->willReturnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireCheck(DatabaseCheck::class);
});
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);

$this->exercise
->expects($this->once())
Expand Down
Loading
Loading