From e35fc32a97a3bad18d49108208418064310b80d6 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Thu, 1 Feb 2024 17:38:47 -0400 Subject: [PATCH] Chaining method definitions in Dispatcher::set() --- flight/core/Dispatcher.php | 6 +++++- tests/DispatcherTest.php | 30 +++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index fe7ca2f..7659446 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -69,10 +69,14 @@ class Dispatcher * * @param string $name Event name * @param callable $callback Callback function + * + * @return $this */ - public function set(string $name, callable $callback): void + public function set(string $name, callable $callback): self { $this->events[$name] = $callback; + + return $this; } /** diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 6a9ff3d..df3880f 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace tests; +use Closure; use Exception; use flight\core\Dispatcher; use tests\classes\Hello; @@ -21,54 +22,49 @@ class DispatcherTest extends TestCase // Map a closure public function testClosureMapping() { - $this->dispatcher->set('map1', function () { + $closure = Closure::fromCallable(function (): string { return 'hello'; }); - $result = $this->dispatcher->run('map1'); + $this->dispatcher->set('map1', $closure); + $result = $this->dispatcher->run('map1'); self::assertEquals('hello', $result); } // Map a function public function testFunctionMapping() { - $this->dispatcher->set('map2', function () { + $this->dispatcher->set('map2', function (): string { return 'hello'; }); $result = $this->dispatcher->run('map2'); - self::assertEquals('hello', $result); } public function testHasEvent() { - $this->dispatcher->set('map-event', function () { - return 'hello'; + $this->dispatcher->set('map-event', function (): void { }); $result = $this->dispatcher->has('map-event'); - $this->assertTrue($result); } public function testClearAllRegisteredEvents() { - $this->dispatcher->set('map-event', function () { - return 'hello'; - }); + $customFunction = $anotherFunction = function (): void { + }; - $this->dispatcher->set('map-event-2', function () { - return 'there'; - }); + $this->dispatcher + ->set('map-event', $customFunction) + ->set('map-event-2', $anotherFunction); $this->dispatcher->clear(); - $result = $this->dispatcher->has('map-event'); - $this->assertFalse($result); - $result = $this->dispatcher->has('map-event-2'); - $this->assertFalse($result); + $this->assertFalse($this->dispatcher->has('map-event')); + $this->assertFalse($this->dispatcher->has('map-event-2')); } public function testClearDeclaredRegisteredEvent()