Skip to content

Commit ca50272

Browse files
committed
fix when eventListeners not set
1 parent c7b1a15 commit ca50272

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

src/Factory/EventDispatcherFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __invoke(ContainerInterface $container)
4040
$dispatcher->listen('verify.post.check', $container->get(SelfCheckListener::class));
4141

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

4545
if (!is_array($eventListeners)) {
4646
throw InvalidArgumentException::typeMisMatch('array', $eventListeners);

src/Factory/MenuFactory.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@ public function __invoke(ContainerInterface $c)
5050
->addLineBreak('_')
5151
->addLineBreak()
5252
->addStaticItem('Exercises')
53-
->addStaticItem('---------')
54-
->addItems(
55-
array_map(function (ExerciseInterface $exercise) use ($exerciseRenderer, $userState, $workshopType, $eventDispatcher) {
56-
return [
57-
$exercise->getName(),
58-
function (CliMenu $menu) use ($exerciseRenderer, $eventDispatcher, $exercise) {
59-
$this->dispatchExerciseSelectedEvent($eventDispatcher, $exercise);
60-
$exerciseRenderer->__invoke($menu);
61-
},
62-
$userState->completedExercise($exercise->getName()),
63-
$this->isExerciseDisabled($exercise, $userState, $workshopType)
64-
];
65-
}, $exerciseRepository->findAll())
66-
)
53+
->addStaticItem('---------');
54+
55+
foreach ($exerciseRepository->findAll() as $exercise) {
56+
$builder->addItem(
57+
$exercise->getName(),
58+
function (CliMenu $menu) use ($exerciseRenderer, $eventDispatcher, $exercise) {
59+
$this->dispatchExerciseSelectedEvent($eventDispatcher, $exercise);
60+
$exerciseRenderer->__invoke($menu);
61+
},
62+
$userState->completedExercise($exercise->getName()),
63+
$this->isExerciseDisabled($exercise, $userState, $workshopType)
64+
);
65+
}
66+
67+
$builder
6768
->addLineBreak()
6869
->addLineBreak('-')
6970
->addLineBreak()

test/Factory/EventDispatcherFactoryTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsNotArray()
109109
->will($this->returnValue($selfCheckListener));
110110

111111
$c->expects($this->at(4))
112+
->method('has')
113+
->with('eventListeners')
114+
->willReturn(true);
115+
116+
$c->expects($this->at(5))
112117
->method('get')
113118
->with('eventListeners')
114119
->will($this->returnValue(new \stdClass));
@@ -150,6 +155,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenersNotArray
150155
->will($this->returnValue($selfCheckListener));
151156

152157
$c->expects($this->at(4))
158+
->method('has')
159+
->with('eventListeners')
160+
->willReturn(true);
161+
162+
$c->expects($this->at(5))
153163
->method('get')
154164
->with('eventListeners')
155165
->will($this->returnValue([ 'someEvent' => new \stdClass]));
@@ -191,6 +201,11 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerNotCallab
191201
->will($this->returnValue($selfCheckListener));
192202

193203
$c->expects($this->at(4))
204+
->method('has')
205+
->with('eventListeners')
206+
->willReturn(true);
207+
208+
$c->expects($this->at(5))
194209
->method('get')
195210
->with('eventListeners')
196211
->will($this->returnValue([ 'someEvent' => [new \stdClass]]));
@@ -232,11 +247,16 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerContainer
232247
->will($this->returnValue($selfCheckListener));
233248

234249
$c->expects($this->at(4))
250+
->method('has')
251+
->with('eventListeners')
252+
->willReturn(true);
253+
254+
$c->expects($this->at(5))
235255
->method('get')
236256
->with('eventListeners')
237257
->will($this->returnValue([ 'someEvent' => ['nonExistingContainerEntry']]));
238258

239-
$c->expects($this->once())
259+
$c->expects($this->at(6))
240260
->method('has')
241261
->with('nonExistingContainerEntry')
242262
->will($this->returnValue(false));
@@ -278,16 +298,21 @@ public function testConfigEventListenersThrowsExceptionIfEventsListenerContainer
278298
->will($this->returnValue($selfCheckListener));
279299

280300
$c->expects($this->at(4))
301+
->method('has')
302+
->with('eventListeners')
303+
->willReturn(true);
304+
305+
$c->expects($this->at(5))
281306
->method('get')
282307
->with('eventListeners')
283308
->will($this->returnValue([ 'someEvent' => ['notCallableEntry']]));
284309

285-
$c->expects($this->once())
310+
$c->expects($this->at(6))
286311
->method('has')
287312
->with('notCallableEntry')
288313
->will($this->returnValue(true));
289314

290-
$c->expects($this->at(6))
315+
$c->expects($this->at(7))
291316
->method('get')
292317
->with('notCallableEntry')
293318
->will($this->returnValue(null));
@@ -332,11 +357,15 @@ public function testConfigEventListenersWithAnonymousFunction()
332357
};
333358

334359
$c->expects($this->at(4))
360+
->method('has')
361+
->with('eventListeners')
362+
->willReturn(true);
363+
364+
$c->expects($this->at(5))
335365
->method('get')
336366
->with('eventListeners')
337367
->will($this->returnValue([ 'someEvent' => [$callback]]));
338368

339-
340369
$dispatcher = (new EventDispatcherFactory)->__invoke($c);
341370
$this->assertInstanceOf(EventDispatcher::class, $dispatcher);
342371
$this->assertSame(
@@ -398,22 +427,25 @@ public function testConfigEventListenersWithContainerEntry()
398427
->with(SelfCheckListener::class)
399428
->will($this->returnValue($selfCheckListener));
400429

401-
402-
403430
$c->expects($this->at(4))
431+
->method('has')
432+
->with('eventListeners')
433+
->willReturn(true);
434+
435+
$c->expects($this->at(5))
404436
->method('get')
405437
->with('eventListeners')
406438
->will($this->returnValue([ 'someEvent' => ['containerEntry']]));
407439

408-
$c->expects($this->once())
440+
$c->expects($this->at(6))
409441
->method('has')
410442
->with('containerEntry')
411443
->will($this->returnValue(true));
412444

413445
$callback = function () {
414446
};
415447

416-
$c->expects($this->at(6))
448+
$c->expects($this->at(7))
417449
->method('get')
418450
->with('containerEntry')
419451
->will($this->returnValue($callback));

0 commit comments

Comments
 (0)