Skip to content

Commit 9b72245

Browse files
committed
Custom Runner
1 parent 8a0a1ee commit 9b72245

File tree

8 files changed

+324
-0
lines changed

8 files changed

+324
-0
lines changed

app/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
2323
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CgiRunnerFactory;
2424
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CliRunnerFactory;
25+
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CustomRunnerFactory;
2526
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
2627
use PhpSchool\PhpWorkshop\Factory\EventDispatcherFactory;
2728
use PhpSchool\PhpWorkshop\Factory\MenuFactory;
@@ -125,6 +126,7 @@
125126
$manager = new RunnerManager;
126127
$manager->addFactory(new CliRunnerFactory($c->get(EventDispatcher::class)));
127128
$manager->addFactory(new CgiRunnerFactory($c->get(EventDispatcher::class)));
129+
$manager->addFactory(new CustomRunnerFactory);
128130
return $manager;
129131
},
130132

src/Exercise/CustomExercise.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise;
4+
5+
use PhpSchool\PhpWorkshop\Result\ResultInterface;
6+
7+
/**
8+
* @author Aydin Hassan <aydin@hotmail.co.uk>
9+
*/
10+
interface CustomExercise
11+
{
12+
/**
13+
* @return ResultInterface
14+
*/
15+
public function verify();
16+
}

