Skip to content

Make sure to pass arguments added by events to the results #157

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 2 commits into from
Feb 28, 2018
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
4 changes: 2 additions & 2 deletions src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private function doVerify(array $args, Input $input)
return GenericFailure::fromArgsAndCodeExecutionFailure($args, $e);
}
if ($solutionOutput === $userOutput) {
return new Success($args);
return new Success($event->getArgs());
}

return RequestFailure::fromArgsAndOutput($args, $solutionOutput, $userOutput);
return RequestFailure::fromArgsAndOutput($event->getArgs(), $solutionOutput, $userOutput);
}

/**
Expand Down
40 changes: 38 additions & 2 deletions test/ExerciseRunner/CliRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
Expand All @@ -31,14 +32,20 @@ class CliRunnerTest extends PHPUnit_Framework_TestCase
private $runner;

/**
* @var CliExerciseInterface
* @var CliExerciseInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $exercise;

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

public function setUp()
{
$this->exercise = $this->createMock(CliExerciseInterface::class);
$this->runner = new CliRunner($this->exercise, new EventDispatcher(new ResultAggregator));
$this->eventDispatcher = new EventDispatcher(new ResultAggregator);
$this->runner = new CliRunner($this->exercise, $this->eventDispatcher);

$this->exercise
->expects($this->any())
Expand Down Expand Up @@ -214,4 +221,33 @@ public function testRunPassesOutputAndReturnsFailureIfScriptFails()
$success = $this->runner->run(new Input('app', ['program' => __DIR__ . '/../res/cli/user-error.php']), $output);
$this->assertFalse($success);
}

public function testsArgsAppendedByEventsArePassedToResults()
{
$this->eventDispatcher->listen(
['cli.verify.student-execute.pre', 'cli.verify.reference-execute.pre'],
function (CliExecuteEvent $e) {
$e->appendArg('4');
}
);

$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php'));
$this->exercise
->expects($this->once())
->method('getSolution')
->will($this->returnValue($solution));

$this->exercise
->expects($this->once())
->method('getArgs')
->will($this->returnValue([1, 2, 3]));

$this->assertInstanceOf(
CliResult::class,
$res = $this->runner->verify(new Input('app', ['program' => __DIR__ . '/../res/cli/user.php']))
);

$this->assertTrue($res->isSuccessful());
$this->assertEquals([1, 2, 3, 4], $res->getResults()[0]->getArgs()->getArrayCopy());
}
}
2 changes: 1 addition & 1 deletion test/res/database/solution.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$count = 0;
for ($i = 1; $i < count($argv); $i++) {
for ($i = 2; $i < count($argv); $i++) {
$count += $argv[$i];
}

Expand Down
2 changes: 1 addition & 1 deletion test/res/database/user.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$count = 0;
for ($i = 1; $i < count($argv); $i++) {
for ($i = 2; $i < count($argv); $i++) {
$count += $argv[$i];
}

Expand Down