|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/phly/phly-event-dispatcher for the canonical source repository |
| 4 | + * @copyright Copyright (c) 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 Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider; |
| 13 | +use PhlyTest\EventDispatcher\TestAsset\TestEvent; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use stdClass; |
| 16 | + |
| 17 | +class AttachableListenerProviderTest extends TestCase |
| 18 | +{ |
| 19 | + public function testProviderAllowsListenerRegistrationAndReturnsListenersBasedOnEventType() |
| 20 | + { |
| 21 | + $listenerForTestEvent = function (TestEvent $e) { |
| 22 | + }; |
| 23 | + $listenerForStdclass = function (stdClass $e) { |
| 24 | + }; |
| 25 | + |
| 26 | + $provider = new AttachableListenerProvider(); |
| 27 | + $provider->listen(TestEvent::class, $listenerForTestEvent); |
| 28 | + $provider->listen(stdClass::class, $listenerForStdclass); |
| 29 | + |
| 30 | + $this->assertSame( |
| 31 | + [$listenerForTestEvent], |
| 32 | + iterator_to_array($provider->getListenersForEvent(new TestEvent())) |
| 33 | + ); |
| 34 | + |
| 35 | + $this->assertSame( |
| 36 | + [$listenerForStdclass], |
| 37 | + iterator_to_array($provider->getListenersForEvent(new stdClass())) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function testProviderDoesNotAllowDuplicateRegistration() |
| 42 | + { |
| 43 | + $listenerForTestEvent = function (TestEvent $e) { |
| 44 | + }; |
| 45 | + |
| 46 | + $provider = new AttachableListenerProvider(); |
| 47 | + $provider->listen(TestEvent::class, $listenerForTestEvent); |
| 48 | + $provider->listen(TestEvent::class, $listenerForTestEvent); |
| 49 | + |
| 50 | + $this->assertSame( |
| 51 | + [$listenerForTestEvent], |
| 52 | + iterator_to_array($provider->getListenersForEvent(new TestEvent())) |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
0 commit comments