浏览代码

Add getCookies() method to response.

this method better follows the segregated getter method style we've been
adopting.
Mark Story 9 年之前
父节点
当前提交
d6a720b2c0
共有 2 个文件被更改,包括 53 次插入1 次删除
  1. 13 1
      src/Http/Response.php
  2. 40 0
      tests/TestCase/Network/ResponseTest.php

+ 13 - 1
src/Http/Response.php

@@ -1925,7 +1925,7 @@ class Response implements ResponseInterface
      * @param array|null $options Either null to get all cookies, string for a specific cookie
      *  or array to set cookie.
      * @return mixed
-     * @deprecated 3.4.0 Use getCookie() and withCookie() instead.
+     * @deprecated 3.4.0 Use getCookie(), getCookies() and withCookie() instead.
      */
     public function cookie($options = null)
     {
@@ -2023,6 +2023,18 @@ class Response implements ResponseInterface
     }
 
     /**
+     * Get all cookies in the response.
+     *
+     * Returns an associative array of cookie name => cookie data.
+     *
+     * @return array
+     */
+    public function getCookies()
+    {
+        return $this->_cookies;
+    }
+
+    /**
      * Setup access for origin and methods on cross origin requests
      *
      * This method allow multiple ways to setup the domains, see the examples

+ 40 - 0
tests/TestCase/Network/ResponseTest.php

@@ -1430,6 +1430,46 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Test getCookies() and array data.
+     *
+     * @return void
+     */
+    public function testGetCookies()
+    {
+        $response = new Response();
+        $cookie = [
+            'name' => 'ignored key',
+            'value' => '[a,b,c]',
+            'expire' => 1000,
+            'path' => '/test',
+            'secure' => true
+        ];
+        $new = $response->withCookie('testing', 'a')
+            ->withCookie('test2', ['value' => 'b', 'path' => '/test', 'secure' => true]);
+        $expected = [
+            'testing' => [
+                'name' => 'testing',
+                'value' => 'a',
+                'expire' => null,
+                'path' => '/',
+                'domain' => '',
+                'secure' => false,
+                'httpOnly' => false
+            ],
+            'test2' => [
+                'name' => 'test2',
+                'value' => 'b',
+                'expire' => null,
+                'path' => '/test',
+                'domain' => '',
+                'secure' => true,
+                'httpOnly' => false
+            ]
+        ];
+        $this->assertEquals($expected, $new->getCookies());
+    }
+
+    /**
      * Test CORS
      *
      * @dataProvider corsData