Skip to content

Custom runner #141

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 7 commits into from
Nov 28, 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
10 changes: 6 additions & 4 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CgiRunnerFactory;
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CliRunnerFactory;
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CustomVerifyingRunnerFactory;
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
use PhpSchool\PhpWorkshop\Factory\EventDispatcherFactory;
use PhpSchool\PhpWorkshop\Factory\MenuFactory;
Expand Down Expand Up @@ -125,6 +126,7 @@
$manager = new RunnerManager;
$manager->addFactory(new CliRunnerFactory($c->get(EventDispatcher::class)));
$manager->addFactory(new CgiRunnerFactory($c->get(EventDispatcher::class)));
$manager->addFactory(new CustomVerifyingRunnerFactory);
return $manager;
},

Expand Down Expand Up @@ -326,25 +328,25 @@
containerListener(CodePatchListener::class, 'patch'),
],
'cli.verify.finish' => [
containerListener(CodePatchListener::class, 'patch'),
containerListener(CodePatchListener::class, 'revert'),
],
'cli.run.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cli.run.finish' => [
containerListener(CodePatchListener::class, 'patch'),
containerListener(CodePatchListener::class, 'revert'),
],
'cgi.verify.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.verify.finish' => [
containerListener(CodePatchListener::class, 'patch'),
containerListener(CodePatchListener::class, 'revert'),
],
'cgi.run.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.run.finish' => [
containerListener(CodePatchListener::class, 'patch'),
containerListener(CodePatchListener::class, 'revert'),
],
],
'self-check' => [
Expand Down
16 changes: 16 additions & 0 deletions src/Exercise/CustomVerifyingExercise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpSchool\PhpWorkshop\Exercise;

use PhpSchool\PhpWorkshop\Result\ResultInterface;

/**
* @author Aydin Hassan <[email protected]>
*/
interface CustomVerifyingExercise
{
/**
* @return ResultInterface
*/
public function verify();
}
3 changes: 3 additions & 0 deletions src/Exercise/ExerciseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ExerciseType extends Enum
{
const CLI = 'CLI';
const CGI = 'CGI';
const CUSTOM = 'CUSTOM';


/**
* Map of exercise types to the required interfaces exercises of that particular
Expand All @@ -30,6 +32,7 @@ class ExerciseType extends Enum
private static $exerciseTypeToExerciseInterfaceMap = [
self::CLI => CliExercise::class,
self::CGI => CgiExercise::class,
self::CUSTOM => CustomVerifyingExercise::class,
];

/**
Expand Down
75 changes: 75 additions & 0 deletions src/ExerciseRunner/CustomVerifyingRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace PhpSchool\PhpWorkshop\ExerciseRunner;

use PhpSchool\PhpWorkshop\Exercise\CustomVerifyingExercise;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\ResultInterface;

/**
* @author Aydin Hassan <[email protected]>
*/
class CustomVerifyingRunner implements ExerciseRunnerInterface
{
/**
* @var CustomVerifyingExercise
*/
private $exercise;

/**
* @param CustomVerifyingExercise $exercise
*/
public function __construct(CustomVerifyingExercise $exercise)
{
$this->exercise = $exercise;
}

/**
* Get the name of the exercise runner.
*
* @return string
*/
public function getName()
{
return 'Custom Verifying Runner';
}

/**
* Get an array of the class names of the required checks this runner needs.
*
* @return array
*/
public function getRequiredChecks()
{
return [];
}

/**
* Delegate to the exercise for verifying. Verifying could mean checking that a program was installed or that some
* other arbitrary task was performed.
*
* @param Input $input The command line arguments passed to the command.
* @return ResultInterface The result of the check.
*/
public function verify(Input $input)
{
return $this->exercise->verify();
}

/**
* Running a custom verifying exercise does nothing. There is no program required, therefore there is nothing
* to run.
*
* @param Input $input The command line arguments passed to the command.
* @param OutputInterface $output A wrapper around STDOUT.
* @return bool If the solution was successfully executed, eg. exit code was 0.
*/
public function run(Input $input, OutputInterface $output)
{
$message = 'Nothing to run here. This exercise does not require a code solution, ';
$message .= 'so there is nothing to execute.';
$output->writeLine($message);
return true;
}
}
51 changes: 51 additions & 0 deletions src/ExerciseRunner/Factory/CustomVerifyingRunnerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace PhpSchool\PhpWorkshop\ExerciseRunner\Factory;

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseRunner\CustomVerifyingRunner;
use PhpSchool\PhpWorkshop\ExerciseRunner\ExerciseRunnerInterface;

/**
* @author Aydin Hassan <[email protected]>
*/
class CustomVerifyingRunnerFactory implements ExerciseRunnerFactoryInterface
{
/**
* @var string
*/
private static $type = ExerciseType::CUSTOM;

/**
* Whether the factory supports this exercise type.
*
* @param ExerciseInterface $exercise
* @return bool
*/
public function supports(ExerciseInterface $exercise)
{
return $exercise->getType()->getValue() === self::$type;
}

/**
* Add any extra required arguments to the command.
*
* @param CommandDefinition $commandDefinition
*/
public function configureInput(CommandDefinition $commandDefinition)
{
}

/**
* Create and return an instance of the runner.
*
* @param ExerciseInterface $exercise
* @return ExerciseRunnerInterface
*/
public function create(ExerciseInterface $exercise)
{
return new CustomVerifyingRunner($exercise);
}
}
55 changes: 55 additions & 0 deletions test/Asset/CustomVerifyingExerciseImpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\CustomVerifyingExercise;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\Result\Success;

/**
* @author Aydin Hassan <[email protected]>
*/
class CustomVerifyingExerciseImpl extends AbstractExercise implements ExerciseInterface, CustomVerifyingExercise
{

/**
* Get the name of the exercise, like `Hello World!`.
*
* @return string
*/
public function getName()
{
return 'Custom Verifying exercise';
}

/**
* A short description of the exercise.
*
* @return string
*/
public function getDescription()
{
return 'Custom Verifying exercise';
}

/**
* Return the type of exercise. This is an ENUM. See `PhpSchool\PhpWorkshop\Exercise\ExerciseType`.
*
* @return ExerciseType
*/
public function getType()
{
return ExerciseType::CUSTOM();
}

/**
* @return ResultInterface
*/
public function verify()
{
return new Success('success');
}
}
58 changes: 58 additions & 0 deletions test/ExerciseRunner/CustomVerifyingRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace PhpSchool\PhpWorkshop\ExerciseRunner;

use Colors\Color;
use PhpSchool\CliMenu\Terminal\TerminalInterface;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\StdOutput;
use PhpSchool\PhpWorkshopTest\Asset\CustomVerifyingExerciseImpl;
use PHPUnit_Framework_TestCase;

/**
* @author Aydin Hassan <[email protected]>
*/
class ExtRunnerTest extends PHPUnit_Framework_TestCase
{
/**
* @var CustomVerifyingRunner
*/
private $runner;

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

public function setUp()
{
$this->exercise = new CustomVerifyingExerciseImpl;
$this->runner = new CustomVerifyingRunner($this->exercise);

$this->assertEquals('Custom Verifying Runner', $this->runner->getName());
}

public function testRequiredChecks()
{
$this->assertEquals([], $this->runner->getRequiredChecks());
}

public function testRunOutputsErrorMessage()
{
$color = new Color;
$color->setForceStyle(true);
$output = new StdOutput($color, $this->createMock(TerminalInterface::class));

$exp = 'Nothing to run here. This exercise does not require a code solution, ';
$exp .= "so there is nothing to execute.\n";

$this->expectOutputString($exp);

$this->runner->run(new Input('app'), $output);
}

public function testVerifyProxiesToExercise()
{
self::assertEquals($this->exercise->verify(), $this->runner->verify(new Input('app')));
}
}
2 changes: 0 additions & 2 deletions test/ExerciseRunner/Factory/CliRunnerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner;
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CliRunnerFactory;
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseInterface;
use PHPUnit_Framework_TestCase;

/**
Expand Down
57 changes: 57 additions & 0 deletions test/ExerciseRunner/Factory/CustomVerifyingRunnerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PhpSchool\PhpWorkshopTest\ExerciseRunner\Factory;

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Exercise\CustomVerifyingExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseRunner\CustomVerifyingRunner;
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CustomVerifyingRunnerFactory;
use PhpSchool\PhpWorkshopTest\Asset\CustomVerifyingExerciseImpl;
use PHPUnit_Framework_TestCase;

/**
* @author Aydin Hassan <[email protected]>
*/
class CustomRunnerFactoryTest extends PHPUnit_Framework_TestCase
{
/**
* @var CustomVerifyingRunnerFactory
*/
private $factory;

public function setUp()
{
$this->factory = new CustomVerifyingRunnerFactory;
}

public function testSupports()
{
$exercise1 = $this->prophesize(ExerciseInterface::class);
$exercise2 = $this->prophesize(ExerciseInterface::class);
$exercise3 = $this->prophesize(ExerciseInterface::class);

$exercise1->getType()->willReturn(ExerciseType::CLI());
$exercise2->getType()->willReturn(ExerciseType::CGI());
$exercise3->getType()->willReturn(ExerciseType::CUSTOM());

$this->assertFalse($this->factory->supports($exercise1->reveal()));
$this->assertFalse($this->factory->supports($exercise2->reveal()));
$this->assertTrue($this->factory->supports($exercise3->reveal()));
}

public function testConfigureInputAddsNoArgument()
{
$command = new CommandDefinition('my-command', [], 'var_dump');

$this->factory->configureInput($command);
$this->assertCount(0, $command->getRequiredArgs());
}

public function testCreateReturnsRunner()
{
$exercise = new CustomVerifyingExerciseImpl;
$this->assertInstanceOf(CustomVerifyingRunner::class, $this->factory->create($exercise));
}
}