Ver código fonte

[Routing] fixed a bug when a default value is an integer

A default value is always a string in the context of routing.
Fabien Potencier 14 anos atrás
pai
commit
a1d8c70890

+ 5 - 2
src/Symfony/Component/Routing/Route.php

@@ -161,7 +161,10 @@ class Route
      */
     public function setDefaults(array $defaults)
     {
-        $this->defaults = $defaults;
+        $this->defaults = array();
+        foreach ($defaults as $name => $default) {
+            $this->defaults[(string) $name] = (string) $default;
+        }
 
         return $this;
     }
@@ -202,7 +205,7 @@ class Route
      */
     public function setDefault($name, $default)
     {
-        $this->defaults[$name] = $default;
+        $this->defaults[(string) $name] = (string) $default;
 
         return $this;
     }

+ 7 - 0
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -191,6 +191,13 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
     }
 
+    public function testWithAnIntegerAsADefaultValue()
+    {
+        $routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0)));
+
+        $this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
+    }
+
     protected function getGenerator(RouteCollection $routes, array $parameters = array())
     {
         $context = new RequestContext('/app.php');