|
| 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; |
| 11 | + |
| 12 | +use Phly\EventDispatcher\Exception\InvalidListenerException; |
| 13 | +use Phly\EventDispatcher\LazyListener; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Container\ContainerInterface; |
| 16 | + |
| 17 | +class LazyListenerTest extends TestCase |
| 18 | +{ |
| 19 | + public function setUp() |
| 20 | + { |
| 21 | + $this->event = new TestAsset\TestEvent(); |
| 22 | + $this->container = $this->prophesize(ContainerInterface::class); |
| 23 | + } |
| 24 | + |
| 25 | + public function testRaisesExceptionIfServiceReturnedIsNeitherAnObjectNorCallable() |
| 26 | + { |
| 27 | + $this->container->get('LazyService')->willReturn('not-callable'); |
| 28 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService'); |
| 29 | + |
| 30 | + $this->expectException(InvalidListenerException::class); |
| 31 | + $lazyListener($this->event); |
| 32 | + } |
| 33 | + |
| 34 | + public function testInvokesNonObjectCallableListenerReturnedByContainer() |
| 35 | + { |
| 36 | + $this->container->get('LazyService')->willReturn(__NAMESPACE__ . '\TestAsset\listenerFunction'); |
| 37 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService'); |
| 38 | + |
| 39 | + $this->assertNull($lazyListener($this->event)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testRaisesExceptionIfObjectServiceReturnedIsNotCallableAndNoMethodProvided() |
| 43 | + { |
| 44 | + $this->container->get('LazyService')->willReturn((object) []); |
| 45 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService'); |
| 46 | + |
| 47 | + $this->expectException(InvalidListenerException::class); |
| 48 | + $lazyListener($this->event); |
| 49 | + } |
| 50 | + |
| 51 | + public function testInvokesCallableListenerReturnedByContainerWhenNoMethodProvided() |
| 52 | + { |
| 53 | + $this->container->get('LazyService')->willReturn(new TestAsset\Listener()); |
| 54 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService'); |
| 55 | + |
| 56 | + $this->assertNull($lazyListener($this->event)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testRaisesExceptionIfObjectServiceReturnedIsNotCallableViaMethodProvided() |
| 60 | + { |
| 61 | + $this->container->get('LazyService')->willReturn(new TestAsset\Listener()); |
| 62 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService', 'not-a-real-method'); |
| 63 | + |
| 64 | + $this->expectException(InvalidListenerException::class); |
| 65 | + $lazyListener($this->event); |
| 66 | + } |
| 67 | + |
| 68 | + public function testInvokesMethodOnObjectListenerReturnedByContainer() |
| 69 | + { |
| 70 | + $this->container->get('LazyService')->willReturn(new TestAsset\Listener()); |
| 71 | + $lazyListener = new LazyListener($this->container->reveal(), 'LazyService', 'onTest'); |
| 72 | + |
| 73 | + $this->assertNull($lazyListener($this->event)); |
| 74 | + } |
| 75 | +} |
0 commit comments