|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Tmdb\Laravel\Adapters\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\Event; |
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 17 | + |
| 18 | +use Prophecy\Prophecy\MethodProphecy; |
| 19 | + |
| 20 | +abstract class AbstractEventDispatcherTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + const EVENT = 'foo'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var EventDispatcher |
| 26 | + */ |
| 27 | + protected $dispatcher; |
| 28 | + |
| 29 | + protected $laravel; |
| 30 | + protected $symfony; |
| 31 | + |
| 32 | + private $listener; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->dispatcher = $this->createEventDispatcher(); |
| 37 | + } |
| 38 | + |
| 39 | + abstract protected function createEventDispatcher(); |
| 40 | + |
| 41 | + /** @test */ |
| 42 | + public function it_dispatches_events_through_both_laravel_and_symfony() |
| 43 | + { |
| 44 | + $this->laravel->fire(static::EVENT, null)->shouldBeCalled(); |
| 45 | + $this->symfony->dispatch(static::EVENT, null)->shouldBeCalled(); |
| 46 | + |
| 47 | + $this->dispatcher->dispatch(static::EVENT); |
| 48 | + } |
| 49 | + |
| 50 | + /** @test */ |
| 51 | + public function it_returns_the_event_returned_by_the_symfony_dispatcher() |
| 52 | + { |
| 53 | + $this->symfony->dispatch(static::EVENT, null)->willReturn('bar'); |
| 54 | + $this->assertEquals('bar', $this->dispatcher->dispatch(static::EVENT)); |
| 55 | + } |
| 56 | + |
| 57 | + /** @test */ |
| 58 | + public function it_adds_listeners_to_the_symfony_dispatcher() |
| 59 | + { |
| 60 | + $this->dispatcher->addListener(static::EVENT, 'listener', 1); |
| 61 | + $this->symfony->addListener(static::EVENT, 'listener', 1)->shouldHaveBeenCalled(); |
| 62 | + } |
| 63 | + |
| 64 | + /** @test */ |
| 65 | + public function it_adds_a_subscriber_to_the_symfony_dispatcher() |
| 66 | + { |
| 67 | + $subscriber = $this->prophesize('Symfony\Component\EventDispatcher\EventSubscriberInterface'); |
| 68 | + $this->dispatcher->addSubscriber($subscriber->reveal()); |
| 69 | + $this->symfony->addSubscriber($subscriber->reveal())->shouldHaveBeenCalled(); |
| 70 | + } |
| 71 | + |
| 72 | + /** @test */ |
| 73 | + public function it_removes_listeners_from_the_symfony_dispathcer() |
| 74 | + { |
| 75 | + $this->dispatcher->removeListener(static::EVENT, 'listener'); |
| 76 | + $this->symfony->removeListener(static::EVENT, 'listener')->shouldHaveBeenCalled(); |
| 77 | + } |
| 78 | + |
| 79 | + /** @test */ |
| 80 | + public function it_removes_subscriptions_from_the_symfony_dispathcer() |
| 81 | + { |
| 82 | + $subscriber = $this->prophesize('Symfony\Component\EventDispatcher\EventSubscriberInterface'); |
| 83 | + $this->dispatcher->removeSubscriber($subscriber->reveal()); |
| 84 | + $this->symfony->removeSubscriber($subscriber->reveal())->shouldHaveBeenCalled(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @test |
| 89 | + * We are not checking Laravel's listeners as its interface does not contain a getListeners function |
| 90 | + */ |
| 91 | + public function it_gets_listeners_from_the_symfony_dispatcher() |
| 92 | + { |
| 93 | + $this->symfony->getListeners(static::EVENT)->willReturn(['bar']); |
| 94 | + $this->assertEquals(['bar'], $this->dispatcher->getListeners(static::EVENT)); |
| 95 | + } |
| 96 | + |
| 97 | + /** @test */ |
| 98 | + public function it_asks_the_symfony_dispatcher_if_it_has_a_listener() |
| 99 | + { |
| 100 | + $this->symfony->hasListeners(static::EVENT)->willReturn(true); |
| 101 | + $this->assertTrue($this->dispatcher->hasListeners(static::EVENT)); |
| 102 | + } |
| 103 | + |
| 104 | + /** @test */ |
| 105 | + public function it_asks_the_laravel_dispatcher_if_it_has_a_listener() |
| 106 | + { |
| 107 | + $this->symfony->hasListeners(static::EVENT)->willReturn(false); |
| 108 | + $this->laravel->hasListeners(static::EVENT)->willReturn(true); |
| 109 | + $this->assertTrue($this->dispatcher->hasListeners(static::EVENT)); |
| 110 | + } |
| 111 | + |
| 112 | + /** @test */ |
| 113 | + public function it_asks_both_the_symfony_and_laravel_dispatcher_if_it_has_a_listener() |
| 114 | + { |
| 115 | + $this->symfony->hasListeners(static::EVENT)->willReturn(false); |
| 116 | + $this->laravel->hasListeners(static::EVENT)->willReturn(false); |
| 117 | + $this->assertFalse($this->dispatcher->hasListeners(static::EVENT)); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @test |
| 122 | + * We are not checking Laravel's listeners as its interface does not contain a getListenerPriority function |
| 123 | + */ |
| 124 | + public function it_asks_the_symfony_dispatcher_for_a_listeners_priority() |
| 125 | + { |
| 126 | + $this->symfony->getListenerPriority(static::EVENT, 'listener')->willReturn(100); |
| 127 | + $this->assertEquals(100, $this->dispatcher->getListenerPriority(static::EVENT, 'listener')); |
| 128 | + } |
| 129 | +} |
0 commit comments