Skip to content

Commit 847d3c9

Browse files
committed
Check exercise is assigned listener
1 parent bc82383 commit 847d3c9

File tree

8 files changed

+144
-67
lines changed

8 files changed

+144
-67
lines changed

app/config.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpSchool\PhpWorkshop\Factory\MenuFactory;
2424
use PhpSchool\PhpWorkshop\Factory\ResultRendererFactory;
2525
use PhpSchool\PhpWorkshop\Factory\RunnerFactory;
26+
use PhpSchool\PhpWorkshop\Listener\CheckExerciseAssignedListener;
2627
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
2728
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
2829
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
@@ -148,7 +149,6 @@
148149
$c->get(ExerciseRepository::class),
149150
$c->get(ExerciseDispatcher::class),
150151
$c->get(UserState::class),
151-
$c->get(UserStateSerializer::class),
152152
$c->get(OutputInterface::class)
153153
);
154154
},
@@ -178,6 +178,9 @@
178178
SelfCheckListener::class => function (ContainerInterface $c) {
179179
return new SelfCheckListener($c->get(ResultAggregator::class));
180180
},
181+
CheckExerciseAssignedListener::class => function (ContainerInterface $c) {
182+
return new CheckExerciseAssignedListener($c->get(UserState::class));
183+
},
181184

182185
//checks
183186
FileExistsCheck::class => object(),
@@ -260,5 +263,10 @@
260263
'@shakeyShane' => 'Shane Osbourne',
261264
'@chris3ailey' => 'Chris Bailey'
262265
],
263-
'appContributors' => []
266+
'appContributors' => [],
267+
'eventListeners' => [
268+
'route.pre.resolve.args' => [
269+
CheckExerciseAssignedListener::class
270+
],
271+
]
264272
];

src/Command/PrintCommand.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ public function __construct(
6464
*/
6565
public function __invoke()
6666
{
67-
if (!$this->userState->isAssignedExercise()) {
68-
$this->output->printError("No active exercises. Select one from the menu");
69-
return 1;
70-
}
71-
7267
$currentExercise = $this->userState->getCurrentExercise();
7368
$exercise = $this->exerciseRepository->findByName($currentExercise);
7469

src/Command/RunCommand.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class RunCommand
3131
*/
3232
private $userState;
3333

34-
/**
35-
* @var UserStateSerializer
36-
*/
37-
private $userStateSerializer;
38-
3934
/**
4035
* @var ExerciseDispatcher
4136
*/
@@ -45,20 +40,17 @@ class RunCommand
4540
* @param ExerciseRepository $exerciseRepository
4641
* @param ExerciseDispatcher $exerciseDispatcher
4742
* @param UserState $userState
48-
* @param UserStateSerializer $userStateSerializer
4943
* @param OutputInterface $output
5044
*/
5145
public function __construct(
5246
ExerciseRepository $exerciseRepository,
5347
ExerciseDispatcher $exerciseDispatcher,
5448
UserState $userState,
55-
UserStateSerializer $userStateSerializer,
5649
OutputInterface $output
5750
) {
5851
$this->output = $output;
5952
$this->exerciseRepository = $exerciseRepository;
6053
$this->userState = $userState;
61-
$this->userStateSerializer = $userStateSerializer;
6254
$this->exerciseDispatcher = $exerciseDispatcher;
6355
}
6456

@@ -77,14 +69,7 @@ public function __invoke(Input $input)
7769
return 1;
7870
}
7971
$program = realpath($program);
80-
81-
if (!$this->userState->isAssignedExercise()) {
82-
$this->output->printError("No active exercises. Select one from the menu");
83-
return 1;
84-
}
85-
8672
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
87-
8873
$this->exerciseDispatcher->run($exercise, $program, $this->output);
8974
}
9075
}

src/Command/VerifyCommand.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ public function __invoke(Input $input)
8888
}
8989
$program = realpath($program);
9090

91-
if (!$this->userState->isAssignedExercise()) {
92-
$this->output->printError("No active exercises. Select one from the menu");
93-
return 1;
94-
}
95-
9691
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
9792
$results = $this->exerciseDispatcher->verify($exercise, $input);
9893

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Listener;
4+
5+
use PhpSchool\PhpWorkshop\CommandDefinition;
6+
use PhpSchool\PhpWorkshop\Event\Event;
7+
use PhpSchool\PhpWorkshop\UserState;
8+
9+
/**
10+
* @author Aydin Hassan <[email protected]>
11+
*/
12+
class CheckExerciseAssignedListener
13+
{
14+
/**
15+
* @var UserState
16+
*/
17+
private $userState;
18+
19+
/**
20+
* @param UserState $userState
21+
*/
22+
public function __construct(UserState $userState)
23+
{
24+
$this->userState = $userState;
25+
}
26+
27+
/**
28+
* @param Event $event
29+
*/
30+
public function __invoke(Event $event)
31+
{
32+
/** @var CommandDefinition $command */
33+
$command = $event->getParameter('command');
34+
35+
if (!in_array($command->getName(), ['verify', 'run', 'print'])) {
36+
return;
37+
}
38+
39+
if (!$this->userState->isAssignedExercise()) {
40+
throw new \RuntimeException('No active exercise. Select one from the menu');
41+
}
42+
}
43+
}

