Skip to content

Check exercise assigned listener #136

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 1 commit into from
Nov 21, 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
12 changes: 10 additions & 2 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpSchool\PhpWorkshop\Factory\MenuFactory;
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
use PhpSchool\PhpWorkshop\Factory\RunnerFactory;
use PhpSchool\PhpWorkshop\Listener\CheckExerciseAssignedListener;
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
Expand Down Expand Up @@ -148,7 +149,6 @@
$c->get(ExerciseRepository::class),
$c->get(ExerciseDispatcher::class),
$c->get(UserState::class),
$c->get(UserStateSerializer::class),
$c->get(OutputInterface::class)
);
},
Expand Down Expand Up @@ -178,6 +178,9 @@
SelfCheckListener::class => function (ContainerInterface $c) {
return new SelfCheckListener($c->get(ResultAggregator::class));
},
CheckExerciseAssignedListener::class => function (ContainerInterface $c) {
return new CheckExerciseAssignedListener($c->get(UserState::class));
},

//checks
FileExistsCheck::class => object(),
Expand Down Expand Up @@ -260,5 +263,10 @@
'@shakeyShane' => 'Shane Osbourne',
'@chris3ailey' => 'Chris Bailey'
],
'appContributors' => []
'appContributors' => [],
'eventListeners' => [
'route.pre.resolve.args' => [
CheckExerciseAssignedListener::class
],
]
];
5 changes: 0 additions & 5 deletions src/Command/PrintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public function __construct(
*/
public function __invoke()
{
if (!$this->userState->isAssignedExercise()) {
$this->output->printError("No active exercises. Select one from the menu");
return 1;
}

$currentExercise = $this->userState->getCurrentExercise();
$exercise = $this->exerciseRepository->findByName($currentExercise);

Expand Down
15 changes: 0 additions & 15 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class RunCommand
*/
private $userState;

/**
* @var UserStateSerializer
*/
private $userStateSerializer;

/**
* @var ExerciseDispatcher
*/
Expand All @@ -45,20 +40,17 @@ class RunCommand
* @param ExerciseRepository $exerciseRepository
* @param ExerciseDispatcher $exerciseDispatcher
* @param UserState $userState
* @param UserStateSerializer $userStateSerializer
* @param OutputInterface $output
*/
public function __construct(
ExerciseRepository $exerciseRepository,
ExerciseDispatcher $exerciseDispatcher,
UserState $userState,
UserStateSerializer $userStateSerializer,
OutputInterface $output
) {
$this->output = $output;
$this->exerciseRepository = $exerciseRepository;
$this->userState = $userState;
$this->userStateSerializer = $userStateSerializer;
$this->exerciseDispatcher = $exerciseDispatcher;
}

Expand All @@ -77,14 +69,7 @@ public function __invoke(Input $input)
return 1;
}
$program = realpath($program);

if (!$this->userState->isAssignedExercise()) {
$this->output->printError("No active exercises. Select one from the menu");
return 1;
}

$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());

$this->exerciseDispatcher->run($exercise, $program, $this->output);
}
}
5 changes: 0 additions & 5 deletions src/Command/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ public function __invoke(Input $input)
}
$program = realpath($program);

if (!$this->userState->isAssignedExercise()) {
$this->output->printError("No active exercises. Select one from the menu");
return 1;
}

$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
$results = $this->exerciseDispatcher->verify($exercise, $input);

Expand Down
43 changes: 43 additions & 0 deletions src/Listener/CheckExerciseAssignedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpSchool\PhpWorkshop\Listener;

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\UserState;

