Browse Source

Fix deprecation warnings in client request/response

Mark Story 8 years ago
parent
commit
45c032a013
2 changed files with 203 additions and 115 deletions
  1. 105 74
      tests/TestCase/Http/Client/RequestTest.php
  2. 98 41
      tests/TestCase/Http/Client/ResponseTest.php

+ 105 - 74
tests/TestCase/Http/Client/RequestTest.php

@@ -37,7 +37,7 @@ class RequestTest extends TestCase
         $data = ['a' => 'b', 'c' => 'd'];
         $request = new Request('http://example.com', 'POST', $headers, json_encode($data));
 
-        $this->assertEquals('http://example.com', $request->url());
+        $this->assertEquals('http://example.com', (string)$request->getUri());
         $this->assertContains($request->getMethod(), 'POST');
         $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
         $this->assertEquals(json_encode($data), $request->body());
@@ -54,7 +54,7 @@ class RequestTest extends TestCase
         $request = new Request('http://example.com', $method, $headers, json_encode($data));
 
         $this->assertEquals($request->getMethod(), $method);
-        $this->assertEquals('http://example.com', $request->url());
+        $this->assertEquals('http://example.com', (string)$request->getUri());
         $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
         $this->assertEquals(json_encode($data), $request->body());
     }
@@ -91,7 +91,7 @@ class RequestTest extends TestCase
         $data = ['a' => 'b', 'c' => 'd'];
         $request = new Request('http://example.com', 'POST', $headers, $data);
 
-        $this->assertEquals('http://example.com', $request->url());
+        $this->assertEquals('http://example.com', (string)$request->getUri());
         $this->assertEquals('POST', $request->getMethod());
         $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
         $this->assertEquals('a=b&c=d', $request->body());
@@ -111,7 +111,7 @@ class RequestTest extends TestCase
         $data = ['a' => 'b', 'c' => ['foo', 'bar']];
         $request = new Request('http://example.com', 'POST', $headers, $data);
 
-        $this->assertEquals('http://example.com', $request->url());
+        $this->assertEquals('http://example.com', (string)$request->getUri());
         $this->assertEquals('POST', $request->getMethod());
         $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type'));
         $this->assertEquals('a=b&c%5B0%5D=foo&c%5B1%5D=bar', $request->body());
@@ -120,76 +120,91 @@ class RequestTest extends TestCase
     /**
      * test url method
      *
+     * @group deprecated
      * @return void
      */
     public function testUrl()
     {
-        $request = new Request();
-        $this->assertSame($request, $request->url('http://example.com'));
+        $this->deprecated(function () {
+            $request = new Request();
+            $this->assertSame($request, $request->url('http://example.com'));
 
-        $this->assertEquals('http://example.com', $request->url());
+            $this->assertEquals('http://example.com', $request->url());
+        });
     }
 
     /**
      * Test that url() modifies the PSR7 stream
      *
+     * @group deprecated
      * @return void
      */
     public function testUrlInteroperability()
     {
-        $request = new Request();
-        $request->url('http://example.com');
-        $this->assertSame('http://example.com', $request->url());
-        $this->assertSame('http://example.com', $request->getUri()->__toString());
-
-        $uri = 'http://example.com/test';
-        $request = new Request();
-        $request = $request->withUri(new Uri($uri));
-        $this->assertSame($uri, $request->url());
-        $this->assertSame($uri, $request->getUri()->__toString());
+        $this->deprecated(function () {
+            $request = new Request();
+            $request->url('http://example.com');
+            $this->assertSame('http://example.com', $request->url());
+            $this->assertSame('http://example.com', $request->getUri()->__toString());
+
+            $uri = 'http://example.com/test';
+            $request = new Request();
+            $request = $request->withUri(new Uri($uri));
+            $this->assertSame($uri, $request->url());
+            $this->assertSame($uri, $request->getUri()->__toString());
+        });
     }
 
     /**
      * test method method.
      *
+     * @group deprecated
      * @return void
      */
     public function testMethod()
     {
-        $request = new Request();
-        $this->assertSame($request, $request->method(Request::METHOD_GET));
+        $this->deprecated(function () {
+            $request = new Request();
+            $this->assertSame($request, $request->method(Request::METHOD_GET));
 
-        $this->assertEquals(Request::METHOD_GET, $request->method());
+            $this->assertEquals(Request::METHOD_GET, $request->method());
+        });
     }
 
     /**
      * test method interoperability.
      *
+     * @group deprecated
      * @return void
      */
     public function testMethodInteroperability()
     {
-        $request = new Request();
-        $this->assertSame($request, $request->method(Request::METHOD_GET));
+        $this->deprecated(function () {
+            $request = new Request();
+            $this->assertSame($request, $request->method(Request::METHOD_GET));
 
-        $this->assertEquals(Request::METHOD_GET, $request->method());
-        $this->assertEquals(Request::METHOD_GET, $request->getMethod());
+            $this->assertEquals(Request::METHOD_GET, $request->method());
+            $this->assertEquals(Request::METHOD_GET, $request->getMethod());
 
-        $request = $request->withMethod(Request::METHOD_GET);
-        $this->assertEquals(Request::METHOD_GET, $request->method());
-        $this->assertEquals(Request::METHOD_GET, $request->getMethod());
+            $request = $request->withMethod(Request::METHOD_GET);
+            $this->assertEquals(Request::METHOD_GET, $request->method());
+            $this->assertEquals(Request::METHOD_GET, $request->getMethod());
+        });
     }
 
     /**
      * test invalid method.
      *
+     * @group deprecated
      * @return void
      */
     public function testMethodInvalid()
     {
         $this->expectException(\Cake\Core\Exception\Exception::class);
-        $request = new Request();
-        $request->method('set on fire');
+        $this->deprecated(function () {
+            $request = new Request();
+            $request->method('set on fire');
+        });
     }
 
     /**
@@ -209,6 +224,7 @@ class RequestTest extends TestCase
     /**
      * test body method with array payload
      *
+     * @group deprecated
      * @return void
      */
     public function testBodyArray()
