Browse Source

Implement withUri() and tests for getUri() as well.

Mark Story 9 years ago
parent
commit
bc19b3a2dc
2 changed files with 51 additions and 0 deletions
  1. 17 0
      src/Network/Request.php
  2. 34 0
      tests/TestCase/Network/RequestTest.php

+ 17 - 0
src/Network/Request.php

@@ -1886,6 +1886,23 @@ class Request implements ArrayAccess
     }
 
     /**
+     * Return an instance with the specified uri
+     *
+     * *Warning* Replacing the Uri will not update the `base`, `webroot`,
+     * and `url` attributes.
+     *
+     * @param \Psr\Http\Message\UriInterface $uri The new request uri
+     * @return static
+     */
+    public function withUri(UriInterface $uri)
+    {
+        $new = clone $this;
+        $new->uri = $uri;
+
+        return $new;
+    }
+
+    /**
      * Array access read implementation
      *
      * @param string $name Name of the key being accessed.

+ 34 - 0
tests/TestCase/Network/RequestTest.php

@@ -2698,6 +2698,40 @@ XML;
     }
 
     /**
+     * Test getUri
+     *
+     * @return void
+     */
+    public function testGetUri()
+    {
+        $request = new Request(['url' => 'articles/view/3']);
+        $this->assertEquals('articles/view/3', $request->url);
+
+        $result = $request->getUri();
+        $this->assertInstanceOf('Psr\Http\Message\UriInterface', $result);
+        $this->assertEquals('/articles/view/3', $result->getPath());
+    }
+
+    /**
+     * Test withUri
+     *
+     * @return void
+     */
+    public function testWithUri()
+    {
+        $request = new Request([
+            'url' => 'articles/view/3'
+        ]);
+        $uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
+        $new = $request->withUri($uri);
+        $this->assertNotSame($new, $request);
+        $this->assertNotSame($uri, $request->getUri());
+        $this->assertSame($uri, $new->getUri());
+        $this->assertSame('articles/view/3', $new->url);
+        $this->assertSame('articles/view/3', $request->url);
+    }
+
+    /**
      * Test is('requested') and isRequested()
      *
      * @return void