[HttpFoundation] fix return types and handling of zero in Response

This commit is contained in:
Tobias Schultze 2013-01-02 22:06:26 +01:00
parent 75952af0c3
commit 10b01c9159
2 changed files with 15 additions and 10 deletions

View File

@ -614,8 +614,8 @@ class Response
*/ */
public function getAge() public function getAge()
{ {
if ($age = $this->headers->get('Age')) { if (null !== $age = $this->headers->get('Age')) {
return $age; return (int) $age;
} }
return max(time() - $this->getDate()->format('U'), 0); return max(time() - $this->getDate()->format('U'), 0);
@ -691,12 +691,12 @@ class Response
*/ */
public function getMaxAge() public function getMaxAge()
{ {
if ($age = $this->headers->getCacheControlDirective('s-maxage')) { if ($this->headers->hasCacheControlDirective('s-maxage')) {
return $age; return (int) $this->headers->getCacheControlDirective('s-maxage');
} }
if ($age = $this->headers->getCacheControlDirective('max-age')) { if ($this->headers->hasCacheControlDirective('max-age')) {
return $age; return (int) $this->headers->getCacheControlDirective('max-age');
} }
if (null !== $this->getExpires()) { if (null !== $this->getExpires()) {
@ -757,7 +757,7 @@ class Response
*/ */
public function getTtl() public function getTtl()
{ {
if ($maxAge = $this->getMaxAge()) { if (null !== $maxAge = $this->getMaxAge()) {
return $maxAge - $this->getAge(); return $maxAge - $this->getAge();
} }
@ -961,7 +961,7 @@ class Response
*/ */
public function hasVary() public function hasVary()
{ {
return (Boolean) $this->headers->get('Vary'); return null !== $this->headers->get('Vary');
} }
/** /**

View File

@ -223,7 +223,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
$response = new Response(); $response = new Response();
$response->headers->set('Expires', -1); $response->headers->set('Expires', -1);
$response->expire(); $response->expire();
$this->assertEquals(0, $response->headers->get('Age'), '->expire() does not set the Age to 0'); $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
} }
public function testGetTtl() public function testGetTtl()
@ -237,7 +237,12 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
$response = new Response(); $response = new Response();
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)); $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part'); $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
$response = new Response();
$response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
$response->headers->set('Age', 0);
$this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
$response = new Response(); $response = new Response();
$response->headers->set('Cache-Control', 'max-age=60'); $response->headers->set('Cache-Control', 'max-age=60');