Skip to content

Commit 28f0d57

Browse files
[Http*] Mock time() to fix transient tests
1 parent 6f4aa17 commit 28f0d57

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

HttpCache/HttpCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
190190

191191
$this->restoreResponseBody($request, $response);
192192

193-
$response->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
193+
$response->setDate(\DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC')));
194194

195195
if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
196196
$response->headers->set('X-Symfony-Cache', $this->getLog());

Tests/HttpCache/HttpCacheTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717

18+
/**
19+
* @group time-sensitive
20+
*/
1821
class HttpCacheTest extends HttpCacheTestCase
1922
{
2023
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
@@ -125,7 +128,7 @@ public function testDoesNotCacheRequestsWithACookieHeader()
125128

126129
public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
127130
{
128-
$time = new \DateTime();
131+
$time = \DateTime::createFromFormat('U', time());
129132

130133
$this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World');
131134
$this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
@@ -154,7 +157,7 @@ public function testRespondsWith304WhenIfNoneMatchMatchesETag()
154157

155158
public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
156159
{
157-
$time = new \DateTime();
160+
$time = \DateTime::createFromFormat('U', time());
158161

159162
$this->setNextResponse(200, array(), '', function ($request, $response) use ($time) {
160163
$response->setStatusCode(200);
@@ -593,7 +596,7 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
593596
$this->assertTraceContains('miss');
594597
$this->assertTraceContains('store');
595598
$this->assertEquals('Hello World', $this->response->getContent());
596-
$this->assertRegExp('/s-maxage=(?:2|3)/', $this->response->headers->get('Cache-Control'));
599+
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
597600

598601
$this->request('GET', '/');
599602
$this->assertHttpKernelIsNotCalled();
@@ -607,8 +610,8 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
607610
$values = $this->getMetaStorageValues();
608611
$this->assertCount(1, $values);
609612
$tmp = unserialize($values[0]);
610-
$time = \DateTime::createFromFormat('U', time());
611-
$tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822);
613+
$time = \DateTime::createFromFormat('U', time() - 5);
614+
$tmp[0][1]['date'] = $time->format(DATE_RFC2822);
612615
$r = new \ReflectionObject($this->store);
613616
$m = $r->getMethod('save');
614617
$m->setAccessible(true);
@@ -657,8 +660,8 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
657660
$values = $this->getMetaStorageValues();
658661
$this->assertCount(1, $values);
659662
$tmp = unserialize($values[0]);
660-
$time = \DateTime::createFromFormat('U', time());
661-
$tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822);
663+
$time = \DateTime::createFromFormat('U', time() - 5);
664+
$tmp[0][1]['date'] = $time->format(DATE_RFC2822);
662665
$r = new \ReflectionObject($this->store);
663666
$m = $r->getMethod('save');
664667
$m->setAccessible(true);
@@ -1199,7 +1202,7 @@ public function testXForwarderForHeaderForPassRequests()
11991202

12001203
public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
12011204
{
1202-
$time = new \DateTime();
1205+
$time = \DateTime::createFromFormat('U', time());
12031206

12041207
$responses = array(
12051208
array(

Tests/HttpCache/HttpCacheTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
1717
use Symfony\Component\HttpKernel\HttpCache\Store;
1818
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
use Symfony\Bridge\PhpUnit\ClockMock;
1920

2021
class HttpCacheTestCase extends \PHPUnit_Framework_TestCase
2122
{
@@ -31,6 +32,9 @@ class HttpCacheTestCase extends \PHPUnit_Framework_TestCase
3132

3233
protected function setUp()
3334
{
35+
if (class_exists('Symfony\Bridge\PhpUnit\ClockMock')) {
36+
ClockMock::register('Symfony\Component\HttpFoundation\Request');
37+
}
3438
$this->kernel = null;
3539

3640
$this->cache = null;

0 commit comments

Comments
 (0)