@@ -248,30 +264,33 @@ class RequestTest extends TestCase
     /**
      * test header method.
      *
+     * @group deprecated
      * @return void
      */
     public function testHeader()
     {
-        $request = new Request();
-        $type = 'application/json';
-        $result = $request->header('Content-Type', $type);
-        $this->assertSame($result, $request, 'Should return self');
-
-        $result = $request->header('content-type');
-        $this->assertEquals($type, $result, 'lowercase does not work');
-
-        $result = $request->header('ConTent-typE');
-        $this->assertEquals($type, $result, 'Funny casing does not work');
-
-        $result = $request->header([
-            'Connection' => 'close',
-            'user-agent' => 'CakePHP'
-        ]);
-        $this->assertSame($result, $request, 'Should return self');
-
-        $this->assertEquals('close', $request->header('connection'));
-        $this->assertEquals('CakePHP', $request->header('USER-AGENT'));
-        $this->assertNull($request->header('not set'));
+        $this->deprecated(function () {
+            $request = new Request();
+            $type = 'application/json';
+            $result = $request->header('Content-Type', $type);
+            $this->assertSame($result, $request, 'Should return self');
+
+            $result = $request->header('content-type');
+            $this->assertEquals($type, $result, 'lowercase does not work');
+
+            $result = $request->header('ConTent-typE');
+            $this->assertEquals($type, $result, 'Funny casing does not work');
+
+            $result = $request->header([
+                'Connection' => 'close',
+                'user-agent' => 'CakePHP'
+            ]);
+            $this->assertSame($result, $request, 'Should return self');
+
+            $this->assertEquals('close', $request->header('connection'));
+            $this->assertEquals('CakePHP', $request->header('USER-AGENT'));
+            $this->assertNull($request->header('not set'));
+        });
     }
 
     /**
@@ -289,64 +308,76 @@ class RequestTest extends TestCase
     /**
      * Test that header() and PSR7 methods play nice.
      *
+     * @group deprecated
      * @return void
      */
     public function testHeaderMethodInteroperability()
     {
-        $request = new Request();
-        $request->header('Content-Type', 'application/json');
-        $this->assertEquals('application/json', $request->header('Content-Type'), 'Old getter should work');
-
-        $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'), 'getHeaderLine works');
-        $this->assertEquals('application/json', $request->getHeaderLine('content-type'), 'getHeaderLine works');
-        $this->assertEquals(['application/json'], $request->getHeader('Content-Type'), 'getHeader works');
-        $this->assertEquals(['application/json'], $request->getHeader('content-type'), 'getHeader works');
+        $this->deprecated(function () {
+            $request = new Request();
+            $request->header('Content-Type', 'application/json');
+            $this->assertEquals('application/json', $request->header('Content-Type'), 'Old getter should work');
+
+            $this->assertEquals('application/json', $request->getHeaderLine('Content-Type'), 'getHeaderLine works');
+            $this->assertEquals('application/json', $request->getHeaderLine('content-type'), 'getHeaderLine works');
+            $this->assertEquals(['application/json'], $request->getHeader('Content-Type'), 'getHeader works');
+            $this->assertEquals(['application/json'], $request->getHeader('content-type'), 'getHeader works');
+        });
     }
 
     /**
      * test cookie method.
      *
+     * @group deprecated
      * @return void
      */
     public function testCookie()
     {
-        $request = new Request();
-        $result = $request->cookie('session', '123456');
-        $this->assertSame($result, $request, 'Should return self');
+        $this->deprecated(function () {
+            $request = new Request();
+            $result = $request->cookie('session', '123456');
+            $this->assertSame($result, $request, 'Should return self');
 
-        $this->assertNull($request->cookie('not set'));
+            $this->assertNull($request->cookie('not set'));
 
-        $result = $request->cookie('session');
-        $this->assertEquals('123456', $result);
+            $result = $request->cookie('session');
+            $this->assertEquals('123456', $result);
+        });
     }
 
     /**
      * test version method.
      *
+     * @group deprecated
      * @return void
      */
     public function testVersion()
     {
-        $request = new Request();
-        $result = $request->version('1.0');
-        $this->assertSame($request, $result, 'Should return self');
+        $this->deprecated(function () {
+            $request = new Request();
+            $result = $request->version('1.0');
+            $this->assertSame($request, $result, 'Should return self');
 
-        $this->assertSame('1.0', $request->version());
+            $this->assertSame('1.0', $request->version());
+        });
     }
 
     /**
      * test version Interoperable.
      *
+     * @group deprecated
      * @return void
      */
     public function testVersionInteroperability()
     {
-        $request = new Request();
-        $this->assertEquals('1.1', $request->version());
-        $this->assertEquals('1.1', $request->getProtocolVersion());
-
-        $request = $request->withProtocolVersion('1.0');
-        $this->assertEquals('1.0', $request->version());
-        $this->assertEquals('1.0', $request->getProtocolVersion());
+        $this->deprecated(function () {
+            $request = new Request();
+            $this->assertEquals('1.1', $request->version());
+            $this->assertEquals('1.1', $request->getProtocolVersion());
+
+            $request = $request->withProtocolVersion('1.0');
+            $this->assertEquals('1.0', $request->version());
+            $this->assertEquals('1.0', $request->getProtocolVersion());
+        });
     }
 }

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

