Skip to content
Permalink
Browse files

Implement PSR7 flavour for method & query data.

Implement the PSR7 methods for interacting with the HTTP method and
QueryString data. We'll keep query() around as it is a simpler to use
method.
  • Loading branch information...
markstory committed Sep 3, 2016
1 parent 4d9f2f6 commit 149c9bae3c50e829173f480232584e098cc67794
Showing with 115 additions and 1 deletion.
  1. +48 −1 src/Network/Request.php
  2. +67 −0 tests/TestCase/Network/RequestTest.php
@@ -959,6 +959,17 @@ public function header($name)
return $this->env($name);
}
/**
* Get the HTTP method used for this request.
*
* @return string The name of the HTTP method used.
* @deprected 3.4.0 This method will be removed in 4.0.0. Use getMethod() instead.
*/
public function method()
{
return $this->env('REQUEST_METHOD');
}
/**
* Get the HTTP method used for this request.
* There are a few ways to specify a method.
@@ -972,11 +983,47 @@ public function header($name)
*
* @return string The name of the HTTP method used.
*/
public function method()
public function getMethod()
{
return $this->env('REQUEST_METHOD');
}
/**
* Update the request method and get a new instance.
*
* @param string $method The HTTP method to use.
* @return static A new instance with the updated method.
*/
public function withMethod($method)
{
$new = clone $this;
$new->_environment['REQUEST_METHOD'] = $method;
return $new;
}
/**
* Get all the query parameters.
*
* @return array
*/
public function getQueryParams()
{
return $this->query;
}
/**
* Update the query string data and get a new instance.
*
* @param array $query The query string data to use
* @return static A new instance with the updated query string data.
*/
public function withQueryParams(array $query)
{
$new = clone $this;
$new->query = $query;
return $new;
}
/**
* Get the host that the request was handled on.
*
@@ -728,6 +728,7 @@ public function testIsAll()
* Test the method() method.
*
* @return void
* @deprecated
*/
public function testMethod()
{
@@ -736,6 +737,34 @@ public function testMethod()
$this->assertEquals('delete', $request->method());
}
/**
* Test getMethod()
*
* @return void
*/
public function testGetMethod()
{
$request = new Request([
'environment' => ['REQUEST_METHOD' => 'delete']
]);
$this->assertEquals('delete', $request->getMethod());
}
/**
* Test withMethod()
*
* @return void
*/
public function testWithMethod()
{
$request = new Request([
'environment' => ['REQUEST_METHOD' => 'delete']
]);
$new = $request->withMethod('put');
$this->assertEquals('delete', $request->getMethod());
$this->assertEquals('put', $new->getMethod());
}
/**
* Test host retrieval.
*
@@ -2041,6 +2070,44 @@ public function testQueryWithArray()
$this->assertNull($result);
}
/**
* Test getQueryParams
*
* @return void
*/
public function testGetQueryParams()
{
$get = [
'test' => ['foo', 'bar'],
'key' => 'value'
];
$request = new Request([
'query' => $get
]);
$this->assertSame($get, $request->getQueryParams());
}
/**
* Test withQueryParams and immutability
*
* @return void
*/
public function testWithQueryParams()
{
$get = [
'test' => ['foo', 'bar'],
'key' => 'value'
];
$request = new Request([
'query' => $get
]);
$new = $request->withQueryParams(['new' => 'data']);
$this->assertSame($get, $request->getQueryParams());
$this->assertSame(['new' => 'data'], $new->getQueryParams());
}
/**
* Test using param()
*

0 comments on commit 149c9ba

Please sign in to comment.
You can’t perform that action at this time.