/
AttachableListenerProviderTest.php
55 lines (45 loc) · 1.79 KB
/
AttachableListenerProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
* @copyright Copyright (c) 2019 Matthew Weier O'Phinney (https:/mwop.net)
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace PhlyTest\EventDispatcher\ListenerProvider;
use Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider;
use PhlyTest\EventDispatcher\TestAsset\TestEvent;
use PHPUnit\Framework\TestCase;
use stdClass;
class AttachableListenerProviderTest extends TestCase
{
public function testProviderAllowsListenerRegistrationAndReturnsListenersBasedOnEventType()
{
$listenerForTestEvent = function (TestEvent $e) {
};
$listenerForStdclass = function (stdClass $e) {
};
$provider = new AttachableListenerProvider();
$provider->listen(TestEvent::class, $listenerForTestEvent);
$provider->listen(stdClass::class, $listenerForStdclass);
$this->assertSame(
[$listenerForTestEvent],
iterator_to_array($provider->getListenersForEvent(new TestEvent()))
);
$this->assertSame(
[$listenerForStdclass],
iterator_to_array($provider->getListenersForEvent(new stdClass()))
);
}
public function testProviderDoesNotAllowDuplicateRegistration()
{
$listenerForTestEvent = function (TestEvent $e) {
};
$provider = new AttachableListenerProvider();
$provider->listen(TestEvent::class, $listenerForTestEvent);
$provider->listen(TestEvent::class, $listenerForTestEvent);
$this->assertSame(
[$listenerForTestEvent],
iterator_to_array($provider->getListenersForEvent(new TestEvent()))
);
}
}