@@ -64,15 +64,15 @@ class ResponseTest extends TestCase
         ];
         $response = new Response($headers, 'ok');
 
-        $this->assertSame(200, $response->statusCode());
-        $this->assertEquals('1.0', $response->version());
+        $this->assertSame(200, $response->getStatusCode());
+        $this->assertEquals('1.0', $response->getProtocolVersion());
         $this->assertEquals(
             'text/html;charset="UTF-8"',
-            $response->header('content-type')
+            $response->getHeaderLine('content-type')
         );
         $this->assertEquals(
             'Tue, 25 Dec 2012 04:43:47 GMT',
-            $response->header('Date')
+            $response->getHeaderLine('Date')
         );
 
         $this->assertEquals(
@@ -86,8 +86,8 @@ class ResponseTest extends TestCase
         ];
         $response = new Response($headers, 'ok');
 
-        $this->assertEquals('1.0', $response->version());
-        $this->assertSame(200, $response->statusCode());
+        $this->assertEquals('1.0', $response->getProtocolVersion());
+        $this->assertSame(200, $response->getStatusCode());
     }
 
     /**
@@ -263,38 +263,41 @@ XML;
     /**
      * Test parsing / getting cookies.
      *
+     * @group deprecated
      * @return void
      */
     public function testCookie()
     {
-        $headers = [
-            'HTTP/1.0 200 Ok',
-            'Set-Cookie: test=value',
-            'Set-Cookie: session=123abc',
-            'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
-        ];
-        $response = new Response($headers, '');
-        $this->assertEquals('value', $response->cookie('test'));
-        $this->assertEquals('123abc', $response->cookie('session'));
-        $this->assertEquals('soon', $response->cookie('expiring'));
-
-        $result = $response->cookie('expiring', true);
-        $this->assertTrue($result['httponly']);
-        $this->assertTrue($result['secure']);
-        $this->assertEquals(
-            'Wed, 09-Jun-2021 10:18:14 GMT',
-            $result['expires']
-        );
-        $this->assertEquals('/', $result['path']);
-
-        $result = $response->header('set-cookie');
-        $this->assertCount(3, $result, 'Should be an array.');
-
-        $this->assertTrue(isset($response->cookies));
-        $this->assertEquals(
-            'soon',
-            $response->cookies['expiring']['value']
-        );
+        $this->deprecated(function () {
+            $headers = [
+                'HTTP/1.0 200 Ok',
+                'Set-Cookie: test=value',
+                'Set-Cookie: session=123abc',
+                'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
+            ];
+            $response = new Response($headers, '');
+            $this->assertEquals('value', $response->cookie('test'));
+            $this->assertEquals('123abc', $response->cookie('session'));
+            $this->assertEquals('soon', $response->cookie('expiring'));
+
+            $result = $response->cookie('expiring', true);
+            $this->assertTrue($result['httponly']);
+            $this->assertTrue($result['secure']);
+            $this->assertEquals(
+                'Wed, 09-Jun-2021 10:18:14 GMT',
+                $result['expires']
+            );
+            $this->assertEquals('/', $result['path']);
+
+            $result = $response->header('set-cookie');
+            $this->assertCount(3, $result, 'Should be an array.');
+
+            $this->assertTrue(isset($response->cookies));
+            $this->assertEquals(
+                'soon',
+                $response->cookies['expiring']['value']
+            );
+        });
     }
 
     /**
@@ -361,14 +364,13 @@ XML;
      *
      * @return void
      */
