From 84adcb1c3e10fa69c14f30f0ef1db90f7a76d73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Sun, 11 Nov 2012 18:31:52 +0100 Subject: [PATCH] [2.2][Routing] Added support for default attributes with default values of method params --- .../Routing/Loader/AnnotationClassLoader.php | 5 ++ .../Fixtures/AnnotatedClasses/BarClass.php | 19 +++++++ .../Loader/AnnotationClassLoaderTest.php | 51 ++++++++++++++++++- .../Loader/AnnotationDirectoryLoaderTest.php | 8 ++- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AnnotatedClasses/BarClass.php diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index d94f7d2c82..3ef006b057 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -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()); diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotatedClasses/BarClass.php b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotatedClasses/BarClass.php new file mode 100644 index 0000000000..a3882773c4 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotatedClasses/BarClass.php @@ -0,0 +1,19 @@ + + * + * 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') + { + } +} diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php index faeacfb2ff..002d7449b8 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php @@ -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); + } + } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php index 3877eeca74..a8ac733120 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php @@ -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'); }