Skip to content

Commit 962bf47

Browse files
OskarStarknicolas-grekas
authored andcommitted
Add check and tests for public properties
1 parent 714105e commit 962bf47

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

Tests/Generator/UrlGeneratorTest.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,50 @@ public function testNotPassedOptionalParameterInBetween()
104104
$this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test'));
105105
}
106106

107-
public function testRelativeUrlWithExtraParameters()
107+
/**
108+
* @dataProvider valuesProvider
109+
*/
110+
public function testRelativeUrlWithExtraParameters(string $expectedQueryString, string $parameter, $value)
108111
{
109112
$routes = $this->getRoutes('test', new Route('/testing'));
110-
$url = $this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_PATH);
113+
$url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_PATH);
111114

112-
$this->assertEquals('/app.php/testing?foo=bar', $url);
115+
$this->assertSame('/app.php/testing'.$expectedQueryString, $url);
113116
}
114117

115-
public function testAbsoluteUrlWithExtraParameters()
118+
/**
119+
* @dataProvider valuesProvider
120+
*/
121+
public function testAbsoluteUrlWithExtraParameters(string $expectedQueryString, string $parameter, $value)
116122
{
117123
$routes = $this->getRoutes('test', new Route('/testing'));
118-
$url = $this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL);
124+
$url = $this->getGenerator($routes)->generate('test', [$parameter => $value], UrlGeneratorInterface::ABSOLUTE_URL);
119125

120-
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
126+
$this->assertSame('http://localhost/app.php/testing'.$expectedQueryString, $url);
121127
}
122128

123-
public function testUrlWithNullExtraParameters()
129+
public function valuesProvider(): array
124130
{
125-
$routes = $this->getRoutes('test', new Route('/testing'));
126-
$url = $this->getGenerator($routes)->generate('test', ['foo' => null], UrlGeneratorInterface::ABSOLUTE_URL);
131+
$stdClass = new \stdClass();
132+
$stdClass->baz = 'bar';
127133

128-
$this->assertEquals('http://localhost/app.php/testing', $url);
134+
$nestedStdClass = new \stdClass();
135+
$nestedStdClass->nested = $stdClass;
136+
137+
return [
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() but has public property' => ['?foo%5Bfoo%5D=property', 'foo', new StringableObjectWithPublicProperty()],
144+
'object implementing __toString() in nested array' => ['?foo%5Bbaz%5D=bar', 'foo', ['baz' => new StringableObject()]],
145+
'object implementing __toString() in nested array but has public property' => ['?foo%5Bbaz%5D%5Bfoo%5D=property', 'foo', ['baz' => new StringableObjectWithPublicProperty()]],
146+
'stdClass' => ['?foo%5Bbaz%5D=bar', 'foo', $stdClass],
147+
'stdClass in nested stdClass' => ['?foo%5Bnested%5D%5Bbaz%5D=bar', 'foo', $nestedStdClass],
148+
'non stringable object' => ['', 'foo', new NonStringableObject()],
149+
'non stringable object but has public property' => ['?foo%5Bfoo%5D=property', 'foo', new NonStringableObjectWithPublicProperty()],
150+
];
129151
}
130152

131153
public function testUrlWithExtraParametersFromGlobals()
@@ -898,3 +920,30 @@ protected function getRoutes($name, Route $route)
898920
return $routes;
899921
}
900922
}
923+
924+
class StringableObject
925+
{
926+
public function __toString()
927+
{
928+
return 'bar';
929+
}
930+
}
931+
932+
class StringableObjectWithPublicProperty
933+
{
934+
public $foo = 'property';
935+
936+
public function __toString()
937+
{
938+
return 'bar';
939+
}
940+
}
941+
942+
class NonStringableObject
943+
{
944+
}
945+
946+
class NonStringableObjectWithPublicProperty
947+
{
948+
public $foo = 'property';
949+
}

0 commit comments

Comments
 (0)