Browse Source

Move Cookie::toArrayClient() to Response::convertCookie()

Robert Pustułka 9 years ago
parent
commit
3db767bdc5

+ 4 - 14
src/Http/Client/Response.php

@@ -446,7 +446,7 @@ class Response extends Message implements ResponseInterface
 
         $cookie = $this->cookies->get($name);
 
-        return $this->toArrayClient($cookie);
+        return $this->convertCookie($cookie);
     }
 
     /**
@@ -458,18 +458,8 @@ class Response extends Message implements ResponseInterface
      * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
      * @return array
      */
-    protected function toArrayClient(CookieInterface $cookie)
+    public function convertCookie(CookieInterface $cookie)
     {
-        if ($cookie instanceof Cookie) {
-            return $cookie->toArrayClient();
-        }
-
-        if ($cookie->getExpiry()) {
-            $expires = $cookie->getExpiry()->format(Cookie::EXPIRES_FORMAT);
-        } else {
-            $expires = '';
-        }
-
         return [
             'name' => $cookie->getName(),
             'value' => $cookie->getValue(),
@@ -477,7 +467,7 @@ class Response extends Message implements ResponseInterface
             'domain' => $cookie->getDomain(),
             'secure' => $cookie->isSecure(),
             'httponly' => $cookie->isHttpOnly(),
-            'expires' => $expires
+            'expires' => $cookie->getFormattedExpires()
         ];
     }
 
@@ -505,7 +495,7 @@ class Response extends Message implements ResponseInterface
 
         $cookies = [];
         foreach ($this->cookies as $cookie) {
-            $cookies[$cookie->getName()] = $this->toArrayClient($cookie);
+            $cookies[$cookie->getName()] = $this->convertCookie($cookie);
         }
 
         return $cookies;

+ 0 - 21
src/Http/Cookie/Cookie.php

@@ -622,27 +622,6 @@ class Cookie implements CookieInterface
     /**
      * Convert the cookie into an array of its properties.
      *
-     * This method is compatible with older client code that
-     * expects date strings instead of timestamps.
-     *
-     * @return array
-     */
-    public function toArrayClient()
-    {
-        return [
-            'name' => $this->getName(),
-            'value' => $this->getValue(),
-            'path' => $this->getPath(),
-            'domain' => $this->getDomain(),
-            'secure' => $this->isSecure(),
-            'httponly' => $this->isHttpOnly(),
-            'expires' => $this->getFormattedExpires()
-        ];
-    }
-
-    /**
-     * Convert the cookie into an array of its properties.
-     *
      * This method is compatible with the historical behavior of Cake\Http\Response,
      * where `httponly` is `httpOnly` and `expires` is `expire`
      *

+ 29 - 0
tests/TestCase/Http/Client/ResponseTest.php

@@ -13,7 +13,9 @@
  */
 namespace Cake\Test\TestCase\Http\Client;
 
+use Cake\Chronos\Chronos;
 use Cake\Http\Client\Response;
+use Cake\Http\Cookie\Cookie;
 use Cake\Http\Cookie\CookieCollection;
 use Cake\TestSuite\TestCase;
 
@@ -428,4 +430,31 @@ XML;
         $response = new Response($headers, $body);
         $this->assertEquals('Hello world!', $response->body);
     }
+
+    /**
+     * Test convertCookie
+     *
+     * @return void
+     */
+    public function testConvertCookie()
+    {
+        $date = Chronos::parse('2017-03-31 12:34:56');
+        $cookie = new Cookie('cakephp', 'cakephp-rocks');
+        $cookie = $cookie->withDomain('cakephp.org')
+            ->withPath('/api')
+            ->withExpiry($date)
+            ->withHttpOnly(true)
+            ->withSecure(true);
+        $expected = [
+            'name' => 'cakephp',
+            'value' => 'cakephp-rocks',
+            'path' => '/api',
+            'domain' => 'cakephp.org',
+            'expires' => 'Fri, 31-Mar-2017 12:34:56 GMT',
+            'secure' => true,
+            'httponly' => true
+        ];
+        $response = new Response([], '');
+        $this->assertEquals($expected, $response->convertCookie($cookie));
+    }
 }

+ 0 - 26
tests/TestCase/Http/Cookie/CookieTest.php

@@ -570,32 +570,6 @@ class CookieTest extends TestCase
     }
 
     /**
-     * Test toArrayClient
-     *
-     * @return void
-     */
-    public function testToArrayClient()
-    {
-        $date = Chronos::parse('2017-03-31 12:34:56');
-        $cookie = new Cookie('cakephp', 'cakephp-rocks');
-        $cookie = $cookie->withDomain('cakephp.org')
-            ->withPath('/api')
-            ->withExpiry($date)
-            ->withHttpOnly(true)
-            ->withSecure(true);
-        $expected = [
-            'name' => 'cakephp',
-            'value' => 'cakephp-rocks',
-            'path' => '/api',
-            'domain' => 'cakephp.org',
-            'expires' => 'Fri, 31-Mar-2017 12:34:56 GMT',
-            'secure' => true,
-            'httponly' => true
-        ];
-        $this->assertEquals($expected, $cookie->toArrayClient());
-    }
-
-    /**
      * Test toArrayResponse
      *
      * @return void