Chaining method definitions in Dispatcher::set()

pull/538/head
fadrian06 12 months ago
parent 28b6c99561
commit e35fc32a97

@ -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;
}
/**

@ -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()

Loading…
Cancel
Save