From f1f9754bcb69fb69192fed08e298026e13d958e8 Mon Sep 17 00:00:00 2001 From: Wouter de Wild Date: Thu, 14 Jan 2016 13:21:56 +0100 Subject: [PATCH] [HttpFoundation][Cookie] Cookie DateTimeInterface fix --- src/Symfony/Component/HttpFoundation/Cookie.php | 16 ++++++++-------- .../HttpFoundation/Tests/CookieTest.php | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Cookie.php b/src/Symfony/Component/HttpFoundation/Cookie.php index 061703d9ca..13d69f3bd2 100644 --- a/src/Symfony/Component/HttpFoundation/Cookie.php +++ b/src/Symfony/Component/HttpFoundation/Cookie.php @@ -29,13 +29,13 @@ class Cookie /** * Constructor. * - * @param string $name The name of the cookie - * @param string $value The value of the cookie - * @param int|string|\DateTime $expire The time the cookie expires - * @param string $path The path on the server in which the cookie will be available on - * @param string $domain The domain that the cookie is available to - * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client - * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol + * @param string $name The name of the cookie + * @param string $value The value of the cookie + * @param int|string|\DateTime|\DateTimeInterface $expire The time the cookie expires + * @param string $path The path on the server in which the cookie will be available on + * @param string $domain The domain that the cookie is available to + * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client + * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol * * @throws \InvalidArgumentException */ @@ -51,7 +51,7 @@ class Cookie } // convert expiration time to a Unix timestamp - if ($expire instanceof \DateTime) { + if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) { $expire = $expire->format('U'); } elseif (!is_numeric($expire)) { $expire = strtotime($expire); diff --git a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php index fd90103e42..222786533e 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php @@ -85,6 +85,17 @@ class CookieTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date'); } + /** + * @requires PHP 5.5 + */ + public function testConstructorWithDateTimeImmutable() + { + $expire = new \DateTimeImmutable(); + $cookie = new Cookie('foo', 'bar', $expire); + + $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date'); + } + public function testGetExpiresTimeWithStringValue() { $value = '+1 day';