diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 68b3fac796c9..42a33162f1a8 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -359,13 +359,13 @@ protected function addInterfaceListeners($eventName, array $listeners = []) /** * Register an event listener with the dispatcher. * - * @param \Closure|string|array $listener + * @param \Closure|string $listener * @param bool $wildcard * @return \Closure */ public function makeListener($listener, $wildcard = false) { - if (is_string($listener) || is_array($listener)) { + if (is_string($listener)) { return $this->createClassListener($listener, $wildcard); } @@ -381,7 +381,7 @@ public function makeListener($listener, $wildcard = false) /** * Create a class based listener using the IoC container. * - * @param string|array $listener + * @param string $listener * @param bool $wildcard * @return \Closure */ @@ -401,12 +401,12 @@ public function createClassListener($listener, $wildcard = false) /** * Create the class based event callable. * - * @param string|array $listener + * @param string $listener * @return callable */ protected function createClassCallable($listener) { - [$class, $method] = is_array($listener) ? $listener : $this->parseClassCallable($listener); + [$class, $method] = $this->parseClassCallable($listener); if ($this->handlerShouldBeQueued($class)) { return $this->createQueuedHandlerCallable($class, $method); diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 1c10f5ffe4a6..900e1f673333 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -33,7 +33,7 @@ class Application extends Container implements ApplicationContract, CachesConfig * * @var string */ - const VERSION = '7.16.0'; + const VERSION = '7.16.1'; /** * The base path for the Laravel installation. diff --git a/tests/Events/EventsDispatcherTest.php b/tests/Events/EventsDispatcherTest.php index dddfeb8d3fd0..5c7313683bc1 100755 --- a/tests/Events/EventsDispatcherTest.php +++ b/tests/Events/EventsDispatcherTest.php @@ -173,6 +173,62 @@ public function testMultiplePushedEventsWillGetFlushed() $this->assertSame('taylor otwell', $_SERVER['__event.test']); } + public function testWildcardListeners() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen('foo.bar', function () { + $_SERVER['__event.test'] = 'regular'; + }); + $d->listen('foo.*', function () { + $_SERVER['__event.test'] = 'wildcard'; + }); + $d->listen('bar.*', function () { + $_SERVER['__event.test'] = 'nope'; + }); + + $response = $d->dispatch('foo.bar'); + + $this->assertEquals([null, null], $response); + $this->assertSame('wildcard', $_SERVER['__event.test']); + } + + public function testWildcardListenersWithResponses() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen('foo.bar', function () { + return 'regular'; + }); + $d->listen('foo.*', function () { + return 'wildcard'; + }); + $d->listen('bar.*', function () { + return 'nope'; + }); + + $response = $d->dispatch('foo.bar'); + + $this->assertEquals(['regular', 'wildcard'], $response); + } + + public function testWildcardListenersCacheFlushing() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen('foo.*', function () { + $_SERVER['__event.test'] = 'cached_wildcard'; + }); + $d->dispatch('foo.bar'); + $this->assertSame('cached_wildcard', $_SERVER['__event.test']); + + $d->listen('foo.*', function () { + $_SERVER['__event.test'] = 'new_wildcard'; + }); + $d->dispatch('foo.bar'); + $this->assertSame('new_wildcard', $_SERVER['__event.test']); + } + public function testListenersCanBeRemoved() { unset($_SERVER['__event.test']); @@ -186,6 +242,39 @@ public function testListenersCanBeRemoved() $this->assertFalse(isset($_SERVER['__event.test'])); } + public function testWildcardListenersCanBeRemoved() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen('foo.*', function () { + $_SERVER['__event.test'] = 'foo'; + }); + $d->forget('foo.*'); + $d->dispatch('foo.bar'); + + $this->assertFalse(isset($_SERVER['__event.test'])); + } + + public function testWildcardCacheIsClearedWhenListenersAreRemoved() + { + unset($_SERVER['__event.test']); + + $d = new Dispatcher; + $d->listen('foo*', function () { + $_SERVER['__event.test'] = 'foo'; + }); + $d->dispatch('foo'); + + $this->assertSame('foo', $_SERVER['__event.test']); + + unset($_SERVER['__event.test']); + + $d->forget('foo*'); + $d->dispatch('foo'); + + $this->assertFalse(isset($_SERVER['__event.test'])); + } + public function testListenersCanBeFound() { $d = new Dispatcher; @@ -196,4 +285,108 @@ public function testListenersCanBeFound() }); $this->assertTrue($d->hasListeners('foo')); } + + public function testWildcardListenersCanBeFound() + { + $d = new Dispatcher; + $this->assertFalse($d->hasListeners('foo.*')); + + $d->listen('foo.*', function () { + // + }); + $this->assertTrue($d->hasListeners('foo.*')); + $this->assertTrue($d->hasListeners('foo.bar')); + } + + public function testEventPassedFirstToWildcards() + { + $d = new Dispatcher; + $d->listen('foo.*', function ($event, $data) { + $this->assertSame('foo.bar', $event); + $this->assertEquals(['first', 'second'], $data); + }); + $d->dispatch('foo.bar', ['first', 'second']); + + $d = new Dispatcher; + $d->listen('foo.bar', function ($first, $second) { + $this->assertSame('first', $first); + $this->assertSame('second', $second); + }); + $d->dispatch('foo.bar', ['first', 'second']); + } + + public function testClassesWork() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen(ExampleEvent::class, function () { + $_SERVER['__event.test'] = 'baz'; + }); + $d->dispatch(new ExampleEvent); + + $this->assertSame('baz', $_SERVER['__event.test']); + } + + public function testEventClassesArePayload() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen(ExampleEvent::class, function ($payload) { + $_SERVER['__event.test'] = $payload; + }); + $d->dispatch($e = new ExampleEvent, ['foo']); + + $this->assertSame($e, $_SERVER['__event.test']); + } + + public function testInterfacesWork() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen(SomeEventInterface::class, function () { + $_SERVER['__event.test'] = 'bar'; + }); + $d->dispatch(new AnotherEvent); + + $this->assertSame('bar', $_SERVER['__event.test']); + } + + public function testBothClassesAndInterfacesWork() + { + unset($_SERVER['__event.test']); + $_SERVER['__event.test'] = []; + $d = new Dispatcher; + $d->listen(AnotherEvent::class, function ($p) { + $_SERVER['__event.test'][] = $p; + $_SERVER['__event.test1'] = 'fooo'; + }); + $d->listen(SomeEventInterface::class, function ($p) { + $_SERVER['__event.test'][] = $p; + $_SERVER['__event.test2'] = 'baar'; + }); + $d->dispatch($e = new AnotherEvent, ['foo']); + + $this->assertSame($e, $_SERVER['__event.test'][0]); + $this->assertSame($e, $_SERVER['__event.test'][1]); + $this->assertSame('fooo', $_SERVER['__event.test1']); + $this->assertSame('baar', $_SERVER['__event.test2']); + + unset($_SERVER['__event.test1']); + unset($_SERVER['__event.test2']); + } +} + +class ExampleEvent +{ + // +} + +interface SomeEventInterface +{ + // +} + +class AnotherEvent implements SomeEventInterface +{ + // } diff --git a/tests/Events/EventsDispatcherWildcardTest.php b/tests/Events/EventsDispatcherWildcardTest.php deleted file mode 100644 index 95f86bdbe040..000000000000 --- a/tests/Events/EventsDispatcherWildcardTest.php +++ /dev/null @@ -1,133 +0,0 @@ -listen('foo.bar', function () { - $_SERVER['__event.test'] = 'regular'; - }); - $d->listen('foo.*', function () { - $_SERVER['__event.test'] = 'wildcard'; - }); - $d->listen('bar.*', function () { - $_SERVER['__event.test'] = 'nope'; - }); - - $response = $d->dispatch('foo.bar'); - - $this->assertEquals([null, null], $response); - $this->assertSame('wildcard', $_SERVER['__event.test']); - } - - public function testWildcardListenersWithResponses() - { - unset($_SERVER['__event.test']); - $d = new Dispatcher; - $d->listen('foo.bar', function () { - return 'regular'; - }); - $d->listen('foo.*', function () { - return 'wildcard'; - }); - $d->listen('bar.*', function () { - return 'nope'; - }); - - $response = $d->dispatch('foo.bar'); - - $this->assertEquals(['regular', 'wildcard'], $response); - } - - public function testWildcardListenersCacheFlushing() - { - unset($_SERVER['__event.test']); - $d = new Dispatcher; - $d->listen('foo.*', function () { - $_SERVER['__event.test'] = 'cached_wildcard'; - }); - $d->dispatch('foo.bar'); - $this->assertSame('cached_wildcard', $_SERVER['__event.test']); - - $d->listen('foo.*', function () { - $_SERVER['__event.test'] = 'new_wildcard'; - }); - $d->dispatch('foo.bar'); - $this->assertSame('new_wildcard', $_SERVER['__event.test']); - } - - public function testWildcardListenersCanBeRemoved() - { - unset($_SERVER['__event.test']); - $d = new Dispatcher; - $d->listen('foo.*', function () { - $_SERVER['__event.test'] = 'foo'; - }); - $d->forget('foo.*'); - $d->dispatch('foo.bar'); - - $this->assertFalse(isset($_SERVER['__event.test'])); - } - - public function testWildcardCacheIsClearedWhenListenersAreRemoved() - { - unset($_SERVER['__event.test']); - - $d = new Dispatcher; - $d->listen('foo*', function () { - $_SERVER['__event.test'] = 'foo'; - }); - $d->dispatch('foo'); - - $this->assertSame('foo', $_SERVER['__event.test']); - - unset($_SERVER['__event.test']); - - $d->forget('foo*'); - $d->dispatch('foo'); - - $this->assertFalse(isset($_SERVER['__event.test'])); - } - - public function testWildcardListenersCanBeFound() - { - $d = new Dispatcher; - $this->assertFalse($d->hasListeners('foo.*')); - - $d->listen('foo.*', function () { - // - }); - $this->assertTrue($d->hasListeners('foo.*')); - $this->assertTrue($d->hasListeners('foo.bar')); - } - - public function testEventPassedFirstToWildcards() - { - $d = new Dispatcher; - $d->listen('foo.*', function ($event, $data) { - $this->assertSame('foo.bar', $event); - $this->assertEquals(['first', 'second'], $data); - }); - $d->dispatch('foo.bar', ['first', 'second']); - - $d = new Dispatcher; - $d->listen('foo.bar', function ($first, $second) { - $this->assertSame('first', $first); - $this->assertSame('second', $second); - }); - $d->dispatch('foo.bar', ['first', 'second']); - } -} diff --git a/tests/Events/ObjectTypeEventsTest.php b/tests/Events/ObjectTypeEventsTest.php deleted file mode 100644 index ef21dcb6a414..000000000000 --- a/tests/Events/ObjectTypeEventsTest.php +++ /dev/null @@ -1,108 +0,0 @@ -listen(ExampleEvent::class, function () { - $_SERVER['__event.test'] = 'baz'; - }); - $d->dispatch(new ExampleEvent); - - $this->assertSame('baz', $_SERVER['__event.test']); - } - - public function testArrayCallbackListenersAreHandled() - { - unset($_SERVER['__event.ExampleListener']); - $d = new Dispatcher; - $d->listen(ExampleEvent::class, [ExampleListener::class, 'hear']); - $d->dispatch(new ExampleEvent); - - $this->assertTrue($_SERVER['__event.ExampleListener']); - } - - public function testEventClassesArePayload() - { - unset($_SERVER['__event.test']); - $d = new Dispatcher; - $d->listen(ExampleEvent::class, function ($payload) { - $_SERVER['__event.test'] = $payload; - }); - $d->dispatch($e = new ExampleEvent, ['foo']); - - $this->assertSame($e, $_SERVER['__event.test']); - } - - public function testInterfacesWork() - { - unset($_SERVER['__event.test']); - $d = new Dispatcher; - $d->listen(SomeEventInterface::class, function () { - $_SERVER['__event.test'] = 'bar'; - }); - $d->dispatch(new AnotherEvent); - - $this->assertSame('bar', $_SERVER['__event.test']); - } - - public function testBothClassesAndInterfacesWork() - { - unset($_SERVER['__event.test']); - $_SERVER['__event.test'] = []; - $d = new Dispatcher; - $d->listen(AnotherEvent::class, function ($p) { - $_SERVER['__event.test'][] = $p; - $_SERVER['__event.test1'] = 'fooo'; - }); - $d->listen(SomeEventInterface::class, function ($p) { - $_SERVER['__event.test'][] = $p; - $_SERVER['__event.test2'] = 'baar'; - }); - $d->dispatch($e = new AnotherEvent, ['foo']); - - $this->assertSame($e, $_SERVER['__event.test'][0]); - $this->assertSame($e, $_SERVER['__event.test'][1]); - $this->assertSame('fooo', $_SERVER['__event.test1']); - $this->assertSame('baar', $_SERVER['__event.test2']); - - unset($_SERVER['__event.test1']); - unset($_SERVER['__event.test2']); - } -} - -class ExampleEvent -{ - // -} - -interface SomeEventInterface -{ - // -} - -class AnotherEvent implements SomeEventInterface -{ - // -} - -class ExampleListener -{ - public function hear() - { - $_SERVER['__event.ExampleListener'] = true; - } -}