Skip to content

Commit d949fcc

Browse files
committed
minor symfony#16221 [Http*] Mock time() to fix transient tests (nicolas-grekas)
This PR was merged into the 2.3 branch. Discussion ---------- [Http*] Mock time() to fix transient tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#15617 | License | MIT | Doc PR | - Commits ------- fa604d3 [Http*] Mock time() to fix transient tests
2 parents 8ced3c6 + fa604d3 commit d949fcc

File tree

9 files changed

+38
-31
lines changed

9 files changed

+38
-31
lines changed

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public function testSubmitDifferentPattern()
262262
$this->assertDateTimeEquals($dateTime, $form->getData());
263263
}
264264

265-
// Bug fix
266265
public function testInitializeWithDateTime()
267266
{
268267
// Throws an exception if "data_class" option is not explicitly set

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ public function testPassWidgetToView()
694694
$this->assertSame('single_text', $view->vars['widget']);
695695
}
696696

697-
// Bug fix
698697
public function testInitializeWithDateTime()
699698
{
700699
// Throws an exception if "data_class" option is not explicitly set

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ public function testIsPartiallyFilledReturnsTrueIfChoiceAndSecondsEmpty()
467467
$this->assertTrue($form->isPartiallyFilled());
468468
}
469469

470-
// Bug fix
471470
public function testInitializeWithDateTime()
472471
{
473472
// Throws an exception if "data_class" option is not explicitly set

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function __construct($content = '', $status = 200, $headers = array())
138138
$this->setStatusCode($status);
139139
$this->setProtocolVersion('1.0');
140140
if (!$this->headers->has('Date')) {
141-
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
141+
$this->setDate(\DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC')));
142142
}
143143
}
144144

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testGetExpiresTimeWithStringValue()
9595
$cookie = new Cookie('foo', 'bar', $value);
9696
$expire = strtotime($value);
9797

98-
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
98+
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
9999
}
100100

101101
public function testGetDomain()

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
1616

17+
/**
18+
* @group time-sensitive
19+
*/
1720
class ResponseTest extends ResponseTestCase
1821
{
1922
public function testCreate()
@@ -246,16 +249,18 @@ public function testGetDate()
246249
{
247250
$oneHourAgo = $this->createDateTimeOneHourAgo();
248251
$response = new Response('', 200, array('Date' => $oneHourAgo->format(DATE_RFC2822)));
249-
$this->assertEquals(0, $oneHourAgo->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present');
252+
$date = $response->getDate();
253+
$this->assertEquals($oneHourAgo->getTimestamp(), $date->getTimestamp(), '->getDate() returns the Date header if present');
250254

251255
$response = new Response();
252256
$date = $response->getDate();
253-
$this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present');
257+
$this->assertEquals(time(), $date->getTimestamp(), '->getDate() returns the current Date if no Date header present');
254258

255259
$response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
256260
$now = $this->createDateTimeNow();
257261
$response->headers->set('Date', $now->format(DATE_RFC2822));
258-
$this->assertLessThanOrEqual(1, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified');
262+
$date = $response->getDate();
263+
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
259264

260265
$response = new Response('', 200);
261266
$response->headers->remove('Date');
@@ -275,7 +280,7 @@ public function testGetMaxAge()
275280
$response = new Response();
276281
$response->headers->set('Cache-Control', 'must-revalidate');
277282
$response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
278-
$this->assertLessThanOrEqual(1, $response->getMaxAge() - 3600, '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
283+
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
279284

280285
$response = new Response();
281286
$response->headers->set('Cache-Control', 'must-revalidate');
@@ -346,7 +351,7 @@ public function testGetTtl()
346351

347352
$response = new Response();
348353
$response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
349-
$this->assertLessThanOrEqual(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
354+
$this->assertEquals(3600, $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
350355

351356
$response = new Response();
352357
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
@@ -359,7 +364,7 @@ public function testGetTtl()
359364

360365
$response = new Response();
361366
$response->headers->set('Cache-Control', 'max-age=60');
362-
$this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
367+
$this->assertEquals(60, $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
363368
}
364369

365370
public function testSetClientTtl()
@@ -524,7 +529,7 @@ public function testSetCache()
524529
$response->setCache($options);
525530
$this->assertEquals($response->getEtag(), '"whatever"');
526531

527-
$now = new \DateTime();
532+
$now = $this->createDateTimeNow();
528533
$options = array('last_modified' => $now);
529534
$response->setCache($options);
530535
$this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
@@ -583,7 +588,7 @@ public function testSetExpires()
583588

584589
$this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
585590

586-
$now = new \DateTime();
591+
$now = $this->createDateTimeNow();
587592
$response->setExpires($now);
588593

589594
$this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
@@ -592,7 +597,7 @@ public function testSetExpires()
592597
public function testSetLastModified()
593598
{
594599
$response = new Response();
595-
$response->setLastModified(new \DateTime());
600+
$response->setLastModified($this->createDateTimeNow());
596601
$this->assertNotNull($response->getLastModified());
597602

598603
$response->setLastModified(null);
@@ -777,7 +782,7 @@ public function testSettersAreChainable()
777782
'setCharset' => 'UTF-8',
778783
'setPublic' => null,
779784
'setPrivate' => null,
780-
'setDate' => new \DateTime(),
785+
'setDate' => $this->createDateTimeNow(),
781786
'expire' => null,
782787
'setMaxAge' => 1,
783788
'setSharedMaxAge' => 1,
@@ -810,21 +815,19 @@ public function invalidContentProvider()
810815

811816
protected function createDateTimeOneHourAgo()
812817
{
813-
$date = new \DateTime();
814-
815-
return $date->sub(new \DateInterval('PT1H'));
818+
return $this->createDateTimeNow()->sub(new \DateInterval('PT1H'));
816819
}
817820

818821
protected function createDateTimeOneHourLater()
819822
{
820-
$date = new \DateTime();
821-
822-
return $date->add(new \DateInterval('PT1H'));
823+
return $this->createDateTimeNow()->add(new \DateInterval('PT1H'));
823824
}
824825

825826
protected function createDateTimeNow()
826827
{
827-
return new \DateTime();
828+
$date = new \DateTime();
829+
830+
return $date->setTimestamp(time());
828831
}
829832

830833
protected function provideResponse()

src/Symfony/Component/HttpKernel/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());

src/Symfony/Component/HttpKernel/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(

src/Symfony/Component/HttpKernel/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)