From fa604d3c6f16f264863a42c200391ab996640296 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 13 Oct 2015 09:59:06 +0200 Subject: [PATCH] [Http*] Mock time() to fix transient tests --- .../Extension/Core/Type/DateTimeTypeTest.php | 1 - .../Extension/Core/Type/DateTypeTest.php | 1 - .../Extension/Core/Type/TimeTypeTest.php | 1 - .../Component/HttpFoundation/Response.php | 2 +- .../HttpFoundation/Tests/CookieTest.php | 2 +- .../HttpFoundation/Tests/ResponseTest.php | 37 ++++++++++--------- .../HttpKernel/HttpCache/HttpCache.php | 2 +- .../Tests/HttpCache/HttpCacheTest.php | 19 ++++++---- .../Tests/HttpCache/HttpCacheTestCase.php | 4 ++ 9 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php index fc003a07d3..5e88646837 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php @@ -262,7 +262,6 @@ class DateTimeTypeTest extends TestCase $this->assertDateTimeEquals($dateTime, $form->getData()); } - // Bug fix public function testInitializeWithDateTime() { // Throws an exception if "data_class" option is not explicitly set diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php index 6851e90caa..06b6c3bc7c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php @@ -694,7 +694,6 @@ class DateTypeTest extends TestCase $this->assertSame('single_text', $view->vars['widget']); } - // Bug fix public function testInitializeWithDateTime() { // Throws an exception if "data_class" option is not explicitly set diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php index bf12cefdcc..44e897b9c4 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php @@ -467,7 +467,6 @@ class TimeTypeTest extends TestCase $this->assertTrue($form->isPartiallyFilled()); } - // Bug fix public function testInitializeWithDateTime() { // Throws an exception if "data_class" option is not explicitly set diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 27361dee81..3c37187cd1 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -138,7 +138,7 @@ class Response $this->setStatusCode($status); $this->setProtocolVersion('1.0'); if (!$this->headers->has('Date')) { - $this->setDate(new \DateTime(null, new \DateTimeZone('UTC'))); + $this->setDate(\DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC'))); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php index 5b9438911d..b3bd4f6e4b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php @@ -95,7 +95,7 @@ class CookieTest extends \PHPUnit_Framework_TestCase $cookie = new Cookie('foo', 'bar', $value); $expire = strtotime($value); - $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date'); + $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1); } public function testGetDomain() diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 730ba09b43..72e143f9da 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -14,6 +14,9 @@ namespace Symfony\Component\HttpFoundation\Tests; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +/** + * @group time-sensitive + */ class ResponseTest extends ResponseTestCase { public function testCreate() @@ -246,16 +249,18 @@ class ResponseTest extends ResponseTestCase { $oneHourAgo = $this->createDateTimeOneHourAgo(); $response = new Response('', 200, array('Date' => $oneHourAgo->format(DATE_RFC2822))); - $this->assertEquals(0, $oneHourAgo->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present'); + $date = $response->getDate(); + $this->assertEquals($oneHourAgo->getTimestamp(), $date->getTimestamp(), '->getDate() returns the Date header if present'); $response = new Response(); $date = $response->getDate(); - $this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present'); + $this->assertEquals(time(), $date->getTimestamp(), '->getDate() returns the current Date if no Date header present'); $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822))); $now = $this->createDateTimeNow(); $response->headers->set('Date', $now->format(DATE_RFC2822)); - $this->assertLessThanOrEqual(1, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified'); + $date = $response->getDate(); + $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified'); $response = new Response('', 200); $response->headers->remove('Date'); @@ -275,7 +280,7 @@ class ResponseTest extends ResponseTestCase $response = new Response(); $response->headers->set('Cache-Control', 'must-revalidate'); $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822)); - $this->assertLessThanOrEqual(1, $response->getMaxAge() - 3600, '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present'); + $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present'); $response = new Response(); $response->headers->set('Cache-Control', 'must-revalidate'); @@ -346,7 +351,7 @@ class ResponseTest extends ResponseTestCase $response = new Response(); $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822)); - $this->assertLessThanOrEqual(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present'); + $this->assertEquals(3600, $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present'); $response = new Response(); $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)); @@ -359,7 +364,7 @@ class ResponseTest extends ResponseTestCase $response = new Response(); $response->headers->set('Cache-Control', 'max-age=60'); - $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present'); + $this->assertEquals(60, $response->getTtl(), '->getTtl() uses Cache-Control max-age when present'); } public function testSetClientTtl() @@ -524,7 +529,7 @@ class ResponseTest extends ResponseTestCase $response->setCache($options); $this->assertEquals($response->getEtag(), '"whatever"'); - $now = new \DateTime(); + $now = $this->createDateTimeNow(); $options = array('last_modified' => $now); $response->setCache($options); $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp()); @@ -583,7 +588,7 @@ class ResponseTest extends ResponseTestCase $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null'); - $now = new \DateTime(); + $now = $this->createDateTimeNow(); $response->setExpires($now); $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp()); @@ -592,7 +597,7 @@ class ResponseTest extends ResponseTestCase public function testSetLastModified() { $response = new Response(); - $response->setLastModified(new \DateTime()); + $response->setLastModified($this->createDateTimeNow()); $this->assertNotNull($response->getLastModified()); $response->setLastModified(null); @@ -777,7 +782,7 @@ class ResponseTest extends ResponseTestCase 'setCharset' => 'UTF-8', 'setPublic' => null, 'setPrivate' => null, - 'setDate' => new \DateTime(), + 'setDate' => $this->createDateTimeNow(), 'expire' => null, 'setMaxAge' => 1, 'setSharedMaxAge' => 1, @@ -810,21 +815,19 @@ class ResponseTest extends ResponseTestCase protected function createDateTimeOneHourAgo() { - $date = new \DateTime(); - - return $date->sub(new \DateInterval('PT1H')); + return $this->createDateTimeNow()->sub(new \DateInterval('PT1H')); } protected function createDateTimeOneHourLater() { - $date = new \DateTime(); - - return $date->add(new \DateInterval('PT1H')); + return $this->createDateTimeNow()->add(new \DateInterval('PT1H')); } protected function createDateTimeNow() { - return new \DateTime(); + $date = new \DateTime(); + + return $date->setTimestamp(time()); } protected function provideResponse() diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php index 694b63fe4d..07179faec4 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php @@ -190,7 +190,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface $this->restoreResponseBody($request, $response); - $response->setDate(new \DateTime(null, new \DateTimeZone('UTC'))); + $response->setDate(\DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC'))); if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) { $response->headers->set('X-Symfony-Cache', $this->getLog()); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index 87b8127554..bfc1dcdf8d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -15,6 +15,9 @@ use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +/** + * @group time-sensitive + */ class HttpCacheTest extends HttpCacheTestCase { public function testTerminateDelegatesTerminationOnlyForTerminableInterface() @@ -125,7 +128,7 @@ class HttpCacheTest extends HttpCacheTestCase public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified() { - $time = new \DateTime(); + $time = \DateTime::createFromFormat('U', time()); $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World'); $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822))); @@ -154,7 +157,7 @@ class HttpCacheTest extends HttpCacheTestCase public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch() { - $time = new \DateTime(); + $time = \DateTime::createFromFormat('U', time()); $this->setNextResponse(200, array(), '', function ($request, $response) use ($time) { $response->setStatusCode(200); @@ -593,7 +596,7 @@ class HttpCacheTest extends HttpCacheTestCase $this->assertTraceContains('miss'); $this->assertTraceContains('store'); $this->assertEquals('Hello World', $this->response->getContent()); - $this->assertRegExp('/s-maxage=(?:2|3)/', $this->response->headers->get('Cache-Control')); + $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control')); $this->request('GET', '/'); $this->assertHttpKernelIsNotCalled(); @@ -607,8 +610,8 @@ class HttpCacheTest extends HttpCacheTestCase $values = $this->getMetaStorageValues(); $this->assertCount(1, $values); $tmp = unserialize($values[0]); - $time = \DateTime::createFromFormat('U', time()); - $tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822); + $time = \DateTime::createFromFormat('U', time() - 5); + $tmp[0][1]['date'] = $time->format(DATE_RFC2822); $r = new \ReflectionObject($this->store); $m = $r->getMethod('save'); $m->setAccessible(true); @@ -657,8 +660,8 @@ class HttpCacheTest extends HttpCacheTestCase $values = $this->getMetaStorageValues(); $this->assertCount(1, $values); $tmp = unserialize($values[0]); - $time = \DateTime::createFromFormat('U', time()); - $tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822); + $time = \DateTime::createFromFormat('U', time() - 5); + $tmp[0][1]['date'] = $time->format(DATE_RFC2822); $r = new \ReflectionObject($this->store); $m = $r->getMethod('save'); $m->setAccessible(true); @@ -1199,7 +1202,7 @@ class HttpCacheTest extends HttpCacheTestCase public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses() { - $time = new \DateTime(); + $time = \DateTime::createFromFormat('U', time()); $responses = array( array( diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php index 766e2b1d49..fa8358da5e 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpKernel\HttpCache\Esi; use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpKernel\HttpCache\Store; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Bridge\PhpUnit\ClockMock; class HttpCacheTestCase extends \PHPUnit_Framework_TestCase { @@ -31,6 +32,9 @@ class HttpCacheTestCase extends \PHPUnit_Framework_TestCase protected function setUp() { + if (class_exists('Symfony\Bridge\PhpUnit\ClockMock')) { + ClockMock::register('Symfony\Component\HttpFoundation\Request'); + } $this->kernel = null; $this->cache = null;