-    public function testStatusCode()
+    public function testGetStatusCode()
     {
         $headers = [
             'HTTP/1.0 404 Not Found',
             'Content-Type: text/html'
         ];
         $response = new Response($headers, '');
-        $this->assertSame(404, $response->statusCode());
         $this->assertSame(404, $response->getStatusCode());
 
         $this->assertSame(404, $response->code);
@@ -376,17 +378,37 @@ XML;
     }
 
     /**
+     * Test statusCode()
+     *
+     * @group deprecated
+     * @return void
+     */
+    public function testStatusCode()
+    {
+        $this->deprecated(function () {
+            $headers = [
+                'HTTP/1.0 404 Not Found',
+                'Content-Type: text/html'
+            ];
+            $response = new Response($headers, '');
+            $this->assertSame(404, $response->statusCode());
+            $this->assertSame(404, $response->code);
+            $this->assertTrue(isset($response->code));
+        });
+    }
+
+    /**
      * Test reading the encoding out.
      *
      * @return void
      */
-    public function testEncoding()
+    public function testGetEncoding()
     {
         $headers = [
             'HTTP/1.0 200 Ok',
         ];
         $response = new Response($headers, '');
-        $this->assertNull($response->encoding());
+        $this->assertNull($response->getEncoding());
 
         $headers = [
             'HTTP/1.0 200 Ok',
@@ -394,7 +416,6 @@ XML;
         ];
         $response = new Response($headers, '');
         $this->assertNull($response->getEncoding());
-        $this->assertNull($response->encoding());
 
         $headers = [
             'HTTP/1.0 200 Ok',
@@ -402,7 +423,6 @@ XML;
         ];
         $response = new Response($headers, '');
         $this->assertEquals('UTF-8', $response->getEncoding());
-        $this->assertEquals('UTF-8', $response->encoding());
 
         $headers = [
             'HTTP/1.0 200 Ok',
@@ -410,7 +430,44 @@ XML;
         ];
         $response = new Response($headers, '');
         $this->assertEquals('ISO-8859-1', $response->getEncoding());
-        $this->assertEquals('ISO-8859-1', $response->encoding());
+    }
+
+    /**
+     * Test reading the encoding out.
+     *
+     * @group deprecated
+     * @return void
+     */
+    public function testEncoding()
+    {
+        $this->deprecated(function () {
+            $headers = [
+                'HTTP/1.0 200 Ok',
+            ];
+            $response = new Response($headers, '');
+            $this->assertNull($response->encoding());
+
+            $headers = [
+                'HTTP/1.0 200 Ok',
+                'Content-Type: text/html'
+            ];
+            $response = new Response($headers, '');
+            $this->assertNull($response->encoding());
+
+            $headers = [
+                'HTTP/1.0 200 Ok',
+                'Content-Type: text/html; charset="UTF-8"'
+            ];
+            $response = new Response($headers, '');
+            $this->assertEquals('UTF-8', $response->encoding());
+
+            $headers = [
+                'HTTP/1.0 200 Ok',
+                "Content-Type: text/html; charset='ISO-8859-1'"
+            ];
+            $response = new Response($headers, '');
+            $this->assertEquals('ISO-8859-1', $response->encoding());
+        });
     }
 
     /**