Skip to content

Commit 2b13283

Browse files
committed
qa: provide tests for ReflectionBasedListenerProvider
Creates ReflectionBasedListenerProviderTest to test the various behaviors of the provider. Discovered a typo in a function name, which led to a fix. Added `PhlyTest\EventDispatcher\TestAsset\Listener` and `listenerFunction` to help test the class.
1 parent 21ca920 commit 2b13283

File tree

5 files changed

+155
-1
lines changed

5 files changed

+155
-1
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
}
3838
},
3939
"autoload-dev": {
40+
"files": [
41+
"test/TestAsset/listener_function.php"
42+
],
4043
"psr-4": {
4144
"PhlyTest\\EventDispatcher\\": "test/"
4245
}

src/ListenerProvider/ReflectionBasedListenerProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
class ReflectionBasedListenerProvider implements ReflectableListenerProviderInterface
2727
{
28+
/** @var array<string, callable[]> */
2829
private $listeners = [];
2930

3031
public function getListenersForEvent(object $event) : iterable
@@ -80,7 +81,7 @@ private function getReflector(callable $listener) : ReflectionFunctionAbstract
8081
return new ReflectionMethod($listener, '__invoke');
8182
}
8283

83-
if (is_string($listener) && false !== stpros($listener, '::')) {
84+
if (is_string($listener) && false !== strpos($listener, '::')) {
8485
$listener = explode('::', $listener, 2);
8586
}
8687

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
4+
* @copyright Copyright (c) 2018-2019 Matthew Weier O'Phinney (https:/mwop.net)
5+
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace PhlyTest\EventDispatcher\ListenerProvider;
11+
12+
use InvalidArgumentException;
13+
use Phly\EventDispatcher\ListenerProvider\ReflectionBasedListenerProvider;
14+
use PhlyTest\EventDispatcher\TestAsset\TestEvent;
15+
use PhlyTest\EventDispatcher\TestAsset\Listener;
16+
use PHPUnit\Framework\TestCase;
17+
18+
use function PhlyTest\EventDispatcher\TestAsset\listenerFunction;
19+
20+
class ReflectionBasedListenerProviderTest extends TestCase
21+
{
22+
public function testCanAttachWithExplicitType()
23+
{
24+
$listener = function ($event) {
25+
};
26+
$provider = new ReflectionBasedListenerProvider();
27+
$provider->listen($listener, TestEvent::class);
28+
29+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
30+
31+
$this->assertSame([$listener], $listeners);
32+
}
33+
34+
public function testProviderDetectsTypeFromClosure()
35+
{
36+
$listener = function (TestEvent $event) {
37+
};
38+
$provider = new ReflectionBasedListenerProvider();
39+
$provider->listen($listener);
40+
41+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
42+
43+
$this->assertSame([$listener], $listeners);
44+
}
45+
46+
public function testProviderDetectsTypeFromFunctionName()
47+
{
48+
$provider = new ReflectionBasedListenerProvider();
49+
$provider->listen('PhlyTest\EventDispatcher\TestAsset\listenerFunction');
50+
51+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
52+
53+
$this->assertSame(['PhlyTest\EventDispatcher\TestAsset\listenerFunction'], $listeners);
54+
}
55+
56+
public function testProviderDetectsTypeFromStaticMethodName()
57+
{
58+
$listener = Listener::class . '::onStatic';
59+
$provider = new ReflectionBasedListenerProvider();
60+
$provider->listen($listener);
61+
62+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
63+
64+
$this->assertSame([$listener], $listeners);
65+
}
66+
67+
public function testProviderDetectsTypeFromArrayStaticMethod()
68+
{
69+
$listener = [Listener::class, 'onStatic'];
70+
$provider = new ReflectionBasedListenerProvider();
71+
$provider->listen($listener);
72+
73+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
74+
75+
$this->assertSame([$listener], $listeners);
76+
}
77+
78+
public function testProviderDetectsTypeFromArrayInstanceMethod()
79+
{
80+
$instance = new Listener();
81+
$listener = [$instance, 'onTest'];
82+
$provider = new ReflectionBasedListenerProvider();
83+
$provider->listen($listener);
84+
85+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
86+
87+
$this->assertSame([$listener], $listeners);
88+
}
89+
90+
public function testProviderDetectsTypeFromInvokableInstance()
91+
{
92+
$listener = new Listener();
93+
$provider = new ReflectionBasedListenerProvider();
94+
$provider->listen($listener);
95+
96+
$listeners = iterator_to_array($provider->getListenersForEvent(new TestEvent()));
97+
98+
$this->assertSame([$listener], $listeners);
99+
}
100+
101+
public function testListenRaisesExceptionIfUnableToDetermineEventType()
102+
{
103+
$listener = function ($event) {
104+
};
105+
$provider = new ReflectionBasedListenerProvider();
106+
107+
$this->expectException(InvalidArgumentException::class);
108+
$this->expectExceptionMessage('Missing event parameter for listener');
109+
$provider->listen($listener);
110+
}
111+
}

test/TestAsset/Listener.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
4+
* @copyright Copyright (c) 2018-2019 Matthew Weier O'Phinney (https:/mwop.net)
5+
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace PhlyTest\EventDispatcher\TestAsset;
11+
12+
class Listener
13+
{
14+
public function __invoke(TestEvent $e) : void
15+
{
16+
}
17+
18+
public function onTest(TestEvent $e) : void
19+
{
20+
}
21+
22+
public static function onStatic(TestEvent $e) : void
23+
{
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
4+
* @copyright Copyright (c) 2018-2019 Matthew Weier O'Phinney (https:/mwop.net)
5+
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace PhlyTest\EventDispatcher\TestAsset;
11+
12+
function listenerFunction(TestEvent $e) : void
13+
{
14+
}

0 commit comments

Comments
 (0)