Skip to content
Permalink
Browse files

Implement parsed body methods.

Add more of the PSR7 interface in.
  • Loading branch information...
markstory committed Sep 4, 2016
1 parent 7aafb67 commit f79582299a20ddb4504a43b9e2d6fb2ea1b54d76
Showing with 61 additions and 0 deletions.
  1. +30 −0 src/Network/Request.php
  2. +31 −0 tests/TestCase/Network/RequestTest.php
@@ -1403,6 +1403,36 @@ public function withCookieParams(array $cookies)
return $new;
}
/**
* Get the parsed request body data.
*
* If the request Content-Type is either application/x-www-form-urlencoded
* or multipart/form-data, nd the request method is POST, this will be the
* post data. For other content types, it may be the deserialized request
* body.
*
* @return null|array|object The deserialized body parameters, if any.
* These will typically be an array or object.
*/
public function getParsedBody()
{
return $this->data;
}
/**
* Update the parsed body and get a new instance.
*
* @param null|array|object $data The deserialized body data. This will
* typically be in an array or object.
* @return static
*/
public function withParsedBody($data)
{
$new = clone $this;
$new->data = $data;
return $new;
}
/**
* Get/Set value from the request's environment data.
* Fallback to using env() if key not set in $environment property.
@@ -2704,6 +2704,37 @@ public function testWithParam()
$this->assertSame('Admin', $result->param('prefix'));
}
/**
* Test getting the parsed body parameters.
*
* @return void
*/
public function testGetParsedBody()
{
$data = ['title' => 'First', 'body' => 'Best Article!'];
$request = new Request(['post' => $data]);
$this->assertSame($data, $request->getParsedBody());
$request = new Request();
$this->assertSame([], $request->getParsedBody());
}
/**
* Test replacing the parsed body parameters.
*
* @return void
*/
public function testWithParsedBody()
{
$data = ['title' => 'First', 'body' => 'Best Article!'];
$request = new Request([]);
$new = $request->withParsedBody($data);
$this->assertNotSame($request, $new);
$this->assertSame([], $request->getParsedBody());
$this->assertSame($data, $new->getParsedBody());
}
/**
* Test updating POST data in a psr7 fashion.
*

0 comments on commit f795822

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