Skip to content

Commit

Permalink
Merge pull request #114 from FriendsOfSymfony/fix-112
Browse files Browse the repository at this point in the history
Throw exception if cache hit/miss header is missing (fix #112)
  • Loading branch information
dbu committed Aug 6, 2014
2 parents 6841917 + 9019dad commit 4d6ec3b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/Test/PHPUnit/AbstractCacheConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ abstract public function getValue();
*/
protected function matches($other)
{
if (!$other->hasHeader($this->header)) {
throw new \RuntimeException(
sprintf(
'Response has no "%s" header. Configure your caching proxy '
. 'to set the header with cache hit/miss status.',
$this->header
)
);
}

return $this->getValue() === (string) $other->getHeader($this->header);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace FOS\HttpCache\Tests\Unit\Test\PHPUnit;

abstract class AbstractCacheConstraintTest extends \PHPUnit_Framework_TestCase
{
protected function getResponseMock()
{
return \Mockery::mock(
'\Guzzle\Http\Message\Response[hasHeader,getHeader]',
array(null)
);
}
}
23 changes: 18 additions & 5 deletions tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;

class IsCacheHitConstraintTest extends \PHPUnit_Framework_TestCase
class IsCacheHitConstraintTest extends AbstractCacheConstraintTest
{
/**
* @var IsCacheHitConstraint
Expand All @@ -30,10 +30,23 @@ public function setUp()
*/
public function testMatches()
{
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
->shouldReceive('getHeader')
->once()
->andReturn('MISS')
$response = $this->getResponseMock()
->shouldReceive('hasHeader')->with('cache-header')->andReturn(true)
->shouldReceive('getHeader')->with('cache-header')->once()->andReturn('MISS')
->getMock();

$this->constraint->evaluate($response);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Response has no "cache-header" header
*/
public function testMatchesThrowsExceptionIfHeaderIsMissing()
{
$response = $this->getResponseMock()
->shouldReceive('hasHeader')->with('cache-header')->once()
->andReturn(false)
->getMock();

$this->constraint->evaluate($response);
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;

class IsCacheMissConstraintTest extends \PHPUnit_Framework_TestCase
class IsCacheMissConstraintTest extends AbstractCacheConstraintTest
{
/**
* @var IsCacheMissConstraint
Expand All @@ -30,9 +30,9 @@ public function setUp()
*/
public function testMatches()
{
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
->shouldReceive('getHeader')
->once()
$response = $this->getResponseMock()
->shouldReceive('hasHeader')->with('cache-header')->andReturn(true)
->shouldReceive('getHeader')->with('cache-header')->once()
->andReturn('HIT')
->getMock();

Expand Down

0 comments on commit 4d6ec3b

Please sign in to comment.