diff --git a/src/Test/PHPUnit/AbstractCacheConstraint.php b/src/Test/PHPUnit/AbstractCacheConstraint.php index 6df827d4..61d027d4 100644 --- a/src/Test/PHPUnit/AbstractCacheConstraint.php +++ b/src/Test/PHPUnit/AbstractCacheConstraint.php @@ -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); } diff --git a/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php b/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php new file mode 100644 index 00000000..5dc5290b --- /dev/null +++ b/tests/Unit/Test/PHPUnit/AbstractCacheConstraintTest.php @@ -0,0 +1,14 @@ +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); diff --git a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php index 4f3e7fab..48b98756 100644 --- a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php @@ -13,7 +13,7 @@ use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint; -class IsCacheMissConstraintTest extends \PHPUnit_Framework_TestCase +class IsCacheMissConstraintTest extends AbstractCacheConstraintTest { /** * @var IsCacheMissConstraint @@ -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();