/**
* @author Aydin Hassan <[email protected]>
*/
class CheckExerciseAssignedListener
{
/**
* @var UserState
*/
private $userState;

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

/**
* @param Event $event
*/
public function __invoke(Event $event)
{
/** @var CommandDefinition $command */
$command = $event->getParameter('command');

if (!in_array($command->getName(), ['verify', 'run', 'print'])) {
return;
}

if (!$this->userState->isAssignedExercise()) {
throw new \RuntimeException('No active exercise. Select one from the menu');
}
}
}
16 changes: 0 additions & 16 deletions test/Command/PrintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@
*/
class PrintCommandTest extends PHPUnit_Framework_TestCase
{
public function testErrorIsPrintedIfNoExerciseAssigned()
{
$repo = new ExerciseRepository([]);
$state = new UserState;
$output = $this->createMock(OutputInterface::class);
$renderer = $this->createMock(MarkdownRenderer::class);

$output
->expects($this->once())
->method('printError')
->with('No active exercises. Select one from the menu');

$command = new PrintCommand('phpschool', $repo, $state, $renderer, $output);
$this->assertSame(1, $command->__invoke());
}

public function testExerciseIsPrintedIfAssigned()
{
$file = tempnam(sys_get_temp_dir(), 'pws');
Expand Down
24 changes: 0 additions & 24 deletions test/Command/VerifyCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ public function testVerifyPrintsErrorIfProgramDoesNotExist()
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $programFile])));
}

public function testVerifyPrintsErrorIfNoExerciseAssigned()
{
$file = tempnam(sys_get_temp_dir(), 'pws');
touch($file);

$repo = new ExerciseRepository([]);
$state = new UserState;
$output = $this->createMock(OutputInterface::class);
$dispatcher = $this->createMock(ExerciseDispatcher::class);

$output
->expects($this->once())
->method('printError')
->with('No active exercises. Select one from the menu');

$serializer = $this->createMock(UserStateSerializer::class);
$renderer = $this->createMock(ResultsRenderer::class);

$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $file])));

unlink($file);
}

public function testVerifyAddsCompletedExerciseAndReturnsCorrectCodeOnSuccess()
{
$file = tempnam(sys_get_temp_dir(), 'pws');
Expand Down
91 changes: 91 additions & 0 deletions test/Listener/CheckExerciseAssignedListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace PhpSchool\PhpWorkshopTest\Listener;

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Listener\CheckExerciseAssignedListener;
use PHPUnit_Framework_TestCase;
use PhpSchool\PhpWorkshop\UserState;

/**
* @author Aydin Hassan <[email protected]>
*/
class CheckExerciseAssignedListenerTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider commandsThatRequireAssignedExercise
* @param CommandDefinition $command
*/
public function testExceptionIsThrownIfNoExerciseAssigned(CommandDefinition $command)
{
$state = new UserState;

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No active exercise. Select one from the menu');

$listener = new CheckExerciseAssignedListener($state);
$listener->__invoke(new Event('some-event', ['command' => $command]));
}

/**
* @dataProvider commandsThatRequireAssignedExercise
* @param CommandDefinition $command
*/
public function testExceptionIsNotThrownIfExerciseAssigned(CommandDefinition $command)
{
$state = new UserState(['exercise1'], 'exercise1');
$listener = new CheckExerciseAssignedListener($state);
$listener->__invoke(new Event('some-event', ['command' => $command]));
}

/**
* @return array
*/
public function commandsThatRequireAssignedExercise()
{
return [
[$this->command('verify')],
[$this->command('run')],
[$this->command('print')],
];
}

/**
* @dataProvider commandsThatDoNotRequireAssignedExercise
* @param CommandDefinition $command
*/
public function testExceptionIsNotThrownIfCommandDoesNotRequireAssignedExercise(CommandDefinition $command)
{
$state = new UserState(['exercise1'], 'exercise1');
$listener = new CheckExerciseAssignedListener($state);
$listener->__invoke(new Event('some-event', ['command' => $command]));
}

/**
* @return array
*/
public function commandsThatDoNotRequireAssignedExercise()
{
return [
[$this->command('help')],
[$this->command('credits')],
[$this->command('menu')],
];
}

/**
* @param $commandName
* @return \PHPUnit_Framework_MockObject_MockObject|CommandDefinition
*/
private function command($commandName)
{
$command = $this->createMock(CommandDefinition::class);
$command
->expects($this->any())
->method('getName')
->willReturn($commandName);

return $command;
}
}