[Http*] Mock time() to fix transient tests

This commit is contained in:
Nicolas Grekas 2015-10-13 09:59:06 +02:00
parent 8ced3c63f3
commit fa604d3c6f
9 changed files with 38 additions and 31 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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')));
}
}

View File

@ -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()

View File

@ -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()

View File

@ -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());

View File

@ -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(

View File

@ -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;