Skip to content

Commit 513f518

Browse files
committed
Runner tests
1 parent ef18d66 commit 513f518

File tree

6 files changed

+334
-4
lines changed

6 files changed

+334
-4
lines changed

test/Asset/CgiExerciseImpl.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Asset;
4+
5+
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
6+
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
7+
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
9+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10+
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
11+
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
12+
use Psr\Http\Message\RequestInterface;
13+
14+
/**
15+
* @package PhpSchool\PhpWorkshopTest\Asset
16+
* @author Aydin Hassan <aydin@hotmail.co.uk>
17+
*/
18+
class CgiExerciseImpl implements ExerciseInterface, CgiExercise
19+
{
20+
21+
/**
22+
* @var string
23+
*/
24+
private $name;
25+
26+
/**
27+
* @param string $name
28+
*/
29+
public function __construct($name = 'my-exercise')
30+
{
31+
$this->name = $name;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getName()
38+
{
39+
return $this->name;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getDescription()
46+
{
47+
return $this->name;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getSolution()
54+
{
55+
// TODO: Implement getSolution() method.
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getProblem()
62+
{
63+
// TODO: Implement getProblem() method.
64+
}
65+
66+
/**
67+
* @return void
68+
*/
69+
public function tearDown()
70+
{
71+
// TODO: Implement tearDown() method.
72+
}
73+
74+
/**
75+
* This method should return an array of PSR-7 requests, which will be forwarded to the student's
76+
* solution.
77+
*
78+
* @return RequestInterface[] An array of PSR-7 requests.
79+
*/
80+
public function getRequests()
81+
{
82+
// TODO: Implement getRequests() method.
83+
}
84+
85+
/**
86+
* @return ExerciseType
87+
*/
88+
public function getType()
89+
{
90+
return ExerciseType::CGI();
91+
}
92+
93+
/**
94+
* @param ExerciseDispatcher $dispatcher
95+
*/
96+
public function configure(ExerciseDispatcher $dispatcher)
97+
{
98+
}
99+
}

test/ExerciseRunner/CgiRunnerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22

3-
namespace PhpSchool\PhpWorkshop\ExerciseRunner;
3+
namespace PhpSchool\PhpWorkshopTest\ExerciseRunner;
44

55
use Colors\Color;
66
use PhpSchool\CliMenu\Terminal\TerminalInterface;
7+
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
8+
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
9+
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
710
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
811
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
912
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
13+
use PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner;
1014
use PhpSchool\PhpWorkshop\Input\Input;
1115
use PhpSchool\PhpWorkshop\Output\StdOutput;
1216
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
@@ -20,7 +24,6 @@
2024
use Zend\Diactoros\Uri;
2125

2226
/**
23-
* Class CgiRunnerTest
2427
* @package PhpSchool\PhpWorkshop\ExerciseRunner
2528
* @author Aydin Hassan <aydin@hotmail.co.uk>
2629
*/
@@ -47,6 +50,17 @@ public function setUp()
4750
$this->assertEquals('CGI Program Runner', $this->runner->getName());
4851
}
4952

53+
public function testRequiredChecks()
54+
{
55+
$requiredChecks = [
56+
FileExistsCheck::class,
57+
PhpLintCheck::class,
58+
CodeParseCheck::class,
59+
];
60+
61+
$this->assertEquals($requiredChecks, $this->runner->getRequiredChecks());
62+
}
63+
5064
public function testVerifyThrowsExceptionIfSolutionFailsExecution()
5165
{
5266
$solution = SingleFileSolution::fromFile(__DIR__ . '/../res/cgi/solution-error.php');

test/ExerciseRunner/CliRunnerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3-
namespace PhpSchool\PhpWorkshop\ExerciseRunner;
3+
namespace PhpSchool\PhpWorkshopTest\ExerciseRunner;
44

55
use Colors\Color;
66
use InvalidArgumentException;
77
use PhpSchool\CliMenu\Terminal\TerminalInterface;
8+
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
9+
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
10+
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
811
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
912
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
1013
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
14+
use PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner;
1115
use PhpSchool\PhpWorkshop\Input\Input;
1216
use PhpSchool\PhpWorkshop\Output\StdOutput;
1317
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -19,7 +23,6 @@
1923
use PHPUnit_Framework_TestCase;
2024

2125
/**
22-
* Class CliRunnerTest
2326
* @package PhpSchool\PhpWorkshop\ExerciseRunner
2427
* @author Aydin Hassan <aydin@hotmail.co.uk>
2528
*/
@@ -46,6 +49,17 @@ public function setUp()
4649
$this->assertEquals('CLI Program Runner', $this->runner->getName());
4750
}
4851

52+
public function testRequiredChecks()
53+
{
54+
$requiredChecks = [
55+
FileExistsCheck::class,
56+
PhpLintCheck::class,
57+
CodeParseCheck::class,
58+
];
59+
60+
$this->assertEquals($requiredChecks, $this->runner->getRequiredChecks());
61+
}
62+
4963
public function testVerifyThrowsExceptionIfSolutionFailsExecution()
5064
{
5165
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution-error.php'));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\ExerciseInterface;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
9+
use PhpSchool\PhpWorkshop\ExerciseRunner\CgiRunner;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CgiRunnerFactory;
11+
use PhpSchool\PhpWorkshopTest\Asset\CgiExerciseImpl;
12+
use PHPUnit_Framework_TestCase;
13+
14+
/**
15+
* @author Aydin Hassan <aydin@hotmail.co.uk>
16+
*/
17+
class CgiRunnerFactoryTest extends PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var EventDispatcher
21+
*/
22+
private $eventDispatcher;
23+
24+
/**
25+
* @var CgiRunnerFactory
26+
*/
27+
private $factory;
28+
29+
public function setUp()
30+
{
31+
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
32+
$this->factory = new CgiRunnerFactory($this->eventDispatcher);
33+
}
34+
35+
public function testSupports()
36+
{
37+
$exercise1 = $this->prophesize(ExerciseInterface::class);
38+
$exercise2 = $this->prophesize(ExerciseInterface::class);
39+
40+
$exercise1->getType()->willReturn(ExerciseType::CGI());
41+
$exercise2->getType()->willReturn(ExerciseType::CLI());
42+
43+
$this->assertTrue($this->factory->supports($exercise1->reveal()));
44+
$this->assertFalse($this->factory->supports($exercise2->reveal()));
45+
}
46+
47+
public function testConfigureInputAddsProgramArgument()
48+
{
49+
$command = new CommandDefinition('my-command', [], 'var_dump');
50+
51+
$this->factory->configureInput($command);
52+
53+
$this->assertCount(1, $command->getRequiredArgs());
54+
$this->assertSame('program', $command->getRequiredArgs()[0]->getName());
55+
$this->assertTrue($command->getRequiredArgs()[0]->isRequired());
56+
}
57+
58+
public function testCreateReturnsRunner()
59+
{
60+
$exercise = new CgiExerciseImpl;
61+
$this->assertInstanceOf(CgiRunner::class, $this->factory->create($exercise));
62+
}
63+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\CliExercise;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
9+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10+
use PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner;
11+
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CliRunnerFactory;
12+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
13+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseInterface;
14+
use PHPUnit_Framework_TestCase;
15+
16+
/**
17+
* @author Aydin Hassan <aydin@hotmail.co.uk>
18+
*/
19+
class CliRunnerFactoryTest extends PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var EventDispatcher
23+
*/
24+
private $eventDispatcher;
25+
26+
/**
27+
* @var CliRunnerFactory
28+
*/
29+
private $factory;
30+
31+
public function setUp()
32+
{
33+
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
34+
$this->factory = new CliRunnerFactory($this->eventDispatcher);
35+
}
36+
37+
public function testSupports()
38+
{
39+
$exercise1 = $this->prophesize(ExerciseInterface::class);
40+
$exercise2 = $this->prophesize(ExerciseInterface::class);
41+
42+
$exercise1->getType()->willReturn(ExerciseType::CLI());
43+
$exercise2->getType()->willReturn(ExerciseType::CGI());
44+
45+
$this->assertTrue($this->factory->supports($exercise1->reveal()));
46+
$this->assertFalse($this->factory->supports($exercise2->reveal()));
47+
}
48+
49+
public function testConfigureInputAddsProgramArgument()
50+
{
51+
$command = new CommandDefinition('my-command', [], 'var_dump');
52+
53+
$this->factory->configureInput($command);
54+
55+
$this->assertCount(1, $command->getRequiredArgs());
56+
$this->assertSame('program', $command->getRequiredArgs()[0]->getName());
57+
$this->assertTrue($command->getRequiredArgs()[0]->isRequired());
58+
}
59+
60+
public function testCreateReturnsRunner()
61+
{
62+
$exercise = new CliExerciseImpl;
63+
$this->assertInstanceOf(CliRunner::class, $this->factory->create($exercise));
64+
}
65+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\ExerciseRunner;
4+
5+
use PhpSchool\PhpWorkshop\CommandDefinition;
6+
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
7+
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\ExerciseRunnerFactoryInterface;
8+
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
9+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
10+
use PHPUnit_Framework_TestCase;
11+
12+
/**
13+
* @author Aydin Hassan <aydin@hotmail.co.uk>
14+
*/
15+
class RunnerManagerTest extends PHPUnit_Framework_TestCase
16+
{
17+
public function testConfigureInputCallsCorrectFactory()
18+
{
19+
$exercise = new CliExerciseImpl;
20+
$manager = new RunnerManager;
21+
$command = new CommandDefinition('my-command', [], 'var_dump');
22+
23+
$factory1 = $this->prophesize(ExerciseRunnerFactoryInterface::class);
24+
$factory1->supports($exercise)->willReturn(false);
25+
$factory1->configureInput($command)->shouldNotBeCalled();
26+
27+
$factory2 = $this->prophesize(ExerciseRunnerFactoryInterface::class);
28+
$factory2->supports($exercise)->willReturn(true);
29+
$factory2->configureInput($command)->shouldBeCalled();
30+
31+
$manager->addFactory($factory1->reveal());
32+
$manager->addFactory($factory2->reveal());
33+
$manager->configureInput($exercise, $command);
34+
}
35+
36+
public function testGetRunnerCallsCorrectFactory()
37+
{
38+
$exercise = new CliExerciseImpl;
39+
$manager = new RunnerManager;
40+
41+
$factory1 = $this->prophesize(ExerciseRunnerFactoryInterface::class);
42+
$factory1->supports($exercise)->willReturn(false);
43+
$factory1->create($exercise)->shouldNotBeCalled();
44+
45+
$factory2 = $this->prophesize(ExerciseRunnerFactoryInterface::class);
46+
$factory2->supports($exercise)->willReturn(true);
47+
$factory2->create($exercise)->shouldBeCalled();
48+
49+
$manager->addFactory($factory1->reveal());
50+
$manager->addFactory($factory2->reveal());
51+
$manager->getRunner($exercise);
52+
}
53+
54+
public function testExceptionIsThrownWhenConfiguringInputIfNoFactorySupportsExercise()
55+
{
56+
$exercise = new CliExerciseImpl;
57+
$manager = new RunnerManager;
58+
59+
$this->expectException(InvalidArgumentException::class);
60+
$this->expectExceptionMessage('Exercise Type: "CLI" not supported');
61+
62+
$manager->configureInput($exercise, new CommandDefinition('my-command', [], 'var_dump'));
63+
}
64+
65+
public function testExceptionIsThrownWhenGettingRunnerIfNoFactorySupportsExercise()
66+
{
67+
$exercise = new CliExerciseImpl;
68+
$manager = new RunnerManager;
69+
70+
$this->expectException(InvalidArgumentException::class);
71+
$this->expectExceptionMessage('Exercise Type: "CLI" not supported');
72+
73+
$manager->getRunner($exercise);
74+
}
75+
}

0 commit comments

Comments
 (0)