Skip to content

Commit

Permalink
Merge e0a67d4 into ff82332
Browse files Browse the repository at this point in the history
  • Loading branch information
famoser committed Dec 14, 2016
2 parents ff82332 + e0a67d4 commit 9bdb7f7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions Slim/Http/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function mock(array $userData = [])
'REMOTE_ADDR' => '127.0.0.1',
'REQUEST_TIME' => time(),
'REQUEST_TIME_FLOAT' => microtime(true),
'MOCK_POST_DATA' => null
], $userData);

return new static($data);
Expand Down
5 changes: 3 additions & 2 deletions Slim/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ public static function createFromEnvironment(Environment $environment)
$headers = Headers::createFromEnvironment($environment);
$cookies = Cookies::parseHeader($headers->get('Cookie', []));
$serverParams = $environment->all();
$body = new RequestBody();
$body = new RequestBody($environment);
$uploadedFiles = UploadedFile::createFromEnvironment($environment);

$request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);

if ($method === 'POST' &&
in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])
in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data']) &&
!$environment["MOCK_POST_DATA"]
) {
// parsed body must be $_POST
$request = $request->withParsedBody($_POST);
Expand Down
10 changes: 8 additions & 2 deletions Slim/Http/RequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ class RequestBody extends Body
{
/**
* Create a new RequestBody.
*
* @param Environment $environment The Slim application Environment
*/
public function __construct()
public function __construct(Environment $environment = null)
{
$stream = fopen('php://temp', 'w+');
stream_copy_to_stream(fopen('php://input', 'r'), $stream);
if ($environment != null && $environment["MOCK_POST_DATA"] != null) {
fwrite($stream, $environment["MOCK_POST_DATA"]);
} else {
stream_copy_to_stream(fopen('php://input', 'r'), $stream);
}
rewind($stream);

parent::__construct($stream);
Expand Down
14 changes: 14 additions & 0 deletions tests/Http/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Slim\Tests\Http;

use Slim\Http\Environment;
use Slim\Http\Request;

class EnvironmentTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -55,4 +56,17 @@ public function testMock()
$this->assertEquals('/foo/bar?abc=123', $env->get('REQUEST_URI'));
$this->assertEquals('localhost', $env->get('HTTP_HOST'));
}

/**
* Test mock post data
*/
public function testMockPostData()
{
$env = Environment::mock([
'MOCK_POST_DATA' => 'data'
]);

$request = Request::createFromEnvironment($env);
$this->assertEquals($request->getBody(), "data");
}
}

0 comments on commit 9bdb7f7

Please sign in to comment.