Skip to content

Commit df1b3ef

Browse files
committed
Bugfix and started on the tests
1 parent b275498 commit df1b3ef

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('e3b717d5883a45ef9493d009741f7c64')->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
@@ -102,8 +102,9 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
102102

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

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

121-
$expiresAt = time() + $this->getMaxAge($response);
122+
$maxAge = $this->getMaxAge($response);
122123
$cacheItem
123-
->expiresAfter($this->config['cache_lifetime'] + $expiresAt)
124+
->expiresAfter($this->config['cache_lifetime'] + $maxAge)
124125
->set([
125126
'response' => $response,
126127
'body' => $body,
127-
'expiresAt' => $expiresAt,
128+
'expiresAt' => time() + $maxAge,
128129
'createdAt' => time(),
129130
'etag' => $response->getHeader('ETag'),
130131
]);

0 commit comments

Comments
 (0)