Skip to content

Commit

Permalink
Add cookie and environment methods for PSR7 interface.
Browse files Browse the repository at this point in the history
Add the PSR7 methods that read cookies and environment data to Request.
  • Loading branch information
markstory committed Sep 4, 2016
1 parent 83dbcd8 commit 7aafb67
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/Network/Request.php
Expand Up @@ -1010,6 +1010,19 @@ public function withMethod($method)
return $new;
}

/**
* Get all the server environment parameters.
*
* Read all of the 'environment' or 'server' data that was
* used to create this request.
*
* @return array
*/
public function getServerParams()
{
return $this->_environment;
}

/**
* Get all the query parameters.
*
Expand Down Expand Up @@ -1367,6 +1380,29 @@ public function cookie($key)
return null;
}

/**
* Get all the cookie data from the request.
*
* @return array An array of cookie data.
*/
public function getCookieParams()
{
return $this->cookies;
}

/**
* Replace the cookies and get a new request instance.
*
* @param array $cookies The new cookie data to use.
* @return static
*/
public function withCookieParams(array $cookies)
{
$new = clone $this;
$new->cookies = $cookies;
return $new;
}

/**
* Get/Set value from the request's environment data.
* Fallback to using env() if key not set in $environment property.
Expand Down
58 changes: 57 additions & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -761,6 +761,7 @@ public function testWithMethod()
'environment' => ['REQUEST_METHOD' => 'delete']
]);
$new = $request->withMethod('put');
$this->assertNotSame($new, $request);
$this->assertEquals('delete', $request->getMethod());
$this->assertEquals('put', $new->getMethod());
}
Expand Down Expand Up @@ -2123,6 +2124,30 @@ public function testWithQueryParams()
$this->assertSame(['new' => 'data'], $new->getQueryParams());
}

/**
* Test getServerParams
*
* @return void
*/
public function testGetServerParams()
{
$vars = [
'REQUEST_METHOD' => 'PUT',
'HTTPS' => 'on',
];

$request = new Request([
'environment' => $vars
]);
$expected = $vars + [
'CONTENT_TYPE' => null,
'HTTP_CONTENT_TYPE' => null,
'HTTP_X_HTTP_METHOD_OVERRIDE' => null,
'ORIGINAL_REQUEST_METHOD' => 'PUT',
];
$this->assertSame($expected, $request->getServerParams());
}

/**
* Test using param()
*
Expand Down Expand Up @@ -2511,7 +2536,7 @@ public function testIsRequested()
*
* @return void
*/
public function testReadCookie()
public function testCookie()
{
$request = new Request([
'cookies' => [
Expand All @@ -2525,6 +2550,37 @@ public function testReadCookie()
$this->assertNull($result);
}

/**
* Test getCookieParams()
*
* @return void
*/
public function testGetCookieParams()
{
$cookies = [
'testing' => 'A value in the cookie'
];
$request = new Request(['cookies' => $cookies]);
$this->assertSame($cookies, $request->getCookieParams());
}

/**
* Test withCookieParams()
*
* @return void
*/
public function testWithCookieParams()
{
$cookies = [
'testing' => 'A value in the cookie'
];
$request = new Request(['cookies' => $cookies]);
$new = $request->withCookieParams(['remember_me' => 1]);
$this->assertNotSame($new, $request);
$this->assertSame($cookies, $request->getCookieParams());
$this->assertSame(['remember_me' => 1], $new->getCookieParams());
}

/**
* TestAllowMethod
*
Expand Down

0 comments on commit 7aafb67

Please sign in to comment.