Skip to content

Commit 84dfbd3

Browse files
committed
increase test coverage
1 parent c3e0c82 commit 84dfbd3

12 files changed

+254
-96
lines changed

ContentAwareGenerator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,21 @@ public function getRouteDebugMessage($name, array $parameters = array())
255255
}
256256

257257
/**
258-
* Unset the _locale parameter if it is there and not needed
258+
* If the _locale parameter is allowed by the requirements of the route
259+
* and it is the default locale, remove it from the parameters so that we
260+
* do not get an unneeded ?_locale= query string.
259261
*
260-
* @param SymfonyRoute $route
261-
* @param array $parameters
262+
* @param SymfonyRoute $route The route being generated.
263+
* @param array $parameters The parameters used, will be modified to
264+
* remove the _locale field if needed.
262265
*/
263266
protected function unsetLocaleIfNotNeeded(SymfonyRoute $route, array &$parameters)
264267
{
265268
$locale = $this->getLocale($parameters);
266269
if (null !== $locale) {
267-
if (preg_match('/'.$route->getRequirement('_locale').'/', $locale) && $locale == $route->getDefault('_locale')) {
270+
if (preg_match('/'.$route->getRequirement('_locale').'/', $locale)
271+
&& $locale == $route->getDefault('_locale')
272+
) {
268273
$compiledRoute = $route->compile();
269274
if (!in_array('_locale', $compiledRoute->getVariables())) {
270275
unset($parameters['_locale']);

Tests/DependencyInjection/Compiler/RegisterRouteEnhancersPassTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,41 @@ public function testRouteEnhancerPass()
2222
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
2323
$builder->expects($this->at(0))
2424
->method('hasDefinition')
25-
->will($this->returnValue(true));
25+
->with('cmf_routing.dynamic_router')
26+
->will($this->returnValue(true))
27+
;
2628
$builder->expects($this->once())
2729
->method('findTaggedServiceIds')
28-
->will($this->returnValue($serviceIds));
30+
->will($this->returnValue($serviceIds))
31+
;
2932
$builder->expects($this->once())
3033
->method('getDefinition')
3134
->with('cmf_routing.dynamic_router')
32-
->will($this->returnValue($definition));
35+
->will($this->returnValue($definition))
36+
;
3337

3438
$pass = new RegisterRouteEnhancersPass();
3539
$pass->process($builder);
40+
3641
$calls = $definition->getMethodCalls();
3742
$this->assertEquals(1, count($calls));
3843
$this->assertEquals('addRouteEnhancer', $calls[0][0]);
3944
}
45+
46+
/**
47+
* If there is no dynamic router defined in the container builder, nothing
48+
* should be processed.
49+
*/
50+
public function testNoDynamicRouter()
51+
{
52+
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
53+
$builder->expects($this->once())
54+
->method('hasDefinition')
55+
->with('cmf_routing.dynamic_router')
56+
->will($this->returnValue(false))
57+
;
58+
59+
$pass = new RegisterRouteEnhancersPass();
60+
$pass->process($builder);
61+
}
4062
}

Tests/DependencyInjection/Compiler/RegisterRoutersPassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testValidRouters($name, $priority = null)
3939
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'));
4040
$builder->expects($this->any())
4141
->method('hasDefinition')
42+
->with('cmf_routing.router')
4243
->will($this->returnValue(true));
4344

4445
$builder->expects($this->atLeastOnce())
@@ -61,4 +62,28 @@ public function getValidRoutersData()
6162
array('my_router', 0),
6263
);
6364
}
65+
66+
/**
67+
* If there is no chain router defined in the container builder, nothing
68+
* should be processed.
69+
*/
70+
public function testNoChainRouter()
71+
{
72+
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'));
73+
$builder->expects($this->once())
74+
->method('hasDefinition')
75+
->with('cmf_routing.router')
76+
->will($this->returnValue(false))
77+
;
78+
79+
$builder->expects($this->never())
80+
->method('findTaggedServiceIds')
81+
;
82+
$builder->expects($this->never())
83+
->method('getDefinition')
84+
;
85+
86+
$registerRoutersPass = new RegisterRoutersPass();
87+
$registerRoutersPass->process($builder);
88+
}
6489
}

Tests/Enhancer/FieldByClassEnhancerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class FieldByClassEnhancerTest extends CmfUnitTestCase
1818

