Skip to content

Commit cf909e4

Browse files
committed
Merge pull request #136 from symfony-cmf/event
support event handling for dynamic router
2 parents df34732 + ac23f39 commit cf909e4

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
* **2013-07-31**: [EventDispatcher] Added events to the dynamic router at the start of match and matchRequest
45
* **2013-07-29**: [DependencyInjection] restructured `phpcr_provider` config into `persistence` -> `phpcr` to match other Bundles
56
* **2013-07-28**: [DependencyInjection] added `enabled` flag to `phpcr_provider` config
67
* **2013-07-26**: [Model] Removed setRouteContent and getRouteContent, use setContent and getContent instead.

Resources/config/routing-dynamic.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<argument type="service" id="cmf_routing.nested_matcher" />
5656
<argument type="service" id="cmf_routing.generator" />
5757
<argument>%cmf_routing.uri_filter_regexp%</argument>
58+
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
5859
<call method="setContainer"><argument type="service" id="service_container"/></call>
5960
<call method="addRouteEnhancer"><argument type="service" id="cmf_routing.enhancer_route_content"/></call>
6061
</service>

Tests/Unit/Routing/ContentAwareGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Routing;
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Routing;
44

55
use Symfony\Component\HttpFoundation\Request;
66

Tests/Unit/Routing/DynamicRouterTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3-
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Routing;
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Routing;
44

5+
use Symfony\Cmf\Component\Routing\Event\Events;
6+
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
57
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
69
use Symfony\Component\HttpFoundation\Request;
710

811
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
@@ -20,6 +23,8 @@ class DynamicRouterTest extends CmfUnitTestCase
2023
protected $context;
2124
/** @var Request */
2225
protected $request;
26+
/** @var EventDispatcherInterface */
27+
protected $eventDispatcher;
2328
protected $container;
2429

2530
public function setUp()
@@ -36,8 +41,8 @@ public function setUp()
3641
$this->request = Request::create('/foo');
3742
$this->container = $this->buildMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
3843
$this->context = $this->buildMock('Symfony\\Component\\Routing\\RequestContext');
39-
40-
$this->router = new DynamicRouter($this->context, $this->matcher, $this->generator);
44+
$this->eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
45+
$this->router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $this->eventDispatcher);
4146
$this->router->setContainer($this->container);
4247
}
4348

@@ -57,6 +62,11 @@ public function testMatch()
5762
->will($this->returnValue($this->request))
5863
;
5964

65+
$this->eventDispatcher->expects($this->once())
66+
->method('dispatch')
67+
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
68+
;
69+
6070
$parameters = $this->router->match('/foo');
6171
$this->assertEquals(array('foo' => 'bar'), $parameters);
6272

@@ -65,10 +75,9 @@ public function testMatch()
6575

6676
public function testMatchRequest()
6777
{
68-
$this->container->expects($this->once())
69-
->method('get')
70-
->with('request')
71-
->will($this->returnValue($this->request))
78+
$this->eventDispatcher->expects($this->once())
79+
->method('dispatch')
80+
->with(Events::PRE_DYNAMIC_MATCH_REQUEST, $this->equalTo(new RouterMatchEvent($this->request)))
7281
;
7382

7483
$parameters = $this->router->matchRequest($this->request);
@@ -88,6 +97,22 @@ public function testMatchNoRequest()
8897
->will($this->returnValue(null))
8998
;
9099

100+
$this->eventDispatcher->expects($this->once())
101+
->method('dispatch')
102+
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
103+
;
104+
91105
$this->router->match('/foo');
92106
}
107+
108+
public function testEventOptional()
109+
{
110+
$router = new DynamicRouter($this->context, $this->matcher, $this->generator);
111+
112+
$parameters = $router->matchRequest($this->request);
113+
$this->assertEquals(array('foo' => 'bar'), $parameters);
114+
115+
$this->assertRequestAttributes($this->request);
116+
}
117+
93118
}

0 commit comments

Comments
 (0)