Skip to content

Commit

Permalink
Rewrite Cookie tests to be runnable and cover current behaviour
Browse files Browse the repository at this point in the history
The cookie tests were all being skipped because headers are always sent
by the PHPUnit test runner (fixes kohana/kohana#47).

Additionally even when they ran they were covering very little of the actual
logic of the class, mostly just asserting that setcookie returns TRUE. Rewrote
the tests completely and slightly refactored the Cookie class to allow
mocking of the timestamp and setcookie calls.

Refs #547.
  • Loading branch information
acoulton committed Sep 22, 2014
1 parent a9db554 commit 3db5bce
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 94 deletions.
60 changes: 49 additions & 11 deletions classes/Kohana/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function get($key, $default = NULL)
}

// The cookie signature is invalid, delete it
Cookie::delete($key);
static::delete($key);
}

return $default;
Expand All @@ -88,33 +88,38 @@ public static function get($key, $default = NULL)
* Sets a signed cookie. Note that all cookie values must be strings and no
* automatic serialization will be performed!
*
* [!!] By default, Cookie::$expiration is 0 - if you skip/pass NULL for the optional
* lifetime argument your cookies will expire immediately unless you have separately
* configured Cookie::$expiration.
*
*
* // Set the "theme" cookie
* Cookie::set('theme', 'red');
*
* @param string $name name of cookie
* @param string $value value of cookie
* @param integer $expiration lifetime in seconds
* @param integer $lifetime lifetime in seconds
* @return boolean
* @uses Cookie::salt
*/
public static function set($name, $value, $expiration = NULL)
public static function set($name, $value, $lifetime = NULL)
{
if ($expiration === NULL)
if ($lifetime === NULL)
{
// Use the default expiration
$expiration = Cookie::$expiration;
$lifetime = Cookie::$expiration;
}

if ($expiration !== 0)
if ($lifetime !== 0)
{
// The expiration is expected to be a UNIX timestamp
$expiration += time();
$lifetime += static::_time();
}

// Add the salt to the cookie value
$value = Cookie::salt($name, $value).'~'.$value;

return setcookie($name, $value, $expiration, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
return static::_setcookie($name, $value, $lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}

/**
Expand All @@ -131,16 +136,18 @@ public static function delete($name)
unset($_COOKIE[$name]);

// Nullify the cookie and make it expire
return setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
return static::_setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}

/**
* Generates a salt string for a cookie based on the name and value.
*
* $salt = Cookie::salt('theme', 'red');
*
* @param string $name name of cookie
* @param string $value value of cookie
* @param string $name name of cookie
* @param string $value value of cookie
*
* @throws Kohana_Exception if Cookie::$salt is not configured
* @return string
*/
public static function salt($name, $value)
Expand All @@ -157,4 +164,35 @@ public static function salt($name, $value)
return sha1($agent.$name.$value.Cookie::$salt);
}

/**
* Proxy for the native setcookie function - to allow mocking in unit tests so that they do not fail when headers
* have been sent.
*
* @param string $name
* @param string $value
* @param integer $expire
* @param string $path
* @param string $domain
* @param boolean $secure
* @param boolean $httponly
*
* @return bool
* @see setcookie
*/
protected static function _setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)
{
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}

/**
* Proxy for the native time function - to allow mocking of time-related logic in unit tests
*
* @return int
* @see time
*/
protected static function _time()
{
return time();
}

}
Loading

0 comments on commit 3db5bce

Please sign in to comment.