-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also put entries into the cache that allow stale-while-revalidate (#125)
* Also put entries into the cache that allow stale-while-revalidate * Rename phpunit.xml to phpunit.xml.dist * Add require-dev dependency on symfony/phpunit-bridge to allow for clock mocking See https://symfony.com/doc/current/components/phpunit_bridge.html#time-sensitive-tests. This helps to get reliable results when time computations are under test. * Explain in the DocBlock what exactly the computed TTL means * Add first tests for CacheEntry * Improve the docblock explanation * Allow symfony/phpunit-bridge 4.4 as well ... so we get compatibility even with PHPUnit 4. * Ensure BC with PHPUnit 4 * Avoid failing tests due to deprecation notices
- Loading branch information
Showing
4 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Kevinrob\GuzzleCache\Tests; | ||
|
||
use Kevinrob\GuzzleCache\CacheEntry; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @group time-sensitive | ||
*/ | ||
class CacheEntryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var ResponseInterface | ||
*/ | ||
private $response; | ||
|
||
private $responseHeaders = []; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->request = $this->getMockBuilder(RequestInterface::class)->getMock(); | ||
$this->response = $this->getMockBuilder(ResponseInterface::class)->getMock(); | ||
$this->response->method('getHeader')->will($this->returnCallback(function ($header) { | ||
if (isset($this->responseHeaders[$header])) { | ||
return $this->responseHeaders[$header]; | ||
} | ||
|
||
return []; | ||
})); | ||
$this->response->method('hasHeader')->will($this->returnCallback(function ($header) { | ||
return isset($this->responseHeaders[$header]); | ||
})); | ||
} | ||
|
||
public function testTtlForValidateableResponseShouldBeInfinite() | ||
{ | ||
$this->setResponseHeader('Etag', 'some-etag'); | ||
$cacheEntry = new CacheEntry($this->request, $this->response, $this->makeDateTimeOffset()); | ||
|
||
// getTTL() will return 0 to indicate "infinite" | ||
$this->assertEquals(0, $cacheEntry->getTTL()); | ||
} | ||
|
||
public function testTtlForSimpleExpiration() | ||
{ | ||
$cacheEntry = new CacheEntry($this->request, $this->response, $this->makeDateTimeOffset(10)); | ||
|
||
$this->assertEquals(10, $cacheEntry->getTTL()); | ||
} | ||
|
||
public function testTtlConsidersStaleIfError() | ||
{ | ||
$this->setResponseHeader('Cache-Control', 'stale-if-error=30'); | ||
$cacheEntry = new CacheEntry($this->request, $this->response, $this->makeDateTimeOffset(10)); | ||
|
||
$this->assertEquals(40, $cacheEntry->getTTL()); | ||
} | ||
|
||
public function testTtlConsidersStaleWhileRevalidate() | ||
{ | ||
$this->setResponseHeader('Cache-Control', 'stale-while-revalidate=30'); | ||
$cacheEntry = new CacheEntry($this->request, $this->response, $this->makeDateTimeOffset(10)); | ||
|
||
$this->assertEquals(40, $cacheEntry->getTTL()); | ||
} | ||
|
||
public function testTtlUsesMaximumPossibleLifetime() | ||
{ | ||
$this->setResponseHeader('Cache-Control', 'stale-while-revalidate=30, stale-if-error=60'); | ||
$cacheEntry = new CacheEntry($this->request, $this->response, $this->makeDateTimeOffset(10)); | ||
|
||
$this->assertEquals(70, $cacheEntry->getTTL()); | ||
} | ||
|
||
private function setResponseHeader($name, $value) | ||
{ | ||
$this->responseHeaders[$name] = [$value]; | ||
} | ||
|
||
private function makeDateTimeOffset($offset = 0) | ||
{ | ||
return \DateTime::createFromFormat('U', time() + $offset); | ||
} | ||
} |