Browse Source

Merge pull request #12744 from cakephp/3.7-client-response

3.7 client response
Mark Story 7 years ago
parent
commit
ee8eb1c375

+ 80 - 20
src/Http/Client/Response.php

@@ -44,14 +44,6 @@ use Zend\Diactoros\Stream;
  * $response->getHeaders();
  * ```
  *
- * You can also get at the headers using object access. When getting
- * headers with object access, you have to use case-sensitive header
- * names:
- *
- * ```
- * $val = $response->headers['Content-Type'];
- * ```
- *
  * ### Get the response body
  *
  * You can access the response body stream using:
@@ -60,11 +52,10 @@ use Zend\Diactoros\Stream;
  * $content = $response->getBody();
  * ```
  *
- * You can also use object access to get the string version
- * of the response body:
+ * You can get the body string using:
  *
  * ```
- * $content = $response->body;
+ * $content = $response->getStringBody();
  * ```
  *
  * If your response body is in XML or JSON you can use
@@ -74,9 +65,9 @@ use Zend\Diactoros\Stream;
  *
  * ```
  * // Get as xml
- * $content = $response->xml
+ * $content = $response->getXml()
  * // Get as json
- * $content = $response->json
+ * $content = $response->getJson()
  * ```
  *
  * If the response cannot be decoded, null will be returned.
@@ -88,12 +79,6 @@ use Zend\Diactoros\Stream;
  * ```
  * $content = $response->getStatusCode();
  * ```
- *
- * You can also use object access:
- *
- * ```
- * $content = $response->code;
- * ```
  */
 class Response extends Message implements ResponseInterface
 {
@@ -149,6 +134,20 @@ class Response extends Message implements ResponseInterface
     ];
 
     /**
+     * Map of deprecated magic properties.
+     *
+     * @var array
+     * @internal
+     */
+    protected $_deprecatedMagicProperties = [
+        'cookies' => 'getCookies()',
+        'body' => 'getStringBody()',
+        'json' => 'getJson()',
+        'xml' => 'getXml()',
+        'headers' => 'getHeaders()',
+    ];
+
+    /**
      * Constructor
      *
      * @param array $headers Unparsed headers.
@@ -557,9 +556,14 @@ class Response extends Message implements ResponseInterface
      * @param callable|null $parser The callback to use to decode
      *   the response body.
      * @return mixed The response body.
+     * @deprecated 3.7.0 Use getStringBody()/getJson()/getXml() instead.
      */
     public function body($parser = null)
     {
+        deprecationWarning(
+            'Response::body() is deprecated. Use getStringBody()/getJson()/getXml() instead.'
+        );
+
         $stream = $this->stream;
         $stream->rewind();
         if ($parser) {
@@ -570,6 +574,26 @@ class Response extends Message implements ResponseInterface
     }
 
     /**
+     * Get the response body as string.
+     *
+     * @return string
+     */
+    public function getStringBody()
+    {
+        return $this->_getBody();
+    }
+
+    /**
+     * Get the response body as JSON decoded data.
+     *
+     * @return array|null
+     */
+    public function getJson()
+    {
+        return $this->_getJson();
+    }
+
+    /**
      * Get the response body as JSON decoded data.
      *
      * @return array|null
@@ -588,6 +612,16 @@ class Response extends Message implements ResponseInterface
      *
      * @return null|\SimpleXMLElement
      */
+    public function getXml()
+    {
+        return $this->_getXml();
+    }
+
+    /**
+     * Get the response body as XML decoded data.
+     *
+     * @return null|\SimpleXMLElement
+     */
     protected function _getXml()
     {
         if ($this->_xml) {
@@ -622,7 +656,7 @@ class Response extends Message implements ResponseInterface
     /**
      * Provides magic __get() support.
      *
-     * @return array
+     * @return string
      */
     protected function _getBody()
     {
@@ -644,9 +678,22 @@ class Response extends Message implements ResponseInterface
         }
         $key = $this->_exposedProperties[$name];
         if (substr($key, 0, 4) === '_get') {
+            deprecationWarning(sprintf(
+                'Response::%s is deprecated. Use Response::%s instead.',
+                $name,
+                $this->_deprecatedMagicProperties[$name]
+            ));
+
             return $this->{$key}();
         }
 
+        if ($key === 'code') {
+            deprecationWarning(
+                'Response::code() is deprecated. ' .
+                'Use Response::getStatusCode() instead.'
+            );
+        }
+
         return $this->{$key};
     }
 
@@ -663,11 +710,24 @@ class Response extends Message implements ResponseInterface
         }
         $key = $this->_exposedProperties[$name];
         if (substr($key, 0, 4) === '_get') {
+            deprecationWarning(sprintf(
+                'Response::%s is deprecated. Use Response::%s instead.',
+                $name,
+                $this->_deprecatedMagicProperties[$name]
+            ));
+
             $val = $this->{$key}();
 
             return $val !== null;
         }
 
+        if ($key === 'code') {
+            deprecationWarning(
+                'Response::code() is deprecated. ' .
+                'Use Response::getStatusCode() instead.'
+            );
+        }
+
         return isset($this->{$key});
     }
 }

