Skip to content

ExerciseRunners now depend on a concrete implementation of ExerciseIn… #69

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 24, 2016
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/ExerciseDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function verify(ExerciseInterface $exercise, $fileName)
{
$exercise->configure($this);

$runner = $this->runnerFactory->create($exercise->getType(), $this->eventDispatcher);
$runner = $this->runnerFactory->create($exercise, $this->eventDispatcher);
$this->eventDispatcher->dispatch(new Event('verify.start', compact('exercise', 'fileName')));

$this->validateChecks($this->checksToRunBefore, $exercise);
Expand All @@ -149,7 +149,7 @@ public function verify(ExerciseInterface $exercise, $fileName)
$this->eventDispatcher->dispatch(new Event('verify.pre.execute', compact('exercise', 'fileName')));

try {
$this->results->add($runner->verify($exercise, $fileName));
$this->results->add($runner->verify($fileName));
} finally {
$this->eventDispatcher->dispatch(new Event('verify.post.execute', compact('exercise', 'fileName')));
}
Expand All @@ -176,8 +176,8 @@ public function run(ExerciseInterface $exercise, $fileName, OutputInterface $out
$this->eventDispatcher->dispatch(new Event('run.start', compact('exercise', 'fileName')));

$exitStatus = $this->runnerFactory
->create($exercise->getType(), $this->eventDispatcher)
->run($exercise, $fileName, $output);
->create($exercise, $this->eventDispatcher)
->run($fileName, $output);

$this->eventDispatcher->dispatch(new Event('run.finish', compact('exercise', 'fileName')));
return $exitStatus;
Expand Down
48 changes: 16 additions & 32 deletions src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\CgiOutFailure;
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
Expand All @@ -29,17 +27,24 @@
class CgiRunner implements ExerciseRunnerInterface
{

/**
* @var CgiExercise
*/
private $exercise;

/**
* @var EventDispatcher
*/
private $eventDispatcher;

/**
* @param CgiExercise $exercise
* @param EventDispatcher $eventDispatcher
*/
public function __construct(EventDispatcher $eventDispatcher)
public function __construct(CgiExercise $exercise, EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->exercise = $exercise;
}

/**
Expand All @@ -51,17 +56,16 @@ public function getName()
}

/**
* @param ExerciseInterface $exercise
* @param RequestInterface $request
* @param string $fileName
* @return ResultInterface
*/
private function checkRequest(ExerciseInterface $exercise, RequestInterface $request, $fileName)
private function checkRequest(RequestInterface $request, $fileName)
{
try {
$event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.verify.solution-execute.pre', $request));
$solutionResponse = $this->executePhpFile(
$exercise->getSolution()->getEntryPoint(),
$this->exercise->getSolution()->getEntryPoint(),
$event->getRequest(),
'solution'
);
Expand Down Expand Up @@ -164,37 +168,31 @@ private function getProcess($fileName, RequestInterface $request)
}

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @return ResultInterface
*/
public function verify(ExerciseInterface $exercise, $fileName)
public function verify($fileName)
{
$this->validateExercise($exercise);

return new CgiOutResult(
$this->getName(),
array_map(
function (RequestInterface $request) use ($exercise, $fileName) {
return $this->checkRequest($exercise, $request, $fileName);
function (RequestInterface $request) use ($fileName) {
return $this->checkRequest($request, $fileName);
},
$exercise->getRequests()
$this->exercise->getRequests()
)
);
}

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @param OutputInterface $output
* @return bool
*/
public function run(ExerciseInterface $exercise, $fileName, OutputInterface $output)
public function run($fileName, OutputInterface $output)
{
$this->validateExercise($exercise);

$success = true;
foreach ($exercise->getRequests() as $i => $request) {
foreach ($this->exercise->getRequests() as $i => $request) {
$event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.usr-execute.pre', $request));
$process = $this->getProcess($fileName, $event->getRequest());

Expand All @@ -208,18 +206,4 @@ public function run(ExerciseInterface $exercise, $fileName, OutputInterface $out
}
return $success;
}

/**
* @param ExerciseInterface $exercise
*/
private function validateExercise(ExerciseInterface $exercise)
{
if ($exercise->getType()->getValue() !== ExerciseType::CGI) {
throw new \InvalidArgumentException;
}

if (!$exercise instanceof CgiExercise) {
throw new \InvalidArgumentException;
}
}
}
29 changes: 13 additions & 16 deletions src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck;
Expand All @@ -25,18 +26,24 @@
*/
class CliRunner implements ExerciseRunnerInterface
{
/**
* @var CliExercise
*/
private $exercise;

/**
* @var EventDispatcher
*/
private $eventDispatcher;

/**
* @param CliExercise $exercise
* @param EventDispatcher $eventDispatcher
*/
public function __construct(EventDispatcher $eventDispatcher)
public function __construct(CliExercise $exercise, EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->exercise = $exercise;
}

/**
Expand Down Expand Up @@ -81,23 +88,18 @@ private function getPhpProcess($fileName, ArrayObject $args)
}

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @return ResultInterface
*/
public function verify(ExerciseInterface $exercise, $fileName)
public function verify($fileName)
{
if ($exercise->getType()->getValue() !== ExerciseType::CLI) {
throw new \InvalidArgumentException;
}

//arrays are not pass-by-ref
$args = new ArrayObject($exercise->getArgs());
$args = new ArrayObject($this->exercise->getArgs());

try {
$event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.verify.solution-execute.pre', $args));
$solutionOutput = $this->executePhpFile(
$exercise->getSolution()->getEntryPoint(),
$this->exercise->getSolution()->getEntryPoint(),
$event->getArgs(),
'solution'
);
Expand All @@ -121,19 +123,14 @@ public function verify(ExerciseInterface $exercise, $fileName)
}

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @param OutputInterface $output
* @return bool
*/
public function run(ExerciseInterface $exercise, $fileName, OutputInterface $output)
public function run($fileName, OutputInterface $output)
{
if ($exercise->getType()->getValue() !== ExerciseType::CLI) {
throw new \InvalidArgumentException;
}

$event = $this->eventDispatcher->dispatch(
new CliExecuteEvent('cli.run.user-execute.pre', new ArrayObject($exercise->getArgs()))
new CliExecuteEvent('cli.run.user-execute.pre', new ArrayObject($this->exercise->getArgs()))
);

$process = $this->getPhpProcess($fileName, $event->getArgs());
Expand Down
6 changes: 2 additions & 4 deletions src/ExerciseRunner/ExerciseRunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ interface ExerciseRunnerInterface
public function getName();

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @return ResultInterface
*/
public function verify(ExerciseInterface $exercise, $fileName);
public function verify($fileName);

/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @param OutputInterface $output
* @return bool
*/
public function run(ExerciseInterface $exercise, $fileName, OutputInterface $output);
public function run($fileName, OutputInterface $output);
}
15 changes: 9 additions & 6 deletions src/Factory/RunnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Interop\Container\ContainerInterface;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner;
use PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner;
Expand All @@ -18,19 +19,21 @@
class RunnerFactory
{
/**
* @param ExerciseType $exerciseType
* @param ExerciseInterface $exercise
* @param EventDispatcher $eventDispatcher
* @return ExerciseRunnerInterface
*/
public function create(ExerciseType $exerciseType, EventDispatcher $eventDispatcher)
public function create(ExerciseInterface $exercise, EventDispatcher $eventDispatcher)
{
switch ($exerciseType->getValue()) {
switch ($exercise->getType()->getValue()) {
case ExerciseType::CLI:
return new CliRunner($eventDispatcher);
return new CliRunner($exercise, $eventDispatcher);
case ExerciseType::CGI:
return new CgiRunner($eventDispatcher);
return new CgiRunner($exercise, $eventDispatcher);
}

throw new InvalidArgumentException(sprintf('Exercise Type: "%s" not supported', $exerciseType->getValue()));
throw new InvalidArgumentException(
sprintf('Exercise Type: "%s" not supported', $exercise->getType()->getValue())
);
}
}
37 changes: 16 additions & 21 deletions test/ExerciseDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,25 @@ private function createExercise()
$this->exercise = $this->getMock(ExerciseInterface::class);
$this->solution = $this->getMock(SolutionInterface::class);

$this->exerciseType = ExerciseType::CLI();
$this->exercise
->expects($this->atLeastOnce())
->method('getType')
->will($this->returnValue($this->exerciseType));

$this->exercise
->expects($this->any())
->method('getName')
->will($this->returnValue('Some Exercise'));

$this->exerciseType = new ExerciseType(ExerciseType::CLI);

$this->exercise
->expects($this->any())
->method('getType')
->will($this->returnValue($this->exerciseType));
}

private function mockRunner(ExerciseType $exerciseType = null)
private function mockRunner(ExerciseInterface $exercise = null)
{
$this->runnerFactory
->expects($this->once())
->method('create')
->with($exerciseType ? $exerciseType : $this->exerciseType, $this->eventDispatcher)
->with($exercise ? $exercise : $this->exercise, $this->eventDispatcher)
->will($this->returnValue($this->runner));
}

Expand Down Expand Up @@ -275,7 +276,7 @@ public function testVerify()
$this->runner
->expects($this->once())
->method('verify')
->with($this->exercise, $this->file)
->with($this->file)
->will($this->returnValue($this->getMock(SuccessInterface::class)));

$this->exerciseDispatcher->requireCheck(get_class($this->check), ExerciseDispatcher::CHECK_BEFORE);
Expand Down Expand Up @@ -313,7 +314,7 @@ public function testVerifyOnlyRunsRequiredChecks()
$this->runner
->expects($this->once())
->method('verify')
->with($this->exercise, $this->file)
->with($this->file)
->will($this->returnValue($this->getMock(SuccessInterface::class)));

$this->checkRepository->registerCheck($doNotRunMe);
Expand All @@ -326,7 +327,7 @@ public function testVerifyOnlyRunsRequiredChecks()
$this->assertTrue($result->isSuccessful());
}

public function testWhenBeforeChecksFailTheyReturnImmediatelyEarly()
public function testWhenBeforeChecksFailTheyReturnImmediately()
{
$this->createExercise();
$this->check
Expand Down Expand Up @@ -389,7 +390,7 @@ public function testAllEventsAreDispatched()
$this->runner
->expects($this->once())
->method('verify')
->with($this->exercise, $this->file)
->with($this->file)
->will($this->returnValue(new Success('test')));

$this->exerciseDispatcher->verify($this->exercise, $this->file);
Expand All @@ -411,7 +412,7 @@ public function testVerifyPostExecuteIsStillDispatchedEvenIfRunnerThrowsExceptio
$this->runner
->expects($this->once())
->method('verify')
->with($this->exercise, $this->file)
->with($this->file)
->will($this->throwException(new RuntimeException));

$this->setExpectedException(RuntimeException::class);
Expand All @@ -423,17 +424,11 @@ public function testRun()
$exercise = $this->getMock(ExerciseInterface::class);
$output = $this->getMock(OutputInterface::class);

$exerciseType = ExerciseType::CLI();
$exercise
->expects($this->atLeastOnce())
->method('getType')
->will($this->returnValue($exerciseType));

$this->mockRunner($exerciseType);
$this->mockRunner($exercise);
$this->runner
->expects($this->once())
->method('run')
->with($exercise, $this->file, $output)
->with($this->file, $output)
->will($this->returnValue(true));

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