diff --git a/src/ExerciseDispatcher.php b/src/ExerciseDispatcher.php index 24849304..b1399aee 100644 --- a/src/ExerciseDispatcher.php +++ b/src/ExerciseDispatcher.php @@ -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); @@ -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'))); } @@ -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; diff --git a/src/ExerciseRunner/CgiRunner.php b/src/ExerciseRunner/CgiRunner.php index d48793d5..dbce747f 100644 --- a/src/ExerciseRunner/CgiRunner.php +++ b/src/ExerciseRunner/CgiRunner.php @@ -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; @@ -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; } /** @@ -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' ); @@ -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()); @@ -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; - } - } } diff --git a/src/ExerciseRunner/CliRunner.php b/src/ExerciseRunner/CliRunner.php index adb764c9..46ab56e9 100644 --- a/src/ExerciseRunner/CliRunner.php +++ b/src/ExerciseRunner/CliRunner.php @@ -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; @@ -25,6 +26,10 @@ */ class CliRunner implements ExerciseRunnerInterface { + /** + * @var CliExercise + */ + private $exercise; /** * @var EventDispatcher @@ -32,11 +37,13 @@ class CliRunner implements ExerciseRunnerInterface 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; } /** @@ -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' ); @@ -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()); diff --git a/src/ExerciseRunner/ExerciseRunnerInterface.php b/src/ExerciseRunner/ExerciseRunnerInterface.php index cda15450..ba213873 100644 --- a/src/ExerciseRunner/ExerciseRunnerInterface.php +++ b/src/ExerciseRunner/ExerciseRunnerInterface.php @@ -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); } diff --git a/src/Factory/RunnerFactory.php b/src/Factory/RunnerFactory.php index 76e9a11c..3d2bd0d3 100644 --- a/src/Factory/RunnerFactory.php +++ b/src/Factory/RunnerFactory.php @@ -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; @@ -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()) + ); } } diff --git a/test/ExerciseDispatcherTest.php b/test/ExerciseDispatcherTest.php index 6d64db51..ebc0131d 100644 --- a/test/ExerciseDispatcherTest.php +++ b/test/ExerciseDispatcherTest.php @@ -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)); } @@ -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); @@ -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); @@ -326,7 +327,7 @@ public function testVerifyOnlyRunsRequiredChecks() $this->assertTrue($result->isSuccessful()); } - public function testWhenBeforeChecksFailTheyReturnImmediatelyEarly() + public function testWhenBeforeChecksFailTheyReturnImmediately() { $this->createExercise(); $this->check @@ -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); @@ -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); @@ -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)); diff --git a/test/ExerciseRunner/CgiRunnerTest.php b/test/ExerciseRunner/CgiRunnerTest.php index 43b0bf48..5b8f5238 100644 --- a/test/ExerciseRunner/CgiRunnerTest.php +++ b/test/ExerciseRunner/CgiRunnerTest.php @@ -36,8 +36,8 @@ class CgiRunnerTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->runner = new CgiRunner(new EventDispatcher(new ResultAggregator)); $this->exercise = $this->getMock(CgiExerciseInterface::class); + $this->runner = new CgiRunner($this->exercise, new EventDispatcher(new ResultAggregator)); $this->exercise ->expects($this->any()) @@ -47,23 +47,6 @@ public function setUp() $this->assertEquals('CGI Program Runner', $this->runner->getName()); } - public function testVerifyThrowsExceptionIfNotValidExercise() - { - $this->exercise = $this->getMock(CgiExerciseInterface::class); - $this->exercise - ->expects($this->once()) - ->method('getType') - ->will($this->returnValue(ExerciseType::CLI())); - - $this->exercise - ->expects($this->any()) - ->method('getRequests') - ->will($this->returnValue([])); - - $this->setExpectedException(InvalidArgumentException::class); - $this->runner->verify($this->exercise, ''); - } - public function testVerifyThrowsExceptionIfSolutionFailsExecution() { $solution = SingleFileSolution::fromFile(__DIR__ . '/../res/cgi/solution-error.php'); @@ -83,7 +66,7 @@ public function testVerifyThrowsExceptionIfSolutionFailsExecution() $regex = "/^PHP Code failed to execute\\. Error: \"PHP Parse error: syntax error, unexpected end of file in/"; $this->setExpectedExceptionRegExp(SolutionExecutionException::class, $regex); - $this->runner->verify($this->exercise, ''); + $this->runner->verify(''); } public function testVerifyReturnsSuccessIfGetSolutionOutputMatchesUserOutput() @@ -105,7 +88,7 @@ public function testVerifyReturnsSuccessIfGetSolutionOutputMatchesUserOutput() $this->assertInstanceOf( CgiOutResult::class, - $this->runner->verify($this->exercise, realpath(__DIR__ . '/../res/cgi/get-solution.php')) + $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-solution.php')) ); } @@ -131,7 +114,7 @@ public function testVerifyReturnsSuccessIfPostSolutionOutputMatchesUserOutput() $this->assertInstanceOf( CgiOutResult::class, - $this->runner->verify($this->exercise, realpath(__DIR__ . '/../res/cgi/post-solution.php')) + $this->runner->verify(realpath(__DIR__ . '/../res/cgi/post-solution.php')) ); } @@ -152,7 +135,7 @@ public function testVerifyReturnsFailureIfUserSolutionFailsToExecute() ->method('getRequests') ->will($this->returnValue([$request])); - $failure = $this->runner->verify($this->exercise, realpath(__DIR__ . '/../res/cgi/user-error.php')); + $failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/user-error.php')); $this->assertInstanceOf(CgiOutResult::class, $failure); $this->assertCount(1, $failure); @@ -181,7 +164,7 @@ public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput() ->method('getRequests') ->will($this->returnValue([$request])); - $failure = $this->runner->verify($this->exercise, realpath(__DIR__ . '/../res/cgi/get-user-wrong.php')); + $failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-user-wrong.php')); $this->assertInstanceOf(CgiOutResult::class, $failure); $this->assertCount(1, $failure); @@ -210,10 +193,7 @@ public function testVerifyReturnsFailureIfSolutionOutputHeadersDoesNotMatchUserO ->method('getRequests') ->will($this->returnValue([$request])); - $failure = $this->runner->verify( - $this->exercise, - realpath(__DIR__ . '/../res/cgi/get-user-header-wrong.php') - ); + $failure = $this->runner->verify(realpath(__DIR__ . '/../res/cgi/get-user-header-wrong.php')); $this->assertInstanceOf(CgiOutResult::class, $failure); $this->assertCount(1, $failure); @@ -238,24 +218,6 @@ public function testVerifyReturnsFailureIfSolutionOutputHeadersDoesNotMatchUserO ); } - public function testRunThrowsExceptionIfNotValidExercise() - { - $output = $this->getMock(OutputInterface::class); - $this->exercise = $this->getMock(CgiExerciseInterface::class); - $this->exercise - ->expects($this->once()) - ->method('getType') - ->will($this->returnValue(ExerciseType::CLI())); - - $this->exercise - ->expects($this->any()) - ->method('getRequests') - ->will($this->returnValue([])); - - $this->setExpectedException(InvalidArgumentException::class); - $this->runner->run($this->exercise, '', $output); - } - public function testRunPassesOutputAndReturnsSuccessIfAllRequestsAreSuccessful() { $output = new StdOutput(new Color); @@ -275,7 +237,7 @@ public function testRunPassesOutputAndReturnsSuccessIfAllRequestsAreSuccessful() $exp = "Content-type: text/html; charset=UTF-8\r\n\r\n10Content-type: text/html; charset=UTF-8\r\n\r\n12"; $this->expectOutputString($exp); - $success = $this->runner->run($this->exercise, realpath(__DIR__ . '/../res/cgi/get-solution.php'), $output); + $success = $this->runner->run(realpath(__DIR__ . '/../res/cgi/get-solution.php'), $output); $this->assertTrue($success); } @@ -294,7 +256,7 @@ public function testRunPassesOutputAndReturnsFailureIfARequestFails() $exp = "Status: 404 Not Found\r\nContent-type: text/html; charset=UTF-8\r\n\r\nNo input file specified.\n"; $this->expectOutputString($exp); - $success = $this->runner->run($this->exercise, '', $output); + $success = $this->runner->run('', $output); $this->assertFalse($success); } } diff --git a/test/ExerciseRunner/CliRunnerTest.php b/test/ExerciseRunner/CliRunnerTest.php index a25dd26d..95d05219 100644 --- a/test/ExerciseRunner/CliRunnerTest.php +++ b/test/ExerciseRunner/CliRunnerTest.php @@ -33,8 +33,8 @@ class CliRunnerTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->runner = new CliRunner(new EventDispatcher(new ResultAggregator)); $this->exercise = $this->getMock(CliExerciseInterface::class); + $this->runner = new CliRunner($this->exercise, new EventDispatcher(new ResultAggregator)); $this->exercise ->expects($this->any()) @@ -44,18 +44,6 @@ public function setUp() $this->assertEquals('CLI Program Runner', $this->runner->getName()); } - public function testVerifyThrowsExceptionIfNotValidExercise() - { - $this->exercise = $this->getMock(CliExerciseInterface::class); - $this->exercise - ->expects($this->once()) - ->method('getType') - ->will($this->returnValue(ExerciseType::CGI())); - - $this->setExpectedException(InvalidArgumentException::class); - $this->runner->verify($this->exercise, ''); - } - public function testVerifyThrowsExceptionIfSolutionFailsExecution() { $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution-error.php')); @@ -72,7 +60,7 @@ public function testVerifyThrowsExceptionIfSolutionFailsExecution() $regex = "/^PHP Code failed to execute\\. Error: \"PHP Parse error: syntax error, unexpected end of file"; $regex .= ", expecting ',' or ';'/"; $this->setExpectedExceptionRegExp(SolutionExecutionException::class, $regex); - $this->runner->verify($this->exercise, ''); + $this->runner->verify(''); } public function testVerifyReturnsSuccessIfSolutionOutputMatchesUserOutput() @@ -90,7 +78,7 @@ public function testVerifyReturnsSuccessIfSolutionOutputMatchesUserOutput() $this->assertInstanceOf( Success::class, - $this->runner->verify($this->exercise, __DIR__ . '/../res/cli/user.php') + $this->runner->verify(__DIR__ . '/../res/cli/user.php') ); } @@ -107,7 +95,7 @@ public function testVerifyReturnsFailureIfUserSolutionFailsToExecute() ->method('getArgs') ->will($this->returnValue([1, 2, 3])); - $failure = $this->runner->verify($this->exercise, __DIR__ . '/../res/cli/user-error.php'); + $failure = $this->runner->verify(__DIR__ . '/../res/cli/user-error.php'); $failureMsg = "/^PHP Code failed to execute. Error: \"PHP Parse error: syntax error, "; $failureMsg .= "unexpected end of file, expecting ',' or ';'/"; @@ -129,27 +117,13 @@ public function testVerifyReturnsFailureIfSolutionOutputDoesNotMatchUserOutput() ->method('getArgs') ->will($this->returnValue([1, 2, 3])); - $failure = $this->runner->verify($this->exercise, __DIR__ . '/../res/cli/user-wrong.php'); + $failure = $this->runner->verify(__DIR__ . '/../res/cli/user-wrong.php'); $this->assertInstanceOf(StdOutFailure::class, $failure); $this->assertEquals('6', $failure->getExpectedOutput()); $this->assertEquals('10', $failure->getActualOutput()); } - public function testRunThrowsExceptionIfNotValidExercise() - { - $output = new StdOutput(new Color); - - $this->exercise = $this->getMock(CliExerciseInterface::class); - $this->exercise - ->expects($this->once()) - ->method('getType') - ->will($this->returnValue(ExerciseType::CGI())); - - $this->setExpectedException(InvalidArgumentException::class); - $this->runner->run($this->exercise, '', $output); - } - public function testRunPassesOutputAndReturnsSuccessIfScriptIsSuccessful() { $output = new StdOutput(new Color); @@ -161,7 +135,7 @@ public function testRunPassesOutputAndReturnsSuccessIfScriptIsSuccessful() $this->expectOutputString('6'); - $success = $this->runner->run($this->exercise, __DIR__ . '/../res/cli/user.php', $output); + $success = $this->runner->run(__DIR__ . '/../res/cli/user.php', $output); $this->assertTrue($success); } @@ -176,7 +150,7 @@ public function testRunPassesOutputAndReturnsFailureIfScriptFails() $this->expectOutputRegex('/PHP Parse error: syntax error, unexpected end of file, expecting \',\' or \';\' /'); - $success = $this->runner->run($this->exercise, __DIR__ . '/../res/cli/user-error.php', $output); + $success = $this->runner->run(__DIR__ . '/../res/cli/user-error.php', $output); $this->assertFalse($success); } } diff --git a/test/Factory/RunnerFactoryTest.php b/test/Factory/RunnerFactoryTest.php index 330dfaac..0c7350b6 100644 --- a/test/Factory/RunnerFactoryTest.php +++ b/test/Factory/RunnerFactoryTest.php @@ -5,11 +5,14 @@ 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; use PhpSchool\PhpWorkshop\Factory\RunnerFactory; use PhpSchool\PhpWorkshop\ResultAggregator; +use PhpSchool\PhpWorkshopTest\Asset\CgiExerciseInterface; +use PhpSchool\PhpWorkshopTest\Asset\CliExerciseInterface; use PHPUnit_Framework_TestCase; /** @@ -19,7 +22,6 @@ */ class RunnerFactoryTest extends PHPUnit_Framework_TestCase { - public function testExceptionIsThrownIfTypeNotSupported() { $type = $this->getMockBuilder(ExerciseType::class) @@ -30,9 +32,15 @@ public function testExceptionIsThrownIfTypeNotSupported() ->method('getValue') ->will($this->returnValue('invalid')); + $exercise = $this->getMock(ExerciseInterface::class); + $exercise + ->expects($this->exactly(2)) + ->method('getType') + ->will($this->returnValue($type)); + $this->setExpectedException(InvalidArgumentException::class, 'Exercise Type: "invalid" not supported'); - (new RunnerFactory)->create($type, new EventDispatcher(new ResultAggregator)); + (new RunnerFactory)->create($exercise, new EventDispatcher(new ResultAggregator)); } public function testCliAndCgiRunnerCanBeCreated() @@ -40,10 +48,22 @@ public function testCliAndCgiRunnerCanBeCreated() $cliType = new ExerciseType(ExerciseType::CLI); $cgiType = new ExerciseType(ExerciseType::CGI); - $runnerFactory = new RunnerFactory($this->container); + $cliExercise = $this->getMock(CliExerciseInterface::class); + $cliExercise + ->expects($this->once()) + ->method('getType') + ->will($this->returnValue($cliType)); + $cgiExercise = $this->getMock(CgiExerciseInterface::class); + $cgiExercise + ->expects($this->once()) + ->method('getType') + ->will($this->returnValue($cgiType)); + + + $runnerFactory = new RunnerFactory($this->container); $eventDispatcher = new EventDispatcher(new ResultAggregator); - $this->assertInstanceOf(CliRunner::class, $runnerFactory->create($cliType, $eventDispatcher)); - $this->assertInstanceOf(CgiRunner::class, $runnerFactory->create($cgiType, $eventDispatcher)); + $this->assertInstanceOf(CliRunner::class, $runnerFactory->create($cliExercise, $eventDispatcher)); + $this->assertInstanceOf(CgiRunner::class, $runnerFactory->create($cgiExercise, $eventDispatcher)); } }