test/Command/PrintCommandTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@
1717
*/
1818
class PrintCommandTest extends PHPUnit_Framework_TestCase
1919
{
20-
public function testErrorIsPrintedIfNoExerciseAssigned()
21-
{
22-
$repo = new ExerciseRepository([]);
23-
$state = new UserState;
24-
$output = $this->createMock(OutputInterface::class);
25-
$renderer = $this->createMock(MarkdownRenderer::class);
26-
27-
$output
28-
->expects($this->once())
29-
->method('printError')
30-
->with('No active exercises. Select one from the menu');
31-
32-
$command = new PrintCommand('phpschool', $repo, $state, $renderer, $output);
33-
$this->assertSame(1, $command->__invoke());
34-
}
35-
3620
public function testExerciseIsPrintedIfAssigned()
3721
{
3822
$file = tempnam(sys_get_temp_dir(), 'pws');

test/Command/VerifyCommandTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,6 @@ public function testVerifyPrintsErrorIfProgramDoesNotExist()
6262
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $programFile])));
6363
}
6464

65-
public function testVerifyPrintsErrorIfNoExerciseAssigned()
66-
{
67-
$file = tempnam(sys_get_temp_dir(), 'pws');
68-
touch($file);
69-
70-
$repo = new ExerciseRepository([]);
71-
$state = new UserState;
72-
$output = $this->createMock(OutputInterface::class);
73-
$dispatcher = $this->createMock(ExerciseDispatcher::class);
74-
75-
$output
76-
->expects($this->once())
77-
->method('printError')
78-
->with('No active exercises. Select one from the menu');
79-
80-
$serializer = $this->createMock(UserStateSerializer::class);
81-
$renderer = $this->createMock(ResultsRenderer::class);
82-
83-
$command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer);
84-
$this->assertSame(1, $command->__invoke(new Input('appName', ['program' => $file])));
85-
86-
unlink($file);
87-
}
88-
8965
public function testVerifyAddsCompletedExerciseAndReturnsCorrectCodeOnSuccess()
9066
{
9167
$file = tempnam(sys_get_temp_dir(), 'pws');
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Listener;
4+
5+
use PhpSchool\PhpWorkshop\CommandDefinition;
6+
use PhpSchool\PhpWorkshop\Event\Event;
7+
use PhpSchool\PhpWorkshop\Listener\CheckExerciseAssignedListener;
8+
use PHPUnit_Framework_TestCase;
9+
use PhpSchool\PhpWorkshop\UserState;
10+
11+
/**
12+
* @author Aydin Hassan <[email protected]>
13+
*/
14+
class CheckExerciseAssignedListenerTest extends PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @dataProvider commandsThatRequireAssignedExercise
18+
* @param CommandDefinition $command
19+
*/
20+
public function testExceptionIsThrownIfNoExerciseAssigned(CommandDefinition $command)
21+
{
22+
$state = new UserState;
23+
24+
$this->expectException(\RuntimeException::class);
25+
$this->expectExceptionMessage('No active exercise. Select one from the menu');
26+
27+
$listener = new CheckExerciseAssignedListener($state);
28+
$listener->__invoke(new Event('some-event', ['command' => $command]));
29+
}
30+
31+
/**
32+
* @dataProvider commandsThatRequireAssignedExercise
33+
* @param CommandDefinition $command
34+
*/
35+
public function testExceptionIsNotThrownIfExerciseAssigned(CommandDefinition $command)
36+
{
37+
$state = new UserState(['exercise1'], 'exercise1');
38+
$listener = new CheckExerciseAssignedListener($state);
39+
$listener->__invoke(new Event('some-event', ['command' => $command]));
40+
}
41+
42+
/**
43+
* @return array
44+
*/
45+
public function commandsThatRequireAssignedExercise()
46+
{
47+
return [
48+
[$this->command('verify')],
49+
[$this->command('run')],
50+
[$this->command('print')],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider commandsThatDoNotRequireAssignedExercise
56+
* @param CommandDefinition $command
57+
*/
58+
public function testExceptionIsNotThrownIfCommandDoesNotRequireAssignedExercise(CommandDefinition $command)
59+
{
60+
$state = new UserState(['exercise1'], 'exercise1');
61+
$listener = new CheckExerciseAssignedListener($state);
62+
$listener->__invoke(new Event('some-event', ['command' => $command]));
63+
}
64+
65+
/**
66+
* @return array
67+
*/
68+
public function commandsThatDoNotRequireAssignedExercise()
69+
{
70+
return [
71+
[$this->command('help')],
72+
[$this->command('credits')],
73+
[$this->command('menu')],
74+
];
75+
}
76+
77+
/**
78+
* @param $commandName
79+
* @return \PHPUnit_Framework_MockObject_MockObject|CommandDefinition
80+
*/
81+
private function command($commandName)
82+
{
83+
$command = $this->createMock(CommandDefinition::class);
84+
$command
85+
->expects($this->any())
86+
->method('getName')
87+
->willReturn($commandName);
88+
89+
return $command;
90+
}
91+
}

0 commit comments

Comments
 (0)