Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit ee8dfc4

Browse files
committed
Merge branch 'hotfix/80'
Close #85
2 parents 20cc20f + d0f90f0 commit ee8dfc4

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ All notable changes to this project will be documented in this file, in reverse
2424
- [#73](https://github.com/zendframework/zend-diactoros/pull/73) changes the
2525
behavior in `Request` such that if it marshals a stream during instantiation,
2626
the stream is marked as writeable (specifically, mode `wb+`).
27+
- [#85](https://github.com/zendframework/zend-diactoros/pull/85) updates the
28+
behavior of `Zend\Diactoros\Uri`'s various `with*()` methods that are
29+
documented as accepting strings to raise exceptions on non-string input.
30+
Previously, several simply passed non-string input on verbatim, others
31+
normalized the input, and a few correctly raised the exceptions. Behavior is
32+
now consistent across each.
2733

2834
## 1.1.2 - 2015-07-12
2935

src/Uri.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ public function getFragment()
220220
*/
221221
public function withScheme($scheme)
222222
{
223+
if (! is_string($scheme)) {
224+
throw new InvalidArgumentException(sprintf(
225+
'%s expects a string argument; received %s',
226+
__METHOD__,
227+
(is_object($scheme) ? get_class($scheme) : gettype($scheme))
228+
));
229+
}
230+
223231
$scheme = $this->filterScheme($scheme);
224232

225233
if ($scheme === $this->scheme) {
@@ -238,6 +246,21 @@ public function withScheme($scheme)
238246
*/
239247
public function withUserInfo($user, $password = null)
240248
{
249+
if (! is_string($user)) {
250+
throw new InvalidArgumentException(sprintf(
251+
'%s expects a string user argument; received %s',
252+
__METHOD__,
253+
(is_object($user) ? get_class($user) : gettype($user))
254+
));
255+
}
256+
if (null !== $password && ! is_string($password)) {
257+
throw new InvalidArgumentException(sprintf(
258+
'%s expects a string password argument; received %s',
259+
__METHOD__,
260+
(is_object($password) ? get_class($password) : gettype($password))
261+
));
262+
}
263+
241264
$info = $user;
242265
if ($password) {
243266
$info .= ':' . $password;
@@ -259,6 +282,14 @@ public function withUserInfo($user, $password = null)
259282
*/
260283
public function withHost($host)
261284
{
285+
if (! is_string($host)) {
286+
throw new InvalidArgumentException(sprintf(
287+
'%s expects a string argument; received %s',
288+
__METHOD__,
289+
(is_object($host) ? get_class($host) : gettype($host))
290+
));
291+
}
292+
262293
if ($host === $this->host) {
263294
// Do nothing if no change was made.
264295
return clone $this;
@@ -373,6 +404,14 @@ public function withQuery($query)
373404
*/
374405
public function withFragment($fragment)
375406
{
407+
if (! is_string($fragment)) {
408+
throw new InvalidArgumentException(sprintf(
409+
'%s expects a string argument; received %s',
410+
__METHOD__,
411+
(is_object($fragment) ? get_class($fragment) : gettype($fragment))
412+
));
413+
}
414+
376415
$fragment = $this->filterFragment($fragment);
377416

378417
if ($fragment === $this->fragment) {
@@ -585,10 +624,6 @@ private function splitQueryValue($value)
585624
*/
586625
private function filterFragment($fragment)
587626
{
588-
if (null === $fragment) {
589-
$fragment = '';
590-
}
591-
592627
if (! empty($fragment) && strpos($fragment, '#') === 0) {
593628
$fragment = substr($fragment, 1);
594629
}

test/UriTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,49 @@ public function testProperlyTrimsLeadingSlashesToPreventXSS()
455455
$uri = new Uri($url);
456456
$this->assertEquals('http://example.org/zend.com', (string) $uri);
457457
}
458+
459+
public function invalidStringComponentValues()
460+
{
461+
$methods = [
462+
'withScheme',
463+
'withUserInfo',
464+
'withHost',
465+
'withPath',
466+
'withQuery',
467+
'withFragment',
468+
];
469+
470+
$values = [
471+
'null' => null,
472+
'true' => true,
473+
'false' => false,
474+
'zero' => 0,
475+
'int' => 1,
476+
'zero-float' => 0.0,
477+
'float' => 1.1,
478+
'array' => ['value'],
479+
'object' => (object)['value' => 'value'],
480+
];
481+
482+
$combinations = [];
483+
foreach ($methods as $method) {
484+
foreach ($values as $type => $value) {
485+
$key = sprintf('%s-%s', $method, $type);
486+
$combinations[$key] = [$method, $value];
487+
}
488+
}
489+
490+
return $combinations;
491+
}
492+
493+
/**
494+
* @group 80
495+
* @dataProvider invalidStringComponentValues
496+
*/
497+
public function testPassingInvalidValueToWithMethodRaisesException($method, $value)
498+
{
499+
$uri = new Uri('https://example.com/');
500+
$this->setExpectedException('InvalidArgumentException');
501+
$uri->$method($value);
502+
}
458503
}

0 commit comments

Comments
 (0)