src/Exercise/ExerciseType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ExerciseType extends Enum
2020
{
2121
const CLI = 'CLI';
2222
const CGI = 'CGI';
23+
const CUSTOM = 'CUSTOM';
24+
2325

2426
/**
2527
* Map of exercise types to the required interfaces exercises of that particular
@@ -30,6 +32,7 @@ class ExerciseType extends Enum
3032
private static $exerciseTypeToExerciseInterfaceMap = [
3133
self::CLI => CliExercise::class,
3234
self::CGI => CgiExercise::class,
35+
self::CUSTOM => CustomExercise::class,
3336
];
3437

3538
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\CustomExercise;
6+
use PhpSchool\PhpWorkshop\Input\Input;
7+
use PhpSchool\PhpWorkshop\Output\OutputInterface;
8+
use PhpSchool\PhpWorkshop\Result\ResultInterface;
9+
10+
/**
11+
* @author Aydin Hassan <aydin@hotmail.co.uk>
12+
*/
13+
class CustomRunner implements ExerciseRunnerInterface
14+
{
15+
/**
16+
* @var CustomExercise
17+
*/
18+
private $exercise;
19+
20+
/**
21+
* @param CustomExercise $exercise
22+
*/
23+
public function __construct(CustomExercise $exercise)
24+
{
25+
$this->exercise = $exercise;
26+
}
27+
28+
/**
29+
* Get the name of the exercise runner.
30+
*
31+
* @return string
32+
*/
33+
public function getName()
34+
{
35+
return 'External Runner';
36+
}
37+
38+
/**
39+
* Get an array of the class names of the required checks this runner needs.
40+
*
41+
* @return array
42+
*/
43+
public function getRequiredChecks()
44+
{
45+
return [];
46+
}
47+
48+
/**
49+
* Verify a solution to an exercise. Verification involves executing the reference solution
50+
* and the student's solution and comparing their output. If the output is the same
51+
* an instance of `PhpSchool\PhpWorkshop\Result\SuccessInterface` should be returned, if the output
52+
* is not the same, or something else went wrong then an instance of
53+
* `\PhpSchool\PhpWorkshop\Result\FailureInterface` should be returned.
54+
*
55+
* Other things that could go wrong include the student's solution returning a non-zero
56+
* exit code, or a notice/warning being exhibited.
57+
*
58+
* @param Input $input The command line arguments passed to the command.
59+
* @return ResultInterface The result of the check.
60+
*/
61+
public function verify(Input $input)
62+
{
63+
return $this->exercise->verify();
64+
}
65+
66+
/**
67+
* Run a solution to an exercise. This simply run's the student's solution with the correct input from the exercise
68+
* (such as the CLI arguments) and prints the output directly. This allows the student to have the environment
69+
* setup for them including getting a different set of arguments each time (if the exercise supports that).
70+
*
71+
* @param Input $input The command line arguments passed to the command.
72+
* @param OutputInterface $output A wrapper around STDOUT.
73+
* @return bool If the solution was successfully executed, eg. exit code was 0.
74+
*/
75+
public function run(Input $input, OutputInterface $output)
76+
{
77+
$message = 'Nothing to run here. This exercise does not require a code solution, ';
78+
$message .= 'so there is nothing to execute.';
79+
$output->writeLine($message);
80+
return true;
81+
}
82+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Factory;
4+
5+
use PhpSchool\PhpWorkshop\CommandArgument;
6+
use PhpSchool\PhpWorkshop\CommandDefinition;
7+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
9+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner;
11+
use PhpSchool\PhpWorkshop\ExerciseRunner\CustomRunner;
12+
use PhpSchool\PhpWorkshop\ExerciseRunner\ExerciseRunnerInterface;
13+
14+
/**
15+
* @author Aydin Hassan <aydin@hotmail.co.uk>
16+
*/
17+
class CustomRunnerFactory implements ExerciseRunnerFactoryInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private static $type = ExerciseType::CUSTOM;
23+
24+
/**
25+
* Whether the factory supports this exercise type.
26+
*
27+
* @param ExerciseInterface $exercise
28+
* @return bool
29+
*/
30+
public function supports(ExerciseInterface $exercise)
31+
{
32+
return $exercise->getType()->getValue() === self::$type;
33+
}
34+
35+
/**
36+
* Add any extra required arguments to the command.
37+
*
38+
* @param CommandDefinition $commandDefinition
39+
*/
40+
public function configureInput(CommandDefinition $commandDefinition)
41+
{
42+
}
43+
44+
/**
45+
* Create and return an instance of the runner.
46+
*
47+
* @param ExerciseInterface $exercise
48+
* @return ExerciseRunnerInterface
49+
*/
50+
public function create(ExerciseInterface $exercise)
51+
{
52+
return new CustomRunner($exercise);
53+
}
54+
}

test/Asset/ExtExerciseImpl.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Asset;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
6+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
7+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
8+
use PhpSchool\PhpWorkshop\Exercise\CustomExercise;
9+
use PhpSchool\PhpWorkshop\Result\ResultInterface;
10+
use PhpSchool\PhpWorkshop\Result\Success;
11+
12+
/**
13+
* @author Aydin Hassan <aydin@hotmail.co.uk>
14+
*/
15+
class ExtExerciseImpl extends AbstractExercise implements ExerciseInterface, CustomExercise
16+
{
17+
18+
/**
19+
* Get the name of the exercise, like `Hello World!`.
20+
*
21+
* @return string
22+
*/
23+
public function getName()
24+
{
25+
return 'EXT exercise';
26+
}
27+
28+
/**
29+
* A short description of the exercise.
30+
*
31+
* @return string
32+
*/
33+
public function getDescription()
34+
{
35+
return 'EXT exercise';
36+
}
37+
38+
/**
39+
* Return the type of exercise. This is an ENUM. See `PhpSchool\PhpWorkshop\Exercise\ExerciseType`.
40+
*
41+
* @return ExerciseType
42+
*/
43+
public function getType()
44+
{
45+
return ExerciseType::EXT();
46+
}
47+
48+
/**
49+
* @return ResultInterface
50+
*/
51+
public function verify()
52+
{
53+
return new Success('success');
54+
}
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner;
4+
5+
use Colors\Color;
6+
use PhpSchool\CliMenu\Terminal\TerminalInterface;
7+
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
8+
use PhpSchool\PhpWorkshop\Input\Input;
9+
use PhpSchool\PhpWorkshop\Output\StdOutput;
10+
use PhpSchool\PhpWorkshopTest\Asset\ExtExerciseImpl;
11+
use PHPUnit_Framework_TestCase;
12+
13+
/**
14+
* @author Aydin Hassan <aydin@hotmail.co.uk>
15+
*/
16+
class ExtRunnerTest extends PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var CustomRunner
20+
*/
21+
private $runner;
22+
23+
/**
24+
* @var ExtExerciseImpl
25+
*/
26+
private $exercise;
27+
28+
public function setUp()
29+
{
30+
$this->exercise = new ExtExerciseImpl;
31+
$this->runner = new CustomRunner($this->exercise);
32+
33+
$this->assertEquals('External Runner', $this->runner->getName());
34+
}
35+
36+
public function testRunOutputsErrorMessage()
37+
{
38+
$color = new Color;
39+
$color->setForceStyle(true);
40+
$output = new StdOutput($color, $this->createMock(TerminalInterface::class));
41+
42+
$exp = "Nothing to run here. This exercise does not require a code solution, ";
43+
$exp .= "so there is nothing to execute.\n";
44+
45+
$this->expectOutputString($exp);
46+
47+
$this->runner->run(new Input('app'), $output);
48+
}
49+
50+
public function testVerifyProxiesToExercise()
51+
{
52+
self::assertEquals($this->exercise->verify(), $this->runner->verify(new Input('app')));
53+
}
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\ExerciseRunner\Factory;
4+
5+
use PhpSchool\PhpWorkshop\CommandDefinition;
6+
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
7+
use PhpSchool\PhpWorkshop\Exercise\CustomExercise;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
9+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\CustomRunner;
11+
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CustomRunnerFactory;
12+
use PhpSchool\PhpWorkshopTest\Asset\ExtExerciseImpl;
13+
use PHPUnit_Framework_TestCase;
14+
15+
/**
16+
* @author Aydin Hassan <aydin@hotmail.co.uk>
17+
*/
18+
class CustomRunnerFactoryTest extends PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var CustomRunnerFactory
22+
*/
23+
private $factory;
24+
25+
public function setUp()
26+
{
27+
$this->factory = new CustomRunnerFactory;
28+
}
29+
30+
public function testSupports()
31+
{
32+
$exercise1 = $this->prophesize(ExerciseInterface::class);
33+
$exercise2 = $this->prophesize(ExerciseInterface::class);
34+
$exercise3 = $this->prophesize(ExerciseInterface::class);
35+
36+
$exercise1->getType()->willReturn(ExerciseType::CLI());
37+
$exercise2->getType()->willReturn(ExerciseType::CGI());
38+
$exercise3->getType()->willReturn(ExerciseType::CUSTOM());
39+
40+
$this->assertFalse($this->factory->supports($exercise1->reveal()));
41+
$this->assertFalse($this->factory->supports($exercise2->reveal()));
42+
$this->assertTrue($this->factory->supports($exercise3->reveal()));
43+
}
44+
45+
public function testConfigureInputAddsNoArgument()
46+
{
47+
$command = new CommandDefinition('my-command', [], 'var_dump');
48+
49+
$this->factory->configureInput($command);
50+
$this->assertCount(0, $command->getRequiredArgs());
51+
}
52+
53+
public function testCreateReturnsRunner()
54+
{
55+
$exercise = new ExtExerciseImpl;
56+
$this->assertInstanceOf(CustomRunner::class, $this->factory->create($exercise));
57+
}
58+
}

0 commit comments

Comments
 (0)