+ 1 - 1
tests/TestCase/Http/Client/Adapter/StreamTest.php

@@ -152,7 +152,7 @@ class StreamTest extends TestCase
         $responses = $stream->send($request, []);
         $this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
 
-        $this->assertEquals(20000, strlen($responses[0]->body()));
+        $this->assertEquals(20000, strlen($responses[0]->getStringBody()));
     }
 
     /**

+ 41 - 27
tests/TestCase/Http/Client/ResponseTest.php

@@ -77,9 +77,8 @@ class ResponseTest extends TestCase
 
         $this->assertEquals(
             'text/html;charset="UTF-8"',
-            $response->headers['Content-Type']
+            $response->getHeaderLine('Content-Type')
         );
-        $this->assertTrue(isset($response->headers));
 
         $headers = [
             'HTTP/1.0 200',
@@ -97,20 +96,35 @@ class ResponseTest extends TestCase
      */
     public function testBody()
     {
-        $data = [
-            'property' => 'value'
-        ];
-        $encoded = json_encode($data);
+        $this->deprecated(function () {
+            $data = [
+                'property' => 'value'
+            ];
+            $encoded = json_encode($data);
 
-        $response = new Response([], $encoded);
+            $response = new Response([], $encoded);
 
-        $this->assertEquals($encoded, $response->getBody()->getContents());
-        $this->assertEquals($encoded, $response->body());
+            $this->assertEquals($encoded, $response->getBody()->getContents());
+            $this->assertEquals($encoded, $response->body());
 
-        $result = $response->body('json_decode');
-        $this->assertEquals($data['property'], $result->property);
-        $this->assertEquals($encoded, $response->body);
-        $this->assertTrue(isset($response->body));
+            $result = $response->body('json_decode');
+            $this->assertEquals($data['property'], $result->property);
+            $stream = $response->getBody();
+            $stream->rewind();
+            $this->assertEquals($encoded, $stream->getContents());
+        });
+    }
+
+    /**
+     * Test getStringBody()
+     *
+     * @return void
+     */
+    public function getStringBody()
+    {
+        $response = new Response([], 'string');
+
+        $this->assertEquals('string', $response->getStringBody());
     }
 
     /**
@@ -125,28 +139,27 @@ class ResponseTest extends TestCase
         ];
         $encoded = json_encode($data);
         $response = new Response([], $encoded);
-        $this->assertTrue(isset($response->json));
-        $this->assertEquals($data['property'], $response->json['property']);
+        $this->assertEquals($data['property'], $response->getJson()['property']);
 
         $data = '';
         $response = new Response([], $data);
-        $this->assertNull($response->json);
+        $this->assertNull($response->getJson());
 
         $data = json_encode([]);
         $response = new Response([], $data);
-        $this->assertInternalType('array', $response->json);
+        $this->assertInternalType('array', $response->getJson());
 
         $data = json_encode(null);
         $response = new Response([], $data);
-        $this->assertNull($response->json);
+        $this->assertNull($response->getJson());
 
         $data = json_encode(false);
         $response = new Response([], $data);
-        $this->assertFalse($response->json);
+        $this->assertFalse($response->getJson());
 
         $data = json_encode('');
         $response = new Response([], $data);
-        $this->assertSame('', $response->json);
+        $this->assertSame('', $response->getJson());
     }
 
     /**
@@ -162,7 +175,7 @@ class ResponseTest extends TestCase
         $encoded = json_encode($data);
         $response = new Response([], '');
         $response->getBody()->write($encoded);
-        $this->assertEquals($data, $response->json);
+        $this->assertEquals($data, $response->getJson());
     }
 
     /**
@@ -179,12 +192,11 @@ class ResponseTest extends TestCase
 </root>
 XML;
         $response = new Response([], $data);
-        $this->assertTrue(isset($response->xml));
-        $this->assertEquals('Test', (string)$response->xml->test);
+        $this->assertEquals('Test', (string)$response->getXml()->test);
 
         $data = '';
         $response = new Response([], $data);
-        $this->assertFalse(isset($response->xml));
+        $this->assertNull($response->getXml());
     }
 
     /**
@@ -387,8 +399,10 @@ XML;
         $response = new Response($headers, '');
         $this->assertSame(404, $response->getStatusCode());
 
-        $this->assertSame(404, $response->code);
-        $this->assertTrue(isset($response->code));
+        $this->deprecated(function () use ($response) {
+            $this->assertSame(404, $response->code);
+            $this->assertTrue(isset($response->code));
+        });
     }
 
     /**
@@ -499,6 +513,6 @@ XML;
         ];
         $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
         $response = new Response($headers, $body);
-        $this->assertEquals('Hello world!', $response->body);
+        $this->assertEquals('Hello world!', $response->getBody()->getContents());
     }
 }