Skip to content
Browse files

Merge pull request #13 from avdg/add/cookieArraySettings

Add/cookie array settings
  • Loading branch information...
2 parents 3251d8b + 315f859 commit 62c35068546313ae747bd5f410867bc5ea3f5811 @dragoonis dragoonis committed Dec 27, 2011
Showing with 81 additions and 66 deletions.
  1. +15 −30 PPI/Request/Cookie.php
  2. +66 −36 PPI/Test/Request/CookieTest.php
View
45 PPI/Request/Cookie.php
@@ -30,19 +30,6 @@ function __construct(array $cookies = array()) {
}
- /*
- * Sync local settings with global settings
- *
- * @return void
- */
- protected function resetSettings() {
- $this->_expire = self::$expire;
- $this->_path = self::$path;
- $this->_domain = self::$domain;
- $this->_secure = self::$secure;
- $this->_httponly = self::$httponly;
- }
-
/**
* Changes object settings
*
@@ -51,7 +38,14 @@ protected function resetSettings() {
*
* @return void
*/
- public function setSetting($option, $value) {
+ public function setSetting($option, $value = null) {
+ if (is_array($option)) {
+ foreach ($option as $key => $value) {
+ $this->setSetting($key, $value);
+ }
+ return;
+ }
+
if(array_key_exists($option, $this->_defaults)) {
$this->_defaults[$option] = $value;
}
@@ -68,19 +62,11 @@ public function setSetting($option, $value) {
* @return void
*/
public function offsetSet($offset, $value) {
-
if ($value === null) {
- $this->offsetUnset($offset);
+ return $this->offsetUnset($offset);
}
- $this->_array[$offset] = $value;
-
- if ($this->_isCollected) {
- setcookie($offset,
- $value, $this->_defaults['expire'], $this->_defaults['path'],
- $this->_defaults['domain'], $this->_defaults['secure'], $this->_defaults['httponly']
- );
- }
+ $this->setCookie($offset, array('content' => $value));
}
/**
@@ -92,12 +78,11 @@ public function offsetSet($offset, $value) {
* @return void
*/
public function offsetUnset($offset) {
-
- unset($this->_array[$offset]);
-
- if ($this->_isCollected) {
- setcookie($offset, null, time() - 3600);
- }
+ $this->setcookie($offset, array(
+ 'content' => null,
+ 'expire' => time() - 3600,
+ ));
+ unset($this->_array[$offset]); // FIXME I think setCookie is currently too 'smart'
}
/**
View
102 PPI/Test/Request/CookieTest.php
@@ -50,10 +50,38 @@ public function testCustomCookie() {
* No coverage for setcookie, so use a dummy instead
*/
public function testWriteCookie() {
-
+ $cookie = new CookieDummy();
$cookie['foo'] = 'blah';
-
$this->assertEquals('blah', $cookie['foo']);
+ $this->assertEquals($cookie->setCookies[0], array(
+ 'name' => 'foo',
+ 'content' => 'blah',
+ 'expire' => 0,
+ 'path' => null,
+ 'domain' => null,
+ 'secure' => null,
+ 'httponly' => null,
+ ));
+ }
+
+ /**
+ * No coverage for setcookie, so use a dummy instead
+ */
+ public function testRemoveCookie() {
+ $cookie = new CookieDummy();
+ $this->assertEmpty($cookie->setCookies);
+
+ $cookie['foo'] = null;
+ $this->assertEquals(null, $cookie['foo']);
+ $this->assertEquals(array(
+ 'name' => 'foo',
+ 'content' => null,
+ 'expire' => time() - 3600, // @TODO may fail on slow tests
+ 'path' => null,
+ 'domain' => null,
+ 'secure' => null,
+ 'httponly' => null,
+ ), $cookie->setCookies[0]);
}
/**
@@ -85,50 +113,52 @@ public function testWriteCookieWithChangedSettings() {
/**
* No coverage for setcookie, so use a dummy instead
*/
- public function testWriteCookieWithGlobalSettings() {
+ public function testWriteCookieWithArraySettings() {
+ $cookie = new CookieDummy();
+ $this->assertEmpty($cookie->setCookies);
- $cookie = new Cookie(array('foo' => 'foo_temp'));
- $cookie->setSetting('expire', 20);
- $cookie->setSetting('path', '/test');
- $cookie->setSetting('domain', '.ppi.io');
- $cookie->setSetting('secure', false);
- $cookie->setSetting('httponly', false);
+ $cookie->setSetting(array(
+ 'expire' => 10,
+ 'path' => '/',
+ 'domain' => '.example.com',
+ 'secure' => true,
+ 'httponly' => true,
+ ));
$cookie['foo'] = 'blah';
- $this->assertEquals($cookie->getCookie('foo'), array(
+ $this->assertEquals($cookie->setCookies[0], array(
'name' => 'foo',
'content' => 'blah',
- 'expire' => 20,
- 'path' => '/test',
- 'domain' => '.ppi.io',
- 'secure' => false,
- 'httponly' => false,
+ 'expire' => 10,
+ 'path' => '/',
+ 'domain' => '.example.com',
+ 'secure' => true,
+ 'httponly' => true,
));
}
- /**
- * No coverage for setcookie, so use a dummy instead
- */
- public function testWriteCookieWithGlobalAndChangedSettings() {
+}
- $cookie = new Cookie(array('foo' => 'bar'));
+class CookieDummy extends Cookie {
+ public $setCookies = array();
- $cookie->setSetting('expire', 10);
- $cookie->setSetting('path', '/test');
- $cookie->setSetting('domain', '.ppi.io');
- $cookie->setSetting('secure', true);
- $cookie->setSetting('httponly', false);
+ public function setCookie($key, array $options = array()) {
+ $options = array_merge($this->_defaults, $options);
- // intentional missing httponly param
- $cookie['foo'] = 'blah';
- $this->assertEquals($cookie->getCookie('foo'), array(
- 'name' => 'foo',
- 'content' => 'blah',
- 'expire' => 10,
- 'path' => '/test',
- 'domain' => '.ppi.io',
- 'secure' => true,
- 'httponly' => false,
- ));
+ $this->_array[$key] = $options['content'];
+
+ if (!$this->_isCollected) {
+ return true;
+ }
+
+ $this->setCookies[] = array(
+ 'name' => $key,
+ 'content' => $options['content'],
+ 'expire' => $options['expire'],
+ 'path' => $options['path'],
+ 'domain' => $options['domain'],
+ 'secure' => $options['secure'],
+ 'httponly' => $options['httponly'],
+ );
}
}

0 comments on commit 62c3506

Please sign in to comment.
Something went wrong with that request. Please try again.