Browse Source

Merge pull request #14284 from cakephp/4.1-server-request

Deprecate ServerRequest::input().
ADmad 6 years ago
parent
commit
a90ba12146

+ 8 - 0
src/Http/ServerRequest.php

@@ -1250,6 +1250,9 @@ class ServerRequest implements ServerRequestInterface
      *
      * Any additional parameters are applied to the callback in the order they are given.
      *
+     * @deprecated 4.1.0 Use `(string)$request->getBody()` to get the raw PHP input
+     *  as string; use `BodyParserMiddleware` to parse the request body so that it's
+     *  available as array/object through `$request->getParsedBody()`.
      * @param callable|null $callback A decoding callback that will convert the string data to another
      *     representation. Leave empty to access the raw input data. You can also
      *     supply additional parameters for the decoding callback using var args, see above.
@@ -1258,6 +1261,11 @@ class ServerRequest implements ServerRequestInterface
      */
     public function input(?callable $callback = null, ...$args)
     {
+        deprecationWarning(
+            'Use `(string)$request->getBody()` to get the raw PHP input as string; '
+            . 'use `BodyParserMiddleware` to parse the request body so that it\'s available as array/object '
+            . 'through $request->getParsedBody()'
+        );
         $this->stream->rewind();
         $input = $this->stream->getContents();
         if ($callback) {

+ 0 - 11
tests/TestCase/Http/ServerRequestFactoryTest.php

@@ -122,17 +122,6 @@ class ServerRequestFactoryTest extends TestCase
     }
 
     /**
-     * Test fromGlobals input
-     *
-     * @return void
-     */
-    public function testFromGlobalsInput()
-    {
-        $res = ServerRequestFactory::fromGlobals();
-        $this->assertSame('', $res->input());
-    }
-
-    /**
      * Test fromGlobals includes the session
      *
      * @preserveGlobalState disabled

+ 25 - 12
tests/TestCase/Http/ServerRequestTest.php

@@ -247,6 +247,7 @@ class ServerRequestTest extends TestCase
      * Test parsing json PUT data into the object.
      *
      * @return void
+     * @group deprecated
      */
     public function testPutParsingJSON()
     {
@@ -259,8 +260,11 @@ class ServerRequestTest extends TestCase
             ],
         ]);
         $this->assertEquals([], $request->getData());
-        $result = $request->input('json_decode', true);
-        $this->assertEquals(['title'], $result['Article']);
+
+        $this->deprecated(function () use ($request) {
+            $result = $request->input('json_decode', true);
+            $this->assertEquals(['title'], $result['Article']);
+        });
     }
 
     /**
@@ -2353,14 +2357,18 @@ class ServerRequestTest extends TestCase
         $request = new ServerRequest([
             'input' => 'I came from stdin',
         ]);
-        $result = $request->input();
-        $this->assertSame('I came from stdin', $result);
+
+        $this->deprecated(function () use ($request) {
+            $result = $request->input();
+            $this->assertSame('I came from stdin', $result);
+        });
     }
 
     /**
      * Test input() decoding.
      *
      * @return void
+     * @group deprecated
      */
     public function testInputDecode()
     {
@@ -2368,14 +2376,17 @@ class ServerRequestTest extends TestCase
             'input' => '{"name":"value"}',
         ]);
 
-        $result = $request->input('json_decode');
-        $this->assertEquals(['name' => 'value'], (array)$result);
+        $this->deprecated(function () use ($request) {
+            $result = $request->input('json_decode');
+            $this->assertEquals(['name' => 'value'], (array)$result);
+        });
     }
 
     /**
      * Test input() decoding with additional arguments.
      *
      * @return void
+     * @group deprecated
      */
     public function testInputDecodeExtraParams()
     {
@@ -2390,12 +2401,14 @@ XML;
             'input' => $xml,
         ]);
 
-        $result = $request->input('Cake\Utility\Xml::build', ['return' => 'domdocument']);
-        $this->assertInstanceOf('DOMDocument', $result);
-        $this->assertEquals(
-            'Test',
-            $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
-        );
+        $this->deprecated(function () use ($request) {
+            $result = $request->input('Cake\Utility\Xml::build', ['return' => 'domdocument']);
+            $this->assertInstanceOf('DOMDocument', $result);
+            $this->assertEquals(
+                'Test',
+                $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
+            );
+        });
     }
 
     /**

+ 1 - 1
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -165,7 +165,7 @@ class RequestActionController extends AppController
      */
     public function input_test()
     {
-        $text = $this->request->input('json_decode')->hello;
+        $text = json_decode((string)$this->request->getBody())->hello;
 
         return $this->response->withStringBody($text);
     }