Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception if cache hit/miss header is missing (fix #112) #114

Merged
merged 1 commit into from
Aug 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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