diff --git a/Cookie.php b/Cookie.php index 30d1e00ab..2ac902685 100644 --- a/Cookie.php +++ b/Cookie.php @@ -112,7 +112,7 @@ 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.'); } } @@ -120,7 +120,7 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom $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; @@ -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(); } } diff --git a/Tests/CookieTest.php b/Tests/CookieTest.php index f7dd3f37d..6efd9c5f3 100644 --- a/Tests/CookieTest.php +++ b/Tests/CookieTest.php @@ -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() @@ -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();