Skip to content

Commit 4d6ec3b

Browse files
committed
Merge pull request #114 from FriendsOfSymfony/fix-112
Throw exception if cache hit/miss header is missing (fix #112)
2 parents 6841917 + 9019dad commit 4d6ec3b

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/Test/PHPUnit/AbstractCacheConstraint.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ abstract public function getValue();
4444
*/
4545
protected function matches($other)
4646
{
47+
if (!$other->hasHeader($this->header)) {
48+
throw new \RuntimeException(
49+
sprintf(
50+
'Response has no "%s" header. Configure your caching proxy '
51+
. 'to set the header with cache hit/miss status.',
52+
$this->header
53+
)
54+
);
55+
}
56+
4757
return $this->getValue() === (string) $other->getHeader($this->header);
4858
}
4959

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\Tests\Unit\Test\PHPUnit;
4+
5+
abstract class AbstractCacheConstraintTest extends \PHPUnit_Framework_TestCase
6+
{
7+
protected function getResponseMock()
8+
{
9+
return \Mockery::mock(
10+
'\Guzzle\Http\Message\Response[hasHeader,getHeader]',
11+
array(null)
12+
);
13+
}
14+
}

tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;
1515

16-
class IsCacheHitConstraintTest extends \PHPUnit_Framework_TestCase
16+
class IsCacheHitConstraintTest extends AbstractCacheConstraintTest
1717
{
1818
/**
1919
* @var IsCacheHitConstraint
@@ -30,10 +30,23 @@ public function setUp()
3030
*/
3131
public function testMatches()
3232
{
33-
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
34-
->shouldReceive('getHeader')
35-
->once()
36-
->andReturn('MISS')
33+
$response = $this->getResponseMock()
34+
->shouldReceive('hasHeader')->with('cache-header')->andReturn(true)
35+
->shouldReceive('getHeader')->with('cache-header')->once()->andReturn('MISS')
36+
->getMock();
37+
38+
$this->constraint->evaluate($response);
39+
}
40+
41+
/**
42+
* @expectedException \RuntimeException
43+
* @expectedExceptionMessage Response has no "cache-header" header
44+
*/
45+
public function testMatchesThrowsExceptionIfHeaderIsMissing()
46+
{
47+
$response = $this->getResponseMock()
48+
->shouldReceive('hasHeader')->with('cache-header')->once()
49+
->andReturn(false)
3750
->getMock();
3851

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

tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;
1515

16-
class IsCacheMissConstraintTest extends \PHPUnit_Framework_TestCase
16+
class IsCacheMissConstraintTest extends AbstractCacheConstraintTest
1717
{
1818
/**
1919
* @var IsCacheMissConstraint
@@ -30,9 +30,9 @@ public function setUp()
3030
*/
3131
public function testMatches()
3232
{
33-
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
34-
->shouldReceive('getHeader')
35-
->once()
33+
$response = $this->getResponseMock()
34+
->shouldReceive('hasHeader')->with('cache-header')->andReturn(true)
35+
->shouldReceive('getHeader')->with('cache-header')->once()
3636
->andReturn('HIT')
3737
->getMock();
3838

0 commit comments

Comments
 (0)