|
|
|
@ -37,9 +37,7 @@ class DispatcherTest extends TestCase
|
|
|
|
|
|
|
|
|
|
public function testFunctionMapping(): void
|
|
|
|
|
{
|
|
|
|
|
$this->dispatcher->set('map2', function (): string {
|
|
|
|
|
return 'hello';
|
|
|
|
|
});
|
|
|
|
|
$this->dispatcher->set('map2', fn (): string => 'hello');
|
|
|
|
|
|
|
|
|
|
$this->assertSame('hello', $this->dispatcher->run('map2'));
|
|
|
|
|
}
|
|
|
|
@ -61,6 +59,9 @@ class DispatcherTest extends TestCase
|
|
|
|
|
->set('map-event', $customFunction)
|
|
|
|
|
->set('map-event-2', $anotherFunction);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($this->dispatcher->has('map-event'));
|
|
|
|
|
$this->assertTrue($this->dispatcher->has('map-event-2'));
|
|
|
|
|
|
|
|
|
|
$this->dispatcher->clear();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->dispatcher->has('map-event'));
|
|
|
|
@ -76,6 +77,9 @@ class DispatcherTest extends TestCase
|
|
|
|
|
->set('map-event', $customFunction)
|
|
|
|
|
->set('map-event-2', $anotherFunction);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($this->dispatcher->has('map-event'));
|
|
|
|
|
$this->assertTrue($this->dispatcher->has('map-event-2'));
|
|
|
|
|
|
|
|
|
|
$this->dispatcher->clear('map-event');
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->dispatcher->has('map-event'));
|
|
|
|
@ -105,9 +109,7 @@ class DispatcherTest extends TestCase
|
|
|
|
|
|
|
|
|
|
public function testBeforeAndAfter(): void
|
|
|
|
|
{
|
|
|
|
|
$this->dispatcher->set('hello', function (string $name): string {
|
|
|
|
|
return "Hello, $name!";
|
|
|
|
|
});
|
|
|
|
|
$this->dispatcher->set('hello', fn (string $name): string => "Hello, $name!");
|
|
|
|
|
|
|
|
|
|
$this->dispatcher
|
|
|
|
|
->hook('hello', Dispatcher::FILTER_BEFORE, function (array &$params): void {
|
|
|
|
@ -124,6 +126,25 @@ class DispatcherTest extends TestCase
|
|
|
|
|
$this->assertSame('Hello, Fred! Have a nice day!', $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBeforeAndAfterWithShortAfterFilterSyntax(): void
|
|
|
|
|
{
|
|
|
|
|
$this->dispatcher->set('hello', fn (string $name): string => "Hello, $name!");
|
|
|
|
|
|
|
|
|
|
$this->dispatcher
|
|
|
|
|
->hook('hello', Dispatcher::FILTER_BEFORE, function (array &$params): void {
|
|
|
|
|
// Manipulate the parameter
|
|
|
|
|
$params[0] = 'Fred';
|
|
|
|
|
})
|
|
|
|
|
->hook('hello', Dispatcher::FILTER_AFTER, function (string &$output): void {
|
|
|
|
|
// Manipulate the output
|
|
|
|
|
$output .= ' Have a nice day!';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$result = $this->dispatcher->run('hello', ['Bob']);
|
|
|
|
|
|
|
|
|
|
$this->assertSame('Hello, Fred! Have a nice day!', $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvalidCallback(): void
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|