Browse Source

Merge pull request #13166 from cakephp/3.8-with-cookiecollection

Add Response::withCookieCollection().
Mark Story 7 years ago
parent
commit
413f7b3bda
2 changed files with 30 additions and 0 deletions
  1. 14 0
      src/Http/Response.php
  2. 16 0
      tests/TestCase/Http/ResponseTest.php

+ 14 - 0
src/Http/Response.php

@@ -2388,6 +2388,20 @@ class Response implements ResponseInterface
     }
 
     /**
+     * Get a new instance with provided cookie collection.
+     *
+     * @param \Cake\Http\Cookie\CookieCollection $cookieCollection Cookie collection to set.
+     * @return static
+     */
+    public function withCookieCollection(CookieCollection $cookieCollection)
+    {
+        $new = clone $this;
+        $new->_cookies = $cookieCollection;
+
+        return $new;
+    }
+
+    /**
      * Setup access for origin and methods on cross origin requests
      *
      * This method allow multiple ways to setup the domains, see the examples

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

@@ -1860,6 +1860,22 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Test withCookieCollection()
+     *
+     * @return void
+     */
+    public function testWithCookieCollection()
+    {
+        $response = new Response();
+        $collection = new CookieCollection([new Cookie('foo', 'bar')]);
+        $newResponse = $response->withCookieCollection($collection);
+
+        $this->assertNotSame($response, $newResponse);
+        $this->assertNotSame($response->getCookieCollection(), $newResponse->getCookieCollection());
+        $this->assertSame($newResponse->getCookie('foo')['value'], 'bar');
+    }
+
+    /**
      * Test that cors() returns a builder.
      *
      * @return void