Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 0796f7d

Browse files
committed
Merge branch 'feature/arrays-httpclient-addcookie' of https://github.com/juriansluiman/zf2 into hotfix/http-header-cookie_arrays
6 parents d181c52 + d562686 + 67b42b2 + bdb1dae + 9809630 + 2e9129e commit 0796f7d

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/Client.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,19 @@
4343
*/
4444
class Client implements Dispatchable
4545
{
46-
/**#@+
46+
/**
4747
* @const string Supported HTTP Authentication methods
4848
*/
4949
const AUTH_BASIC = 'basic';
5050
const AUTH_DIGEST = 'digest'; // not implemented yet
51-
/**#@-*/
5251

53-
/**#@+
52+
/**
5453
* @const string POST data encoding methods
5554
*/
5655
const ENC_URLENCODED = 'application/x-www-form-urlencoded';
5756
const ENC_FORMDATA = 'multipart/form-data';
58-
/**#@-*/
5957

60-
/**#@+
58+
/**
6159
* @const string DIGEST Authentication
6260
*/
6361
const DIGEST_REALM = 'realm';
@@ -66,7 +64,6 @@ class Client implements Dispatchable
6664
const DIGEST_OPAQUE = 'opaque';
6765
const DIGEST_NC = 'nc';
6866
const DIGEST_CNONCE = 'cnonce';
69-
/**#@-*/
7067

7168
/**
7269
* @var Response
@@ -467,7 +464,7 @@ protected function getCookieId($cookie)
467464
/**
468465
* Add a cookie
469466
*
470-
* @param ArrayIterator|SetCookie|string $cookie
467+
* @param array|ArrayIterator|SetCookie|string $cookie
471468
* @param string $value
472469
* @param string $domain
473470
* @param string $expire
@@ -478,7 +475,7 @@ protected function getCookieId($cookie)
478475
*/
479476
public function addCookie($cookie, $value = null, $domain = null, $expire = null, $path = null, $secure = false, $httponly = true)
480477
{
481-
if ($cookie instanceof \ArrayIterator) {
478+
if (is_array($cookie) || $cookie instanceof \ArrayIterator) {
482479
foreach ($cookie as $setCookie) {
483480
if ($setCookie instanceof SetCookie) {
484481
$this->cookies[$this->getCookieId($setCookie)] = $setCookie;

test/ClientTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
namespace ZendTest\Http;
77

8-
use Zend\Http\Client;
8+
use Zend\Http\Client,
9+
Zend\Http\Header\SetCookie;
910

1011
class ClientTest extends \PHPUnit_Framework_TestCase
1112
{
@@ -37,4 +38,44 @@ public function testIfNullValueCookiesThrowsException()
3738
$client = new Client();
3839
$client->addCookie("test", null);
3940
}
41+
42+
public function testIfCookieHeaderCanBeSet()
43+
{
44+
$header = new SetCookie('foo');
45+
46+
$client = new Client();
47+
$client->addCookie($header);
48+
49+
$cookies = $client->getCookies();
50+
$this->assertEquals(1, count($cookies));
51+
$this->assertEquals($header, $cookies['foo']);
52+
}
53+
54+
public function testIfArrayOfHeadersCanBeSet()
55+
{
56+
$headers = array(
57+
new SetCookie('foo'),
58+
new SetCookie('bar')
59+
);
60+
61+
$client = new Client();
62+
$client->addCookie($headers);
63+
64+
$cookies = $client->getCookies();
65+
$this->assertEquals(2, count($cookies));
66+
}
67+
68+
public function testIfArrayIteratorOfHeadersCanBeSet()
69+
{
70+
$headers = new \ArrayIterator(array(
71+
new SetCookie('foo'),
72+
new SetCookie('bar')
73+
));
74+
75+
$client = new Client();
76+
$client->addCookie($headers);
77+
78+
$cookies = $client->getCookies();
79+
$this->assertEquals(2, count($cookies));
80+
}
4081
}

0 commit comments

Comments
 (0)