Skip to content

Commit 39c0cfe

Browse files
committed
Bugfix and started on the tests
1 parent 3f36fb5 commit 39c0cfe

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

spec/CachePluginSpec.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CachePluginSpec extends ObjectBehavior
1515
{
1616
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
1717
{
18-
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60]);
18+
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60, 'cache_lifetime'=>1000]);
1919
}
2020

2121
function it_is_initializable(CacheItemPoolInterface $pool)
@@ -41,18 +41,25 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
4141
$response->getBody()->willReturn($stream);
4242
$response->getHeader('Cache-Control')->willReturn(array());
4343
$response->getHeader('Expires')->willReturn(array());
44+
$response->getHeader('ETag')->willReturn(array());
4445

4546
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
4647
$item->isHit()->willReturn(false);
47-
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
48-
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
48+
$item->set()->willReturn($item)->shouldBeCalled();
49+
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();
4950
$pool->save($item)->shouldBeCalled();
5051

5152
$next = function (RequestInterface $request) use ($response) {
5253
return new FulfilledPromise($response->getWrappedObject());
5354
};
5455

5556
$this->handleRequest($request, $next, function () {});
57+
$item->get()->shouldHaveKeyWithValue('response', $response);
58+
$item->get()->shouldHaveKeyWithValue('body', $httpBody);
59+
$item->get()->shouldHaveKey('expiresAt');
60+
$item->get()->shouldHaveKey('createdAt');
61+
$item->get()->shouldHaveKey('etag');
62+
5663
}
5764

5865
function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)

src/CachePlugin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
103103

104104
// The cached response we have is still valid
105105
$data = $cacheItem->get();
106-
$data['expiresAt'] = time() + $this->getMaxAge($response);
107-
$cacheItem->set($data)->expiresAfter($this->config['cache_lifetime'] + $data['expiresAt']);
106+
$maxAge = $this->getMaxAge($response);
107+
$data['expiresAt'] = time() + $maxAge;
108+
$cacheItem->set($data)->expiresAfter($this->config['cache_lifetime'] + $maxAge);
108109
$this->pool->save($cacheItem);
109110

110111
return $this->createResponseFromCacheItem($cacheItem);
@@ -119,13 +120,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
119120
$response = $response->withBody($this->streamFactory->createStream($body));
120121
}
121122

122-
$expiresAt = time() + $this->getMaxAge($response);
123+
$maxAge = $this->getMaxAge($response);
123124
$cacheItem
124-
->expiresAfter($this->config['cache_lifetime'] + $expiresAt)
125+
->expiresAfter($this->config['cache_lifetime'] + $maxAge)
125126
->set([
126127
'response' => $response,
127128
'body' => $body,
128-
'expiresAt' => $expiresAt,
129+
'expiresAt' => time() + $maxAge,
129130
'createdAt' => time(),
130131
'etag' => $response->getHeader('ETag'),
131132
]);

0 commit comments

Comments
 (0)