Browse Source

Make CookieInterface::toArray() return a flat array.

ADmad 6 years ago
parent
commit
8cc3cf9dbd

+ 1 - 2
src/Http/Cookie/Cookie.php

@@ -729,8 +729,7 @@ class Cookie implements CookieInterface
         return [
             'name' => $this->name,
             'value' => $this->getScalarValue(),
-            'options' => $this->getOptions(),
-        ];
+        ] + $this->getOptions();
     }
 
     /**

+ 1 - 1
src/Http/Cookie/CookieInterface.php

@@ -237,7 +237,7 @@ interface CookieInterface
     /**
      * Get cookie data as array.
      *
-     * @return array With keys `name`, `value` and `options`.
+     * @return array With keys `name`, `value`, `expires` etc. options.
      */
     public function toArray(): array;
 

+ 4 - 4
tests/TestCase/Http/Client/ResponseTest.php

@@ -354,13 +354,13 @@ XML;
 
         $result = $response->getCookieData('expiring');
         $this->assertSame('soon', $result['value']);
-        $this->assertTrue($result['options']['httponly']);
-        $this->assertTrue($result['options']['secure']);
+        $this->assertTrue($result['httponly']);
+        $this->assertTrue($result['secure']);
         $this->assertEquals(
             strtotime('Wed, 09-Jun-2021 10:18:14 GMT'),
-            $result['options']['expires']
+            $result['expires']
         );
-        $this->assertSame('/', $result['options']['path']);
+        $this->assertSame('/', $result['path']);
 
         $result = $response->getCookies();
         $this->assertCount(3, $result);

+ 6 - 6
tests/TestCase/Http/Middleware/CsrfProtectionMiddlewareTest.php

@@ -92,8 +92,8 @@ class CsrfProtectionMiddlewareTest extends TestCase
         $cookie = $response->getCookie('csrfToken');
         $this->assertNotEmpty($cookie, 'Should set a token.');
         $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
-        $this->assertSame(0, $cookie['options']['expires'], 'session duration.');
-        $this->assertSame('/dir/', $cookie['options']['path'], 'session path.');
+        $this->assertSame(0, $cookie['expires'], 'session duration.');
+        $this->assertSame('/dir/', $cookie['path'], 'session path.');
         $this->assertEquals($cookie['value'], $updatedRequest->getAttribute('csrfToken'));
     }
 
@@ -278,10 +278,10 @@ class CsrfProtectionMiddlewareTest extends TestCase
         $cookie = $response->getCookie('token');
         $this->assertNotEmpty($cookie, 'Should set a token.');
         $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
