diff --git a/composer.json b/composer.json index badc4b1..06977bf 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "prefer-stable": true, "require": { "php": "^7.1", - "phpgears/event": "~0.2", + "phpgears/event": "~0.3.1", "symfony/event-dispatcher": "^4.3" }, "require-dev": { diff --git a/src/ContainerAwareDispatcher.php b/src/ContainerAwareDispatcher.php index 7cca347..a1d9164 100644 --- a/src/ContainerAwareDispatcher.php +++ b/src/ContainerAwareDispatcher.php @@ -82,7 +82,7 @@ public function addListener($eventName, $listener, $priority = 0): void { if (!\is_string($listener)) { throw new \InvalidArgumentException(\sprintf( - 'Event handler must be a container entry, %s given', + 'Event handler must be a container entry, "%s" given', \is_object($listener) ? \get_class($listener) : \gettype($listener) )); } @@ -105,14 +105,14 @@ public function dispatch($eventEnvelope): SymfonyEvent if (!$eventEnvelope instanceof EventEnvelope) { throw new \InvalidArgumentException(\sprintf( - 'Dispatched event must implement %s, %s given', + 'Dispatched event must implement "%s", "%s" given', EventEnvelope::class, \get_class($eventEnvelope) )); } - $eventName = \get_class($eventEnvelope->getWrappedEvent()); - $this->dispatchEvent($this->getListeners($eventName), $eventEnvelope); + $eventListeners = $this->getListeners($eventEnvelope->getWrappedEvent()->getEventType()); + $this->dispatchEvent($eventListeners, $eventEnvelope); return $eventEnvelope; } @@ -133,7 +133,7 @@ private function dispatchEvent(array $listeners, EventEnvelope $event): void if (!$handler instanceof EventHandler) { throw new \RuntimeException(\sprintf( - 'Event handler should implement %s, %s given', + 'Event handler should implement "%s", "%s" given', EventHandler::class, \is_object($handler) ? \get_class($handler) : \gettype($handler) )); diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 971cf1c..f37ce14 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -71,7 +71,7 @@ public function addListener($eventName, $listener, $priority = 0): void { if (!$listener instanceof EventHandler) { throw new \InvalidArgumentException(\sprintf( - 'Event handler must be an instance of %s, %s given', + 'Event handler must be an instance of "%s", "%s" given', EventHandler::class, \is_object($listener) ? \get_class($listener) : \gettype($listener) )); @@ -95,14 +95,14 @@ public function dispatch($eventEnvelope): SymfonyEvent if (!$eventEnvelope instanceof EventEnvelope) { throw new \InvalidArgumentException(\sprintf( - 'Dispatched event must implement %s, %s given', + 'Dispatched event must implement "%s", "%s" given', EventEnvelope::class, \get_class($eventEnvelope) )); } - $eventName = \get_class($eventEnvelope->getWrappedEvent()); - $this->dispatchEvent($this->getListeners($eventName), $eventEnvelope); + $eventListeners = $this->getListeners($eventEnvelope->getWrappedEvent()->getEventType()); + $this->dispatchEvent($eventListeners, $eventEnvelope); return $eventEnvelope; } diff --git a/tests/Dispatcher/ContainerAwareDispatcherTest.php b/tests/Dispatcher/ContainerAwareDispatcherTest.php index d188762..b919c3a 100644 --- a/tests/Dispatcher/ContainerAwareDispatcherTest.php +++ b/tests/Dispatcher/ContainerAwareDispatcherTest.php @@ -30,7 +30,7 @@ class ContainerAwareDispatcherTest extends TestCase public function testInvalidListener(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Event handler must be a container entry, stdClass given'); + $this->expectExceptionMessage('Event handler must be a container entry, "stdClass" given'); /** @var ContainerInterface $containerMock */ $containerMock = $this->getMockBuilder(ContainerInterface::class) @@ -58,7 +58,7 @@ public function testEmptyEvent(): void public function testInvalidEvent(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/^Dispatched event must implement .+\\\EventEnvelope, .+ given$/'); + $this->expectExceptionMessageRegExp('/^Dispatched event must implement ".+\\\EventEnvelope", ".+" given$/'); /** @var ContainerInterface $containerMock */ $containerMock = $this->getMockBuilder(ContainerInterface::class) @@ -73,7 +73,7 @@ public function testInvalidEvent(): void public function testInvalidHandler(): void { $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Event handler should implement Gears\Event\EventHandler, string given'); + $this->expectExceptionMessage('Event handler should implement "Gears\Event\EventHandler", "string" given'); $containerMock = $this->getMockBuilder(ContainerInterface::class) ->disableOriginalConstructor() diff --git a/tests/Dispatcher/DispatcherTest.php b/tests/Dispatcher/DispatcherTest.php index ac5b507..b56cbc2 100644 --- a/tests/Dispatcher/DispatcherTest.php +++ b/tests/Dispatcher/DispatcherTest.php @@ -30,7 +30,7 @@ public function testInvalidListener(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp( - '/^Event handler must be an instance of .+\\\EventHandler, stdClass given$/' + '/^Event handler must be an instance of ".+\\\EventHandler", "stdClass" given$/' ); new Dispatcher([\stdClass::class => new \stdClass()]); @@ -49,7 +49,7 @@ public function testEmptyEvent(): void public function testInvalidEvent(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/^Dispatched event must implement .+\\\EventEnvelope, .+ given$/'); + $this->expectExceptionMessageRegExp('/^Dispatched event must implement ".+\\\EventEnvelope", ".+" given$/'); $eventDispatcher = new Dispatcher();