Skip to content
Permalink
Browse files

Add cookie and environment methods for PSR7 interface.

Add the PSR7 methods that read cookies and environment data to Request.
  • Loading branch information...
markstory committed Sep 4, 2016
1 parent 83dbcd8 commit 7aafb67a99d979fb5755e2222672b1d84b159ed5
Showing with 93 additions and 1 deletion.
  1. +36 −0 src/Network/Request.php
  2. +57 −1 tests/TestCase/Network/RequestTest.php
@@ -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.
*
@@ -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.
@@ -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());
}
@@ -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()
*
@@ -2511,7 +2536,7 @@ public function testIsRequested()
*
* @return void
*/
public function testReadCookie()
public function testCookie()
{
$request = new Request([
'cookies' => [
@@ -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
*

0 comments on commit 7aafb67

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