Skip to content

Commit

Permalink
Fallback to $_FILES instead of nothing.
Browse files Browse the repository at this point in the history
This was missed in the original implementation. I added a test for all
the other superglobals as well. The tests were clearly a bit too sparse
around this.

Refs #10079
  • Loading branch information
markstory committed Jan 22, 2017
1 parent ffcd5f5 commit b0da8f2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Http/ServerRequestFactory.php
Expand Up @@ -49,7 +49,7 @@ public static function fromGlobals(
$request = new ServerRequest([
'environment' => $server,
'uri' => $uri,
'files' => $files,
'files' => $files ?: $_FILES,
'cookies' => $cookies ?: $_COOKIE,
'query' => $query ?: $_GET,
'post' => $body ?: $_POST,
Expand Down
63 changes: 63 additions & 0 deletions tests/TestCase/Http/ServerRequestFactoryTest.php
Expand Up @@ -23,6 +23,31 @@
*/
class ServerRequestFactoryTest extends TestCase
{
/**
* @var array|null
*/
protected $server = null;

/**
* @var array|null
*/
protected $post = null;

/**
* @var array|null
*/
protected $files = null;

/**
* @var array|null
*/
protected $cookies = null;

/**
* @var array|null
*/
protected $get = null;

/**
* setup
*
Expand All @@ -32,6 +57,10 @@ public function setUp()
{
parent::setUp();
$this->server = $_SERVER;
$this->post = $_POST;
$this->files = $_FILES;
$this->cookies = $_COOKIE;
$this->get = $_GET;
}

/**
Expand All @@ -43,6 +72,40 @@ public function tearDown()
{
parent::tearDown();
$_SERVER = $this->server;
$_POST = $this->post;
$_FILES = $this->files;
$_COOKIE = $this->cookies;
$_GET = $this->get;
}

/**
* Test fromGlobals reads super globals
*
* @return void
*/
public function testFromGlobalsSuperGlobals()
{
$_POST = [
'title' => 'custom'
];
$_FILES = [
'image' => [
'tmp_name' => __FILE__,
'error' => 0,
'name' => 'cats.png',
'type' => 'image/png',
'size' => 2112
]
];
$_COOKIE = ['key' => 'value'];
$_GET = ['query' => 'string'];
$res = ServerRequestFactory::fromGlobals();
$this->assertSame($_COOKIE['key'], $res->getCookie('key'));
$this->assertSame($_GET['query'], $res->getQuery('query'));
$this->assertArrayHasKey('title', $res->getData());
$this->assertArrayHasKey('image', $res->getData());
$this->assertSame($_FILES['image'], $res->getData('image'));
$this->assertCount(1, $res->getUploadedFiles());
}

/**
Expand Down

0 comments on commit b0da8f2

Please sign in to comment.