-        $this->assertWithinRange(strtotime('+1 hour'), $cookie['options']['expires'], 1, 'session duration.');
-        $this->assertSame('/dir/', $cookie['options']['path'], 'session path.');
-        $this->assertTrue($cookie['options']['secure'], 'cookie security flag missing');
-        $this->assertTrue($cookie['options']['httponly'], 'cookie httpOnly flag missing');
+        $this->assertWithinRange(strtotime('+1 hour'), $cookie['expires'], 1, 'session duration.');
+        $this->assertSame('/dir/', $cookie['path'], 'session path.');
+        $this->assertTrue($cookie['secure'], 'cookie security flag missing');
+        $this->assertTrue($cookie['httponly'], 'cookie httpOnly flag missing');
     }
 
     /**

+ 34 - 43
tests/TestCase/Http/ResponseTest.php

@@ -783,13 +783,11 @@ class ResponseTest extends TestCase
         $expected = [
             'name' => 'testing',
             'value' => '',
-            'options' => [
-                'expires' => 0,
-                'path' => '/',
-                'domain' => '',
-                'secure' => false,
-                'httponly' => false,
-            ],
+            'expires' => 0,
+            'path' => '/',
+            'domain' => '',
+            'secure' => false,
+            'httponly' => false,
         ];
         $result = $new->getCookie('testing');
         $this->assertEquals($expected, $result);
@@ -843,17 +841,15 @@ class ResponseTest extends TestCase
         $expected = [
             'name' => 'testing',
             'value' => '[a,b,c]',
-            'options' => [
-                'expires' => $expiry,
-                'path' => '/test',
-                'domain' => '',
-                'secure' => true,
-                'httponly' => false,
-            ],
+            'expires' => $expiry,
+            'path' => '/test',
+            'domain' => '',
+            'secure' => true,
+            'httponly' => false,
         ];
 
         // Match the date time formatting to Response::convertCookieToArray
-        $expected['options']['expires'] = $expiry->format('U');
+        $expected['expires'] = $expiry->format('U');
 
         $this->assertEquals($expected, $new->getCookie('testing'));
     }
@@ -882,8 +878,8 @@ class ResponseTest extends TestCase
 
         $new = $response->withExpiredCookie(new Cookie('testing'));
 
-        $this->assertSame(0, $response->getCookie('testing')['options']['expires']);
-        $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$new->getCookie('testing')['options']['expires']);
+        $this->assertSame(0, $response->getCookie('testing')['expires']);
+        $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$new->getCookie('testing')['expires']);
     }
 
     /**
@@ -913,12 +909,13 @@ class ResponseTest extends TestCase
         $response = $response->withCookie($cookie);
 
         $options['options']['expires'] = $options['options']['expires']->format('U');
-        $this->assertEquals($options, $response->getCookie('testing'));
+        $expected = ['name' => $options['name'], 'value' => $options['value']] + $options['options'];
+        $this->assertEquals($expected, $response->getCookie('testing'));
 
         $expiredCookie = $response->withExpiredCookie($cookie);
 
-        $this->assertEquals($options['options']['expires'], $response->getCookie('testing')['options']['expires']);
-        $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$expiredCookie->getCookie('testing')['options']['expires']);
+        $this->assertEquals($expected['expires'], $response->getCookie('testing')['expires']);
+        $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$expiredCookie->getCookie('testing')['expires']);
     }
 
     /**
@@ -933,8 +930,8 @@ class ResponseTest extends TestCase
 
         $new = $response->withExpiredCookie($cookie);
 
-        $this->assertSame(0, $response->getCookie('yay')['options']['expires']);
-        $this->assertSame(1, $new->getCookie('yay')['options']['expires']);
+        $this->assertSame(0, $response->getCookie('yay')['expires']);
+        $this->assertSame(1, $new->getCookie('yay')['expires']);
     }
 
     /**
@@ -951,24 +948,20 @@ class ResponseTest extends TestCase
             'testing' => [
                 'name' => 'testing',
                 'value' => 'a',
-                'options' => [
-                    'expires' => 0,
-                    'path' => '/',
-                    'domain' => '',
-                    'secure' => false,
-                    'httponly' => false,
-                ],
+                'expires' => 0,
+                'path' => '/',
+                'domain' => '',
+                'secure' => false,
+                'httponly' => false,
             ],
             'test2' => [
                 'name' => 'test2',
                 'value' => 'b',
-                'options' => [
-                    'expires' => 0,
-                    'path' => '/test',
-                    'domain' => '',
-                    'secure' => true,
-                    'httponly' => false,
-                ],
+                'expires' => 0,
+                'path' => '/test',
+                'domain' => '',
+                'secure' => true,
+                'httponly' => false,
             ],
         ];
         $this->assertEquals($expected, $new->getCookies());
@@ -991,13 +984,11 @@ class ResponseTest extends TestCase
             'urmc' => [
                 'name' => 'urmc',
                 'value' => '{"user_id":1,"token":"abc123"}',
-                'options' => [
-                    'expires' => 0,
-                    'path' => '/',
-                    'domain' => '',
-                    'secure' => false,
-                    'httponly' => true,
-                ],
+                'expires' => 0,
+                'path' => '/',
+                'domain' => '',
+                'secure' => false,
+                'httponly' => true,
             ],
         ];
         $this->assertEquals($expected, $new->getCookies());