diff --git a/src/Http/ServerRequestFactory.php b/src/Http/ServerRequestFactory.php index 7393090dc8b..b7c7d942006 100644 --- a/src/Http/ServerRequestFactory.php +++ b/src/Http/ServerRequestFactory.php @@ -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, diff --git a/tests/TestCase/Http/ServerRequestFactoryTest.php b/tests/TestCase/Http/ServerRequestFactoryTest.php index 5c20159d52a..d765088cef6 100644 --- a/tests/TestCase/Http/ServerRequestFactoryTest.php +++ b/tests/TestCase/Http/ServerRequestFactoryTest.php @@ -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 * @@ -32,6 +57,10 @@ public function setUp() { parent::setUp(); $this->server = $_SERVER; + $this->post = $_POST; + $this->files = $_FILES; + $this->cookies = $_COOKIE; + $this->get = $_GET; } /** @@ -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()); } /**