public function setUp()
{
// Create default HandlerStack
$stack = HandlerStack::create(function (RequestInterface $request, array $options) {
switch ($request->getUri()->getPath()) {
case '/etag':
if ($request->getHeaderLine('If-None-Match') == 'MyBeautifulHash') {
return new FulfilledPromise(new Response(304));
}
return new FulfilledPromise((new Response())->withHeader('Etag', 'MyBeautifulHash'));
case '/etag-changed':
if ($request->getHeaderLine('If-None-Match') == 'MyBeautifulHash') {
return new FulfilledPromise((new Response())->withHeader('Etag', 'MyBeautifulHash2'));
}
return new FulfilledPromise((new Response())->withHeader('Etag', 'MyBeautifulHash'));
case '/stale-while-revalidate':
if ($request->getHeaderLine('If-None-Match') == 'MyBeautifulHash') {
return new FulfilledPromise((new Response(304))->withHeader('Cache-Control', 'max-age=10'));
}
return new FulfilledPromise((new Response())->withHeader('Etag', 'MyBeautifulHash')->withHeader('Cache-Control', 'max-age=1')->withAddedHeader('Cache-Control', 'stale-while-revalidate=60'));
}
throw new \InvalidArgumentException();
});
$this->cache = new CacheMiddleware();
// Add this middleware to the top with `push`
$stack->push($this->cache, 'cache');
// Initialize the client with the handler option
$this->client = new Client(['handler' => $stack]);
$this->cache->setClient($this->client);
}