Skip to content

Commit 4fec330

Browse files
committed
Merge pull request #25 from MarkRedeman/add-get-listeners-priority
Make EventDispatcherAdapter compatible with event-dispatcher v3.0.
2 parents 3736bdd + 2017ae1 commit 4fec330

5 files changed

+160
-178
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"php-tmdb/api": "~2.0"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "~4.0",
19-
"symfony/event-dispatcher": "~2.0",
18+
"phpunit/phpunit": "~4.5",
19+
"symfony/event-dispatcher": "~2.8|~3.0",
2020
"illuminate/events": "~5.0",
2121
"doctrine/cache": ">=1.3.0",
2222
"monolog/monolog": ">=1.7.0"

src/Adapters/EventDispatcherAdapter.php

+19-16
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
* This adapter provides a Laravel integration for applications
1515
* using the Symfony EventDispatcherInterface
1616
* It passes any request on to a Symfony Dispatcher and only
17-
* uses the Laravel Dispatcher only when dispatching events
17+
* uses the Laravel Dispatcher when dispatching events
1818
*/
1919
abstract class EventDispatcherAdapter implements SymfonyDispatcher
2020
{
2121

2222
/**
2323
* The Laravel Events Dispatcher
24-
* @var Illuminate\Contracts\Events\Dispatcher or Illuminate\Events\Dispatcher
24+
* @var \Illuminate\Contracts\Events\Dispatcher or \Illuminate\Events\Dispatcher
2525
*/
2626
protected $laravelDispatcher;
2727

2828
/**
2929
* The Symfony Event Dispatcher
30-
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface
30+
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
3131
*/
3232
protected $symfonyDispatcher;
3333

@@ -46,20 +46,8 @@ abstract class EventDispatcherAdapter implements SymfonyDispatcher
4646
*/
4747
public function dispatch($eventName, Event $event = null)
4848
{
49-
if ($event == null)
50-
{
51-
$event = new Event();
52-
}
53-
54-
$event->setName($eventName);
55-
$event->setDispatcher($this);
56-
5749
$this->laravelDispatcher->fire($eventName, $event);
58-
$this->symfonyDispatcher->dispatch($eventName, $event);
59-
60-
$event->setDispatcher($this);
61-
62-
return $event;
50+
return $this->symfonyDispatcher->dispatch($eventName, $event);
6351
}
6452

6553
/**
@@ -137,4 +125,19 @@ public function hasListeners($eventName = null)
137125
return ($this->symfonyDispatcher->hasListeners($eventName) ||
138126
$this->laravelDispatcher->hasListeners($eventName));
139127
}
128+
129+
/**
130+
* Gets the listener priority for a specific event.
131+
*
132+
* Returns null if the event or the listener does not exist.
133+
*
134+
* @param string $eventName The name of the event
135+
* @param callable $listener The listener
136+
*
137+
* @return int|null The event listener priority
138+
*/
139+
public function getListenerPriority($eventName, $listener)
140+
{
141+
return $this->symfonyDispatcher->getListenerPriority($eventName, $listener);
142+
}
140143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

tests/Adapters/EventDispatcherTestLaravel4.php

+5-80
Original file line numberDiff line numberDiff line change
@@ -7,92 +7,17 @@
77
namespace Tmdb\Laravel\Adapters\Tests;
88

99
use Tmdb\Laravel\Adapters\EventDispatcherLaravel4 as AdapterDispatcher;
10-
use Illuminate\Events\Dispatcher as LaravelDispatcher;
11-
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
12-
13-
use Symfony\Component\EventDispatcher\Tests\AbstractEventDispatcherTest;
14-
use Symfony\Component\EventDispatcher\Tests\TestEventListener;
15-
use Symfony\Component\EventDispatcher\Tests\TestWithDispatcher;
1610

1711
class EventDispatcherTestLaravel4 extends AbstractEventDispatcherTest
1812
{
19-
private $laravelDispatcher;
20-
private $symfonyDispatcher;
21-
private $dispatcher;
22-
23-
protected function setUp()
24-
{
25-
parent::setUp();
26-
$this->dispatcher = $this->createEventDispatcher();
27-
$this->listener = new TestEventListener();
28-
}
29-
3013
protected function createEventDispatcher()
3114
{
32-
$this->laravelDispatcher = new LaravelDispatcher;
33-
$this->symfonyDispatcher = new SymfonyDispatcher;
15+
$this->laravel = $this->prophesize('Illuminate\Events\Dispatcher');
16+
$this->symfony = $this->prophesize('Symfony\Component\EventDispatcher\EventDispatcher');
17+
3418
return new AdapterDispatcher(
35-
$this->laravelDispatcher,
36-
$this->symfonyDispatcher
19+
$this->laravel->reveal(),
20+
$this->symfony->reveal()
3721
);
3822
}
39-
40-
public function testTheEventIsDispatchedTroughLaravel()
41-
{
42-
$dispatched = false;
43-
$this->laravelDispatcher->listen('test', function ($event) use (&$dispatched) {
44-
$dispatched = true;
45-
});
46-
$this->dispatcher->dispatch('test');
47-
$this->assertTrue($dispatched);
48-
}
49-
50-
public function testItKnowsIfTheLaravelDispatcherHasListeners()
51-
{
52-
$this->laravelDispatcher->listen('pre.foo', function() {});
53-
$this->laravelDispatcher->listen('post.foo', function() {});
54-
55-
$this->assertTrue($this->dispatcher->hasListeners('pre.foo'));
56-
$this->assertTrue($this->dispatcher->hasListeners('post.foo'));
57-
}
58-
59-
60-
/**
61-
* The following two tests are copies of the same tests from the
62-
* AbstractEventDispatcherTest, however instead of asserting
63-
* that the event's dispatcher is the adapter dispatcher, we
64-
* assert that it is the Symfony Dispatcher
65-
*/
66-
67-
public function testLegacyEventReceivesTheDispatcherInstance()
68-
{
69-
$dispatcher = null;
70-
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
71-
$dispatcher = $event->getDispatcher();
72-
});
73-
$this->dispatcher->dispatch('test');
74-
$this->assertSame($this->symfonyDispatcher, $dispatcher);
75-
}
76-
77-
public function testEventReceivesTheDispatcherInstance()
78-
{
79-
$dispatcher = null;
80-
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
81-
$dispatcher = $event->getDispatcher();
82-
});
83-
$this->dispatcher->dispatch('test');
84-
$this->assertSame($this->symfonyDispatcher, $dispatcher);
85-
}
86-
87-
public function testEventReceivesTheDispatcherInstanceAsArgument()
88-
{
89-
$listener = new TestWithDispatcher();
90-
$this->dispatcher->addListener('test', array($listener, 'foo'));
91-
$this->assertNull($listener->name);
92-
$this->assertNull($listener->dispatcher);
93-
$this->dispatcher->dispatch('test');
94-
$this->assertEquals('test', $listener->name);
95-
$this->assertSame($this->symfonyDispatcher, $listener->dispatcher);
96-
}
97-
9823
}

0 commit comments

Comments
 (0)