1919
public function setUp()
2020
{
21-
$this->document = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Enhancer\\RouteObject');
21+
$this->document = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Enhancer\RouteObject');
2222

23-
$mapping = array('Symfony\\Cmf\\Component\\Routing\\Tests\\Enhancer\\RouteObject'
23+
$mapping = array('Symfony\Cmf\Component\Routing\Tests\Enhancer\RouteObject'
2424
=> 'cmf_content.controller:indexAction');
2525

2626
$this->mapper = new FieldByClassEnhancer('_content', '_controller', $mapping);

Tests/Enhancer/RouteContentEnhancerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RouteContentEnhancerTest extends CmfUnitTestCase
2020

2121
public function setUp()
2222
{
23-
$this->document = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Enhancer\\RouteObject',
23+
$this->document = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Enhancer\RouteObject',
2424
array('getContent', 'getRouteDefaults', 'getUrl'));
2525

2626
$this->mapper = new RouteContentEnhancer(RouteObjectInterface::ROUTE_OBJECT, '_content');
@@ -64,7 +64,7 @@ public function testNoContent()
6464

6565
public function testNoCmfRoute()
6666
{
67-
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->buildMock('Symfony\\Component\\Routing\\Route'));
67+
$defaults = array(RouteObjectInterface::ROUTE_OBJECT => $this->buildMock('Symfony\Component\Routing\Route'));
6868
$this->assertEquals($defaults, $this->mapper->enhance($defaults, $this->request));
6969
}
7070
}

Tests/NestedMatcher/NestedMatcherTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ class NestedMatcherTest extends CmfUnitTestCase
2121

2222
public function setUp()
2323
{
24-
$this->provider = $this->buildMock("Symfony\\Cmf\\Component\\Routing\\RouteProviderInterface");
25-
$this->routeFilter1 = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\RouteFilterInterface');
26-
$this->routeFilter2 = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\RouteFilterInterface');
27-
$this->finalMatcher = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\FinalMatcherInterface');
24+
$this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
25+
$this->routeFilter1 = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface');
26+
$this->routeFilter2 = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface');
27+
$this->finalMatcher = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\FinalMatcherInterface');
2828
}
2929

3030
public function testNestedMatcher()
3131
{
3232
$request = Request::create('/path/one');
3333
$routeCollection = new RouteCollection();
34-
$route = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
34+
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
3535
$routeCollection->add('route', $route);
3636

3737
$this->provider->expects($this->once())
@@ -71,10 +71,10 @@ public function testNestedMatcherPriority()
7171
{
7272
$request = Request::create('/path/one');
7373
$routeCollection = new RouteCollection();
74-
$route = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
74+
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
7575
$routeCollection->add('route', $route);
7676

77-
$wrongProvider = $this->buildMock("Symfony\\Cmf\\Component\\Routing\\RouteProviderInterface");
77+
$wrongProvider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
7878
$wrongProvider->expects($this->never())
7979
->method('getRouteCollectionForRequest')
8080
;
@@ -123,7 +123,7 @@ public function testProviderNoMatch()
123123

124124
$matcher = new NestedMatcher($this->provider, $this->finalMatcher);
125125

126-
$this->setExpectedException('Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException');
126+
$this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
127127
$matcher->matchRequest($request);
128128
}
129129

Tests/NestedMatcher/UrlMatcherTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class UrlMatcherTest extends CmfUnitTestCase
2424

2525
public function setUp()
2626
{
27-
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteKey', 'compile'));
28-
$this->routeCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
27+
$this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'getRouteKey', 'compile'));
28+
$this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
2929

30-
$this->context = $this->buildMock('Symfony\\Component\\Routing\\RequestContext');
30+
$this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
3131
$this->request = Request::create($this->url);
3232

3333
$this->matcher = new UrlMatcher(new RouteCollection(), $this->context);
@@ -51,7 +51,7 @@ public function doTestMatchRouteKey($routeKey)
5151
;
5252
$this->routeCompiled->expects($this->atLeastOnce())
5353
->method('getRegex')
54-
->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#'))
54+
->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
5555
;
5656
$this->routeDocument->expects($this->atLeastOnce())
5757
->method('compile')
@@ -66,12 +66,12 @@ public function doTestMatchRouteKey($routeKey)
6666
->will($this->returnValue(array('foo' => 'bar')))
6767
;
6868

69-
$mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
69+
$mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
7070
$mockCompiled->expects($this->any())
7171
->method('getStaticPrefix')
7272
->will($this->returnValue('/no/match'))
7373
;
74-
$mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
74+
$mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
7575
$mockRoute->expects($this->any())
7676
->method('compile')
7777
->will($this->returnValue($mockCompiled))
@@ -100,9 +100,9 @@ public function testMatchNoRouteObject()
100100
;
101101
$this->routeCompiled->expects($this->atLeastOnce())
102102
->method('getRegex')
103-
->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#'))
103+
->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
104104
;
105-
$this->routeDocument = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
105+
$this->routeDocument = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
106106
$this->routeDocument->expects($this->atLeastOnce())
107107
->method('compile')
108108
->will($this->returnValue($this->routeCompiled))
@@ -115,12 +115,12 @@ public function testMatchNoRouteObject()
115115
->will($this->returnValue(array('foo' => 'bar')))
116116
;
117117

118-
$mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute');
118+
$mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
119119
$mockCompiled->expects($this->any())
120120
->method('getStaticPrefix')
121121
->will($this->returnValue('/no/match'))
122122
;
123-
$mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock();
123+
$mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
124124
$mockRoute->expects($this->any())
125125
->method('compile')
126126
->will($this->returnValue($mockCompiled))

0 commit comments

Comments
 (0)