8000 [2.2][Routing] Added support for default attributes with default valu… · symfony/symfony@84adcb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84adcb1

Browse files
lyrixxfabpot
authored andcommitted
[2.2][Routing] Added support for default attributes with default values of method params
1 parent b337655 commit 84adcb1

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
149149
}
150150

151151
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
152+
foreach ($method->getParameters() as $param) {
153+
if ($param->isOptional()) {
154+
$defaults[$param->getName()] = $param->getDefaultValue();
155+
}
156+
}
152157
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
153158
$options = array_merge($globals['options'], $annot->getOptions());
154159

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
13+
14+
class BarClass
15+
{
16+
public function routeAction($arg1, $arg2 = 'defaultValue2', $arg3 = 'defaultValue3')
17+
{
18+
}
19+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ protected function setUp()
2121
{
2222
parent::setUp();
2323

24-
$this->loader = $this->getClassLoader($this->getReader());
24+
$this->reader = $this->getReader();
25+
$this->loader = $this->getClassLoader($this->reader);
2526
}
2627

2728
/**
@@ -71,4 +72,52 @@ public function testSupportsChecksTypeIfSpecified()
7172
$this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
7273
}
7374

75+
public function getLoadTests()
76+
{
77+
return array(
78+
array(
79+
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
80+
array('name'=>'route1'),
81+
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
82+
),
83+
array(
84+
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
85+
array('name'=>'route1', 'defaults' => array('arg2' => 'foo')),
86+
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
87+
),
88+
);
89+
}
90+
91+
/**
92+
* @dataProvider getLoadTests
93+
*/
94+
public function testLoad($className, $routeDatas = array(), $methodArgs = array())
95+
{
96+
$routeDatas = array_replace(array(
97+
'name' => 'route',
98+
'pattern' => '/',
99+
'requirements' => array(),
100+
'options' => array(),
101+
'defaults' => array(),
102+
), $routeDatas);
103+
104+
$this->reader
105+
->expects($this->once())
106+
->method('getMethodAnnotations')
107+
->will($this->returnValue(array($this->getAnnotedRoute($routeDatas))))
108+
;
109+
$routeCollection = $this->loader->load($className);
110+
$route = $routeCollection->get($routeDatas['name']);
111+
112+
$this->assertSame($routeDatas['pattern'], $route->getPattern(), '->load preserves pattern annotation');
113+
$this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
114+
$this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
115+
$this->assertSame(array_replace($routeDatas['defaults'], $methodArgs), $route->getDefaults(), '->load preserves defaults annotation');
116+
}
117+
118+
private function getAnnotedRoute($datas)
119+
{
120+
return new \Symfony\Component\Routing\Annotation\Route($datas);
121+
}
122+
74123
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ protected function setUp()
2929

3030
public function testLoad()
3131
{
32-
$this->reader->expects($this->once())->method('getClassAnnotation');
32+
$this->reader->expects($this->exactly(2))->method('getClassAnnotation');
33+
34+
$this->reader
35+
->expects($this->any())
36+
->method('getMethodAnnotations')
37+
->will($this->returnValue(array()))
38+
;
3339

3440
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
3541
}

0 commit comments

Comments
 (0)
0