Skip to content

Commit

Permalink
Merge branch '3.2'
Browse files Browse the repository at this point in the history
* 3.2:
  [ci] Update travis/appveyor
  [HttpFoundation] Validate/cast cookie expire time
  • Loading branch information
nicolas-grekas committed Jan 3, 2017
2 parents a422bc1 + c1bf268 commit 75acea0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
} elseif (!is_numeric($expire)) {
$expire = strtotime($expire);

if (false === $expire || -1 === $expire) {
if (false === $expire) {
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
}
}

$this->name = $name;
$this->value = $value;
$this->domain = $domain;
$this->expire = $expire;
$this->expire = 0 < $expire ? (int) $expire : 0;
$this->path = empty($path) ? '/' : $path;
$this->secure = (bool) $secure;
$this->httpOnly = (bool) $httpOnly;
Expand All @@ -147,7 +147,7 @@ public function __toString()
} else {
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());

if ($this->getExpiresTime() !== 0) {
if (0 !== $this->getExpiresTime()) {
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; max-age='.$this->getMaxAge();
}
}
Expand Down
16 changes: 15 additions & 1 deletion Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidChara
*/
public function testInvalidExpiration()
{
$cookie = new Cookie('MyCookie', 'foo', 'bar');
new Cookie('MyCookie', 'foo', 'bar');
}

public function testNegativeExpirationIsNotPossible()
{
$cookie = new Cookie('foo', 'bar', -100);

$this->assertSame(0, $cookie->getExpiresTime());
}

public function testGetValue()
Expand Down Expand Up @@ -81,6 +88,13 @@ public function testGetExpiresTime()
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
}

public function testGetExpiresTimeIsCastToInt()
{
$cookie = new Cookie('foo', 'bar', 3600.9);

$this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
}

public function testConstructorWithDateTime()
{
$expire = new \DateTime();
Expand Down

0 comments on commit 75acea0

Please sign in to comment.