Skip to content

Commit bc19b3a

Browse files
committed
Implement withUri() and tests for getUri() as well.
1 parent dd584fe commit bc19b3a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Network/Request.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,23 @@ public function getUri()
18851885
return $this->uri;
18861886
}
18871887

1888+
/**
1889+
* Return an instance with the specified uri
1890+
*
1891+
* *Warning* Replacing the Uri will not update the `base`, `webroot`,
1892+
* and `url` attributes.
1893+
*
1894+
* @param \Psr\Http\Message\UriInterface $uri The new request uri
1895+
* @return static
1896+
*/
1897+
public function withUri(UriInterface $uri)
1898+
{
1899+
$new = clone $this;
1900+
$new->uri = $uri;
1901+
1902+
return $new;
1903+
}
1904+
18881905
/**
18891906
* Array access read implementation
18901907
*

tests/TestCase/Network/RequestTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,40 @@ public function testWithBody()
26972697
$this->assertSame($body, $new->getBody());
26982698
}
26992699

2700+
/**
2701+
* Test getUri
2702+
*
2703+
* @return void
2704+
*/
2705+
public function testGetUri()
2706+
{
2707+
$request = new Request(['url' => 'articles/view/3']);
2708+
$this->assertEquals('articles/view/3', $request->url);
2709+
2710+
$result = $request->getUri();
2711+
$this->assertInstanceOf('Psr\Http\Message\UriInterface', $result);
2712+
$this->assertEquals('/articles/view/3', $result->getPath());
2713+
}
2714+
2715+
/**
2716+
* Test withUri
2717+
*
2718+
* @return void
2719+
*/
2720+
public function testWithUri()
2721+
{
2722+
$request = new Request([
2723+
'url' => 'articles/view/3'
2724+
]);
2725+
$uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
2726+
$new = $request->withUri($uri);
2727+
$this->assertNotSame($new, $request);
2728+
$this->assertNotSame($uri, $request->getUri());
2729+
$this->assertSame($uri, $new->getUri());
2730+
$this->assertSame('articles/view/3', $new->url);
2731+
$this->assertSame('articles/view/3', $request->url);
2732+
}
2733+
27002734
/**
27012735
* Test is('requested') and isRequested()
27022736
*

0 commit comments

Comments
 (0)