|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/phly/phly-event-dispatcher for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2018 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 Phly\EventDispatcher; |
| 11 | + |
| 12 | +use Phly\EventDispatcher\ErrorEmittingDispatcher; |
| 13 | +use Phly\EventDispatcher\EventDispatcher; |
| 14 | +use Phly\EventDispatcher\EventDispatcherFactory; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Psr\Container\ContainerInterface; |
| 17 | +use Psr\EventDispatcher\ListenerProviderInterface; |
| 18 | +use stdClass; |
| 19 | +use TypeError; |
| 20 | + |
| 21 | +class EventDispatcherFactoryTest extends TestCase |
| 22 | +{ |
| 23 | + public function setUp() |
| 24 | + { |
| 25 | + $this->provider = $this->prophesize(ListenerProviderInterface::class)->reveal(); |
| 26 | + $this->container = $this->prophesize(ContainerInterface::class); |
| 27 | + $this->container->get(ListenerProviderInterface::class)->willReturn($this->provider); |
| 28 | + $this->factory = new EventDispatcherFactory(); |
| 29 | + } |
| 30 | + |
| 31 | + public function testFactoryRaisesTypeErrorIfReturnedServiceIsNotAnEventDispatcher() |
| 32 | + { |
| 33 | + $this->expectException(TypeError::class); |
| 34 | + ($this->factory)($this->container->reveal(), stdClass::class); |
| 35 | + } |
| 36 | + |
| 37 | + public function knownDispatcherTypes() : iterable |
| 38 | + { |
| 39 | + yield EventDispatcher::class => [EventDispatcher::class]; |
| 40 | + yield ErrorEmittingDispatcher::class => [ErrorEmittingDispatcher::class]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dataProvider knownDispatcherTypes |
| 45 | + */ |
| 46 | + public function testFactoryCanCreateEventDispatcher(string $type) |
| 47 | + { |
| 48 | + $dispatcher = ($this->factory)($this->container->reveal(), $type); |
| 49 | + $this->assertInstanceOf($type, $dispatcher); |
| 50 | + $this->assertAttributeSame($this->provider, 'listenerProvider', $dispatcher); |
| 51 | + } |
| 52 | +} |
0 commit comments