8000 add test for non stringable object · symfony/symfony@31d6937 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31d6937

Browse files
committed
add test for non stringable object
1 parent 57d6196 commit 31d6937

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
298298
if ($v instanceof \stdClass) {
299299
$v = (array) $v;
300300
array_walk_recursive($v, $caster);
301-
} elseif (\is_object($v)) {
301+
} elseif (\is_object($v) && method_exists($v, '__toString')) {
302302
$v = (string) $v;
303303
}
304304
});

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testRelativeUrlWithExtraParameters(string $expectedQueryString,
112112
$routes = $this->getRoutes('test', new Route('/testing'));
113113
$url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_PATH);
114114

115-
$this->assertSame(sprintf('/app.php/testing?%s', $expectedQueryString), $url);
115+
$this->assertSame(sprintf('/app.php/testing%s', $expectedQueryString), $url);
116116
}
117117

118118
/**
@@ -123,7 +123,7 @@ public function testAbsoluteUrlWithExtraParameters(string $expectedQueryString,
123123
$routes = $this->getRoutes('test', new Route('/testing'));
124124
$url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_URL);
125125

126-
$this->assertSame(sprintf('http://localhost/app.php/testing?%s', $expectedQueryString), $url);
126+
$this->assertSame(sprintf('http://localhost/app.php/testing%s', $expectedQueryString), $url);
127127
}
128128

129129
public function valuesProvider(): array
@@ -135,24 +135,18 @@ public function valuesProvider(): array
135135
$nestedStdClass->nested = $stdClass;
136136

137137
return [
138-
'string' => ['foo=bar', 'foo', 'bar'],
139-
'boolean-false' => ['foo=0', 'foo', false],
140-
'boolean-true' => ['foo=1', 'foo', true],
141-
'object implementing __toString()' => ['foo=bar', 'foo', new StringableObject()],
142-
'object implementing __toString() in nested array' => ['foo%5Bbaz%5D=bar', 'foo', ['baz' => new StringableObject()]],
143-
'stdClass' => ['foo%5Bbaz%5D=bar', 'foo', $stdClass],
144-
'stdClass in nested stdClass' => ['foo%5Bnested%5D%5Bbaz%5D=bar', 'foo', $nestedStdClass],
138+
'null' => ['', 'foo', null],
139+
'string' => ['?foo=bar', 'foo', 'bar'],
140+
'boolean-false' => ['?foo=0', 'foo', false],
141+
'boolean-true' => ['?foo=1', 'foo', true],
142+
'object implementing __toString()' => ['?foo=bar', 'foo', new StringableObject()],
143+
'object implementing __toString() in nested array' => ['?foo%5Bbaz%5D=bar', 'foo', ['baz' => new StringableObject()]],
144+
'stdClass' => ['foo%5Bbaz%5D=bar', '?foo', $stdClass],
145+
'stdClass in nested stdClass' => ['?foo%5Bnested%5D%5Bbaz%5D=bar', 'foo', $nestedStdClass],
146+
'not stringable object' => ['', 'foo', new NotStringableObject()],
145147
];
146148
}
147149

148-
public function testUrlWithNullExtraParameters()
149-
{
150-
$routes = $this->getRoutes('test', new Route('/testing'));
151-
$url = $this->getGenerator($routes)->generate('test', ['foo' => null], UrlGeneratorInterface::ABSOLUTE_URL);
152-
153-
$this->assertEquals('http://localhost/app.php/testing', $url);
154-
}
155-
156150
public function testUrlWithExtraParametersFromGlobals()
157151
{
158152
$routes = $this->getRoutes('test', new Route('/testing'));
@@ -925,3 +919,7 @@ public function __toString()
925919
return 'bar';
926920
}
927921
}
922+
923+
class NotStringableObject
924+
{
925+
}

0 commit comments

Comments
 (0)
0