Skip to content

Dispatch event when an exercise is selected #134

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 3 commits into from
Nov 7, 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
6 changes: 3 additions & 3 deletions src/Exercise/AbstractExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getSolution()
sprintf(
'%s/../../exercises/%s/solution/solution.php',
dirname((new ReflectionClass(static::class))->getFileName()),
$this->normaliseName($this->getName())
self::normaliseName($this->getName())
)
)
);
Expand All @@ -55,7 +55,7 @@ public function getSolution()
*/
public function getProblem()
{
$name = $this->normaliseName($this->getName());
$name = self::normaliseName($this->getName());
$dir = dirname((new ReflectionClass(static::class))->getFileName());
return sprintf('%s/../../exercises/%s/problem/problem.md', $dir, $name);
}
Expand All @@ -74,7 +74,7 @@ public function tearDown()
* @param string $name
* @return string
*/
private function normaliseName($name)
public static function normaliseName($name)
{
return preg_replace('/[^A-Za-z\-]+/', '', str_replace(' ', '-', strtolower($name)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/EventDispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __invoke(ContainerInterface $container)
$dispatcher->listen('verify.post.check', $container->get(SelfCheckListener::class));

//add listeners from config
$eventListeners = $container->get('eventListeners') ?: [];
$eventListeners = $container->has('eventListeners') ? $container->get('eventListeners') : [];

if (!is_array($eventListeners)) {
throw InvalidArgumentException::typeMisMatch('array', $eventListeners);
Expand Down
46 changes: 35 additions & 11 deletions src/Factory/MenuFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use PhpSchool\PhpWorkshop\Command\CreditsCommand;
use PhpSchool\PhpWorkshop\Command\HelpCommand;
use PhpSchool\PhpWorkshop\Command\MenuCommandInvoker;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRenderer;
use PhpSchool\PhpWorkshop\ExerciseRepository;
Expand All @@ -34,6 +37,7 @@ public function __invoke(ContainerInterface $c)
$userState = $userStateSerializer->deSerialize();
$exerciseRenderer = $c->get(ExerciseRenderer::class);
$workshopType = $c->get(WorkshopType::class);
$eventDispatcher = $c->get(EventDispatcher::class);

$builder = (new CliMenuBuilder)
->addLineBreak();
Expand All @@ -46,17 +50,21 @@ public function __invoke(ContainerInterface $c)
->addLineBreak('_')
->addLineBreak()
->addStaticItem('Exercises')
->addStaticItem('---------')
->addItems(
array_map(function (ExerciseInterface $exercise) use ($exerciseRenderer, $userState, $workshopType) {
return [
$exercise->getName(),
$exerciseRenderer,
$userState->completedExercise($exercise->getName()),
$this->isExerciseDisabled($exercise, $userState, $workshopType)
];
}, $exerciseRepository->findAll())
)
->addStaticItem('---------');

foreach ($exerciseRepository->findAll() as $exercise) {
$builder->addItem(
$exercise->getName(),
function (CliMenu $menu) use ($exerciseRenderer, $eventDispatcher, $exercise) {
$this->dispatchExerciseSelectedEvent($eventDispatcher, $exercise);
$exerciseRenderer->__invoke($menu);
},
$userState->completedExercise($exercise->getName()),
$this->isExerciseDisabled($exercise, $userState, $workshopType)
);
}

$builder
->addLineBreak()
->addLineBreak('-')
->addLineBreak()
Expand Down Expand Up @@ -121,4 +129,20 @@ private function isExerciseDisabled(ExerciseInterface $exercise, UserState $user
$previous = $exercise;
return true;
}

/**
* @param EventDispatcher $eventDispatcher
* @param ExerciseInterface $exercise
*/
private function dispatchExerciseSelectedEvent(EventDispatcher $eventDispatcher, ExerciseInterface $exercise)
{
$eventDispatcher->dispatch(
new Event(
sprintf(
'exercise.selected.%s',
AbstractExercise::normaliseName($exercise->getName())
)
)
);
}
}
48 changes: 40 additions & 8 deletions test/Factory/EventDispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsNotArray()
->will($this->returnValue($selfCheckListener));

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue(new \stdClass));
Expand Down Expand Up @@ -150,6 +155,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenersNotArray
->will($this->returnValue($selfCheckListener));

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => new \stdClass]));
Expand Down Expand Up @@ -191,6 +201,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerNotCallab
->will($this->returnValue($selfCheckListener));

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => [new \stdClass]]));
Expand Down Expand Up @@ -232,11 +247,16 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerContainer
->will($this->returnValue($selfCheckListener));

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => ['nonExistingContainerEntry']]));

$c->expects($this->once())
$c->expects($this->at(6))
->method('has')
->with('nonExistingContainerEntry')
->will($this->returnValue(false));
Expand Down Expand Up @@ -278,16 +298,21 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerContainer
->will($this->returnValue($selfCheckListener));

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => ['notCallableEntry']]));

$c->expects($this->once())
$c->expects($this->at(6))
->method('has')
->with('notCallableEntry')
->will($this->returnValue(true));

$c->expects($this->at(6))
$c->expects($this->at(7))
->method('get')
->with('notCallableEntry')
->will($this->returnValue(null));
Expand Down Expand Up @@ -332,11 +357,15 @@ public function testConfigEventListenersWithAnonymousFunction()
};

$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => [$callback]]));


$dispatcher = (new EventDispatcherFactory)->__invoke($c);
$this->assertInstanceOf(EventDispatcher::class, $dispatcher);
$this->assertSame(
Expand Down Expand Up @@ -398,22 +427,25 @@ public function testConfigEventListenersWithContainerEntry()
->with(SelfCheckListener::class)
->will($this->returnValue($selfCheckListener));



$c->expects($this->at(4))
->method('has')
->with('eventListeners')
->willReturn(true);

$c->expects($this->at(5))
->method('get')
->with('eventListeners')
->will($this->returnValue([ 'someEvent' => ['containerEntry']]));

$c->expects($this->once())
$c->expects($this->at(6))
->method('has')
->with('containerEntry')
->will($this->returnValue(true));

$callback = function () {
};

$c->expects($this->at(6))
$c->expects($this->at(7))
->method('get')
->with('containerEntry')
->will($this->returnValue($callback));
Expand Down
4 changes: 3 additions & 1 deletion test/Factory/MenuFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\PhpWorkshop\Command\CreditsCommand;
use PhpSchool\PhpWorkshop\Command\HelpCommand;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRenderer;
use PhpSchool\PhpWorkshop\ExerciseRepository;
Expand Down Expand Up @@ -53,7 +54,8 @@ public function testFactoryReturnsInstance()
'bgColour' => 'black',
'fgColour' => 'green',
'workshopTitle' => 'TITLE',
WorkshopType::class => WorkshopType::STANDARD()
WorkshopType::class => WorkshopType::STANDARD(),
EventDispatcher::class => $this->createMock(EventDispatcher::class),
];

$container
Expand Down