From c4e87066e2b9cc4010d75cf82db89d2fa3a84532 Mon Sep 17 00:00:00 2001 From: Ilya Bakhlin Date: Tue, 28 Dec 2021 22:09:06 +0100 Subject: [PATCH] Testing the Event Dispatcher class. --- chandler/Eventing/EventDispatcher.php | 17 ++++- .../Chandler/Eventing/EventDispatcherTest.php | 71 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/Chandler/Eventing/EventDispatcherTest.php diff --git a/chandler/Eventing/EventDispatcher.php b/chandler/Eventing/EventDispatcher.php index afb0a1d..b00211a 100644 --- a/chandler/Eventing/EventDispatcher.php +++ b/chandler/Eventing/EventDispatcher.php @@ -11,22 +11,35 @@ use Chandler\Patterns\TSimpleSingleton; */ class EventDispatcher { + /** + * @var array + */ private $hooks = []; + /** + * @param mixed $hook + * + * @return bool + */ function addListener($hook): bool { $this->hooks[] = $hook; return true; } + /** + * @param \Chandler\Eventing\Events\Event $event + * + * @return \Chandler\Eventing\Events\Event + */ function pushEvent(Events\Event $event): Events\Event { - foreach ($hooks as $hook) { + foreach ($this->hooks as $hook) { if ($event instanceof Events\Cancelable) if ($event->isCancelled()) break; $method = "on" . str_replace("Event", "", get_class($event)); - if (!method_exists($hook, $methodName)) continue; + if (!method_exists($hook, $method)) continue; $hook->$method($event); } return $event; diff --git a/tests/Chandler/Eventing/EventDispatcherTest.php b/tests/Chandler/Eventing/EventDispatcherTest.php new file mode 100644 index 0000000..c37e1ac --- /dev/null +++ b/tests/Chandler/Eventing/EventDispatcherTest.php @@ -0,0 +1,71 @@ +assertTrue(EventDispatcher::i()->addListener($hook)); + } + + /** + * @return void + */ + public function testMethodI(): void + { + $this->assertSame(EventDispatcher::i(), EventDispatcher::i()); + } + + /** + * @dataProvider provideMethodPushEvent + * + * @param \Chandler\Eventing\Events\Event $event + * + * @return void + */ + public function testMethodPushEvent(Event $event): void + { + $this->assertSame($event, EventDispatcher::i()->pushEvent($event)); + } +}