Browse Source

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

Robert Pustułka 9 years ago
parent
commit
c3c8516ed9

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

@@ -620,27 +620,6 @@ class Cookie implements CookieInterface
     }
 
     /**
-     * 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`
-     *
-     * @return array
-     */
-    public function toArrayResponse()
-    {
-        return [
-            'name' => $this->getName(),
-            'value' => $this->getValue(),
-            'path' => $this->getPath(),
-            'domain' => $this->getDomain(),
-            'secure' => $this->isSecure(),
-            'httpOnly' => $this->isHttpOnly(),
-            'expire' => $this->getExpiresTimestamp()
-        ];
-    }
-
-    /**
      * Implode method to keep keys are multidimensional arrays
      *
      * @param array $array Map of key and values

+ 5 - 15
src/Http/Response.php

@@ -1941,7 +1941,7 @@ class Response implements ResponseInterface
 
             $cookie = $this->_cookies->get($options);
 
-            return $this->toArrayResponse($cookie);
+            return $this->convertCookie($cookie);
         }
 
         $options += [
@@ -2047,7 +2047,7 @@ class Response implements ResponseInterface
 
         $cookie = $this->_cookies->get($name);
 
-        return $this->toArrayResponse($cookie);
+        return $this->convertCookie($cookie);
     }
 
     /**
@@ -2061,7 +2061,7 @@ class Response implements ResponseInterface
     {
         $out = [];
         foreach ($this->_cookies as $cookie) {
-            $out[$cookie->getName()] = $this->toArrayResponse($cookie);
+            $out[$cookie->getName()] = $this->convertCookie($cookie);
         }
 
         return $out;
@@ -2076,18 +2076,8 @@ class Response implements ResponseInterface
      * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
      * @return array
      */
-    protected function toArrayResponse(CookieInterface $cookie)
+    public function convertCookie(CookieInterface $cookie)
     {
-        if ($cookie instanceof Cookie) {
-            return $cookie->toArrayResponse();
-        }
-
-        if ($cookie->getExpiry()) {
-            $expires = $cookie->getExpiry()->format('U');
-        } else {
-            $expires = '';
-        }
-
         return [
             'name' => $cookie->getName(),
             'value' => $cookie->getValue(),
@@ -2095,7 +2085,7 @@ class Response implements ResponseInterface
             'domain' => $cookie->getDomain(),
             'secure' => $cookie->isSecure(),
             'httpOnly' => $cookie->isHttpOnly(),
-            'expire' => $expires
+            'expire' => $cookie->getExpiresTimestamp()
         ];
     }
 

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

@@ -568,30 +568,4 @@ class CookieTest extends TestCase
         ];
         $this->assertEquals($expected, $cookie->toArray());
     }
-
-    /**
-     * Test toArrayResponse
-     *
-     * @return void
-     */
-    public function testToArrayResponse()
-    {
-        $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',
-            'expire' => $date->format('U'),
-            'secure' => true,
-            'httpOnly' => true
-        ];
-        $this->assertEquals($expected, $cookie->toArrayResponse());
-    }
 }

+ 28 - 0
tests/TestCase/Http/ResponseTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\Http;
 
 include_once CORE_TEST_CASES . DS . 'Http' . DS . 'server_mocks.php';
 
+use Cake\Chronos\Chronos;
 use Cake\Http\Cookie\Cookie;
 use Cake\Http\Cookie\CookieCollection;
 use Cake\Http\Response;
@@ -3050,4 +3051,31 @@ class ResponseTest extends TestCase
         ];
         $this->assertEquals($expected, $result);
     }
+
+    /**
+     * 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',
+            'expire' => $date->format('U'),
+            'secure' => true,
+            'httpOnly' => true
+        ];
+        $response = new Response();
+        $this->assertEquals($expected, $response->convertCookie($cookie));
+    }
 }