Skip to content

Commit 3cc6e76

Browse files
committed
throw exception with invalid configuration
1 parent 6c89275 commit 3cc6e76

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/Container/EventDispatcherFactory.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,34 @@
88
use Antidot\Event\ListenerProvider;
99
use Psr\Container\ContainerInterface;
1010
use Psr\EventDispatcher\EventDispatcherInterface;
11+
use RuntimeException;
12+
use Throwable;
1113

1214
class EventDispatcherFactory
1315
{
1416
public function __invoke(ContainerInterface $container): EventDispatcherInterface
1517
{
16-
$config = $container->get('config')['app-events'];
17-
$listenerProvider = new ListenerProvider();
18-
19-
foreach ($config['event-listeners'] ?? [] as $eventClass => $listeners) {
20-
foreach ($listeners ?? [] as $listenerId) {
21-
$listenerProvider->addListener(
22-
$eventClass,
23-
static function () use ($container, $listenerId): callable {
24-
return $container->get($listenerId);
25-
}
26-
);
18+
try {
19+
$config = $container->get('config')['app-events'];
20+
$listenerProvider = new ListenerProvider();
21+
foreach ($config['event-listeners'] ?? [] as $eventClass => $listeners) {
22+
foreach ($listeners ?? [] as $listenerId) {
23+
$listenerProvider->addListener(
24+
$eventClass,
25+
static function () use ($container, $listenerId): callable {
26+
return $container->get($listenerId);
27+
}
28+
);
29+
}
2730
}
28-
}
2931

30-
return new EventDispatcher($listenerProvider);
32+
return new EventDispatcher($listenerProvider);
33+
} catch (Throwable $exception) {
34+
throw new RuntimeException(sprintf(
35+
'Something went wrong constructing an instance of %s, review config related to \'app-events\''
36+
. ' and see the previous exception for more info.',
37+
EventDispatcher::class
38+
), 0, $exception);
39+
}
3140
}
3241
}

test/Container/EventDispatcherFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithMoreIncom
5353
$this->thenItReturnsInstanceOfEventDispatcher();
5454
}
5555

56+
public function testItShouldThrowAnExceptionWhenAnInvalidConfigGiven(): void
57+
{
58+
$this->expectAnException();
59+
$this->givenInvalidConfiguration();
60+
$this->havingAContainerWithConfig();
61+
$this->whenEventDispatcherFactoryIsInvoked();
62+
}
63+
5664
private function givenAConfigProvider(): void
5765
{
5866
$config = new ConfigProvider();
@@ -68,6 +76,20 @@ private function givenAConfigProvider(): void
6876
]);
6977
}
7078

79+
private function givenInvalidConfiguration(): void
80+
{
81+
$this->config = [
82+
'other-configs' => [
83+
'event-listeners' => [
84+
TestEvent::class => [
85+
'Listener1',
86+
'Listener2',
87+
]
88+
]
89+
]
90+
];
91+
}
92+
7193
private function givenAnIncompleteConfigProvider(): void
7294
{
7395
$config = new ConfigProvider();
@@ -131,4 +153,9 @@ private function andThenDispatcherShouldDispatchAnEvent(): void
131153

132154
$this->dispatcher->dispatch($event);
133155
}
156+
157+
private function expectAnException(): void
158+
{
159+
$this->expectException(\RuntimeException::class);
160+
}
134161
}

0 commit comments

Comments
 (0)