Skip to content

Commit 6c89275

Browse files
committed
fix an error with incomplete configuration
1 parent 1e29ff7 commit 6c89275

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Container/EventDispatcherFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function __invoke(ContainerInterface $container): EventDispatcherInterfac
1616
$config = $container->get('config')['app-events'];
1717
$listenerProvider = new ListenerProvider();
1818

19-
foreach ($config['event-listeners'] as $eventClass => $listeners) {
20-
foreach ($listeners as $listenerId) {
19+
foreach ($config['event-listeners'] ?? [] as $eventClass => $listeners) {
20+
foreach ($listeners ?? [] as $listenerId) {
2121
$listenerProvider->addListener(
2222
$eventClass,
2323
static function () use ($container, $listenerId): callable {

test/Container/EventDispatcherFactoryTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,28 @@ public function testItShouldCreateNewInstancesOfEventDispatcher(): void
3131
{
3232
$this->givenAConfigProvider();
3333
$this->havingAContainerWithConfig();
34+
$this->havingListenersConfiguredToReceiveAnEvent();
3435
$this->whenEventDispatcherFactoryIsInvoked();
3536
$this->thenItReturnsInstanceOfEventDispatcher();
3637
$this->andThenDispatcherShouldDispatchAnEvent();
3738
}
3839

40+
public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithIncompleteConfiguration(): void
41+
{
42+
$this->givenAnIncompleteConfigProvider();
43+
$this->havingAContainerWithConfig();
44+
$this->whenEventDispatcherFactoryIsInvoked();
45+
$this->thenItReturnsInstanceOfEventDispatcher();
46+
}
47+
48+
public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithMoreIncompleteConfiguration(): void
49+
{
50+
$this->givenAMoreIncompleteConfigProvider();
51+
$this->havingAContainerWithConfig();
52+
$this->whenEventDispatcherFactoryIsInvoked();
53+
$this->thenItReturnsInstanceOfEventDispatcher();
54+
}
55+
3956
private function givenAConfigProvider(): void
4057
{
4158
$config = new ConfigProvider();
@@ -51,6 +68,28 @@ private function givenAConfigProvider(): void
5168
]);
5269
}
5370

71+
private function givenAnIncompleteConfigProvider(): void
72+
{
73+
$config = new ConfigProvider();
74+
$this->config = array_merge($config->__invoke(), [
75+
'app-events' => [
76+
'event-listeners' => [
77+
TestEvent::class => null
78+
]
79+
]
80+
]);
81+
}
82+
83+
private function givenAMoreIncompleteConfigProvider(): void
84+
{
85+
$config = new ConfigProvider();
86+
$this->config = array_merge($config->__invoke(), [
87+
'app-events' => [
88+
'event-listeners' => null
89+
]
90+
]);
91+
}
92+
5493
private function havingAContainerWithConfig(): void
5594
{
5695
$this->container = $this->createMock(ContainerInterface::class);
@@ -59,6 +98,10 @@ private function havingAContainerWithConfig(): void
5998
->method('get')
6099
->with('config')
61100
->willReturn($this->config);
101+
}
102+
103+
private function havingListenersConfiguredToReceiveAnEvent(): void
104+
{
62105
$this->container
63106
->expects($this->at(1))
64107
->method('get')

0 commit comments

Comments
 (0)