diff --git a/tests/Strategy/GreedyCacheStrategyTest.php b/tests/Strategy/GreedyCacheStrategyTest.php new file mode 100644 index 00000000..77b46472 --- /dev/null +++ b/tests/Strategy/GreedyCacheStrategyTest.php @@ -0,0 +1,74 @@ +push(new CacheMiddleware( + new GreedyCacheStrategy(new VolatileRuntimeStorage(), 2) + ), 'cache'); + + // Initialize the client with the handler option + $this->client = new Client(['handler' => $stack]); + } + + public function testDefaultTtl() + { + $this->client->get('http://test.com/'); + + $response = $this->client->get('http://test.com/'); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_HIT, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + + sleep(3); + + $response = $this->client->get('http://test.com/'); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + } + + public function testHeaderTtl() + { + /** @var Request $request */ + $request = (new Request('GET', 'http://test.com/2')) + ->withHeader(GreedyCacheStrategy::HEADER_TTL, -2); + $this->client->send($request); + + $response = $this->client->send($request); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + + /** @var Request $request */ + $request = (new Request('GET', 'http://test.com/2')) + ->withHeader(GreedyCacheStrategy::HEADER_TTL, 5); + + $this->client->send($request); + + $response = $this->client->send($request); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_HIT, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + + sleep(6); + + $response = $this->client->send($request); + $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); + } +}