Skip to content

Commit

Permalink
Implement PSR7 flavour for method & query data.
Browse files Browse the repository at this point in the history
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 149c9ba
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/Network/Request.php
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -728,6 +728,7 @@ public function testIsAll()
* Test the method() method.
*
* @return void
* @deprecated
*/
public function testMethod()
{
Expand All @@ -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.
*
Expand Down Expand Up @@ -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()
*
Expand Down

0 comments on commit 149c9ba

Please sign in to comment.