[2.2][Routing] Added support for default attributes with default values of method params

This commit is contained in:
Grégoire Pineau 2012-11-11 18:31:52 +01:00 committed by Fabien Potencier
parent b337655feb
commit 84adcb1c3e
4 changed files with 81 additions and 2 deletions

View File

@ -149,6 +149,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
}
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if ($param->isOptional()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
$options = array_merge($globals['options'], $annot->getOptions());

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
class BarClass
{
public function routeAction($arg1, $arg2 = 'defaultValue2', $arg3 = 'defaultValue3')
{
}
}

View File

@ -21,7 +21,8 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
{
parent::setUp();
$this->loader = $this->getClassLoader($this->getReader());
$this->reader = $this->getReader();
$this->loader = $this->getClassLoader($this->reader);
}
/**
@ -71,4 +72,52 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
$this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
}
public function getLoadTests()
{
return array(
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('name'=>'route1'),
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('name'=>'route1', 'defaults' => array('arg2' => 'foo')),
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
),
);
}
/**
* @dataProvider getLoadTests
*/
public function testLoad($className, $routeDatas = array(), $methodArgs = array())
{
$routeDatas = array_replace(array(
'name' => 'route',
'pattern' => '/',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
), $routeDatas);
$this->reader
->expects($this->once())
->method('getMethodAnnotations')
->will($this->returnValue(array($this->getAnnotedRoute($routeDatas))))
;
$routeCollection = $this->loader->load($className);
$route = $routeCollection->get($routeDatas['name']);
$this->assertSame($routeDatas['pattern'], $route->getPattern(), '->load preserves pattern annotation');
$this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
$this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
$this->assertSame(array_replace($routeDatas['defaults'], $methodArgs), $route->getDefaults(), '->load preserves defaults annotation');
}
private function getAnnotedRoute($datas)
{
return new \Symfony\Component\Routing\Annotation\Route($datas);
}
}

View File

@ -29,7 +29,13 @@ class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
public function testLoad()
{
$this->reader->expects($this->once())->method('getClassAnnotation');
$this->reader->expects($this->exactly(2))->method('getClassAnnotation');
$this->reader
->expects($this->any())
->method('getMethodAnnotations')
->will($this->returnValue(array()))
;
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
}