Browse Source

Working on the cookie implementation tests

Florian Krämer 9 years ago
parent
commit
d571324e38

+ 68 - 0
tests/TestCase/Http/Cookie/CookieCollectionTest.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.5.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Http\Cookie;
+
+use Cake\Http\Cookie\Cookie;
+use Cake\Http\Cookie\CookieCollection;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Cookie collection test.
+ */
+class CookieCollectionTest extends TestCase
+{
+    /**
+     * Test constructor
+     *
+     * @return void
+     */
+    public function testConstructorWithEmptyArray()
+    {
+        $collection = new CookieCollection([]);
+        $this->assertCount(0, $collection);
+    }
+
+    /**
+     * Test valid cookies
+     *
+     * @return void
+     */
+    public function testConstructorWithCookieArray()
+    {
+        $cookies = [
+            new Cookie('one', 'one'),
+            new Cookie('two', 'two')
+        ];
+
+        $collection = new CookieCollection($cookies);
+        $this->assertCount(2, $collection);
+    }
+
+    /**
+     * Test that the constructor takes only an array of objects implementing
+     * the CookieInterface
+     *
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Expected `Cake\Http\Cookie\CookieCollection[]` as $cookies but instead got `array` at index 1
+     * @return void
+     */
+    public function testConstructorWithInvalidCookieObjects()
+    {
+        $array = [
+            new Cookie('one', 'one'),
+            []
+        ];
+
+        new CookieCollection($array);
+    }
+}

+ 1 - 1
tests/TestCase/Http/Cookie/CookieTest.php

@@ -7,7 +7,7 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
+ * @since         3.5.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
 namespace Cake\Test\TestCase\Http\Cookie;

+ 2 - 4
tests/TestCase/Http/Cookie/RequestCookiesTest.php

@@ -7,7 +7,7 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
+ * @since         3.5.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
 namespace Cake\Test\TestCase\Http\Cookie;
@@ -31,9 +31,7 @@ class RequestCookiesTest extends TestCase
     public $request;
 
     /**
-     * setup
-     *
-     * @return null
+     * {@inheritDoc}
      */
     public function setUp()
     {

+ 27 - 2
tests/TestCase/Http/Cookie/ResponseCookiesTest.php

@@ -7,19 +7,44 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
+ * @since         3.5.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
 namespace Cake\Test\TestCase\Http\Cookie;
 
+use Cake\Http\Client\Response;
 use Cake\Http\Cookie\Cookie;
 use Cake\Http\Cookie\ResponseCookies;
 use Cake\TestSuite\TestCase;
 
 /**
- * HTTP cookies test.
+ * Response Cookies Test
  */
 class ResponseCookiesTest extends TestCase
 {
+    /**
+     * testAddToResponse
+     *
+     * @return void
+     */
+    public function testAddToResponse()
+    {
+        $cookies = [
+            new Cookie('one', 'one'),
+            new Cookie('two', 'two')
+        ];
 
+        $responseCookies = new ResponseCookies($cookies);
+
+        $response = new Response();
+        $response = $responseCookies->addToResponse($response);
+
+        $expected = [
+            'Set-Cookie' => [
+                'one=one',
+                'two=two'
+            ]
+        ];
+        $this->assertEquals($expected, $response->getHeaders());
+    }
 }