Skip to content

Commit 31837ab

Browse files
committed
Tests are passing
1 parent 6bf63c2 commit 31837ab

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
"Http\\Client\\Common\\Plugin\\": "src/"
2727
}
2828
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"spec\\Http\\Client\\Common\\Plugin\\": "spec/"
32+
}
33+
},
2934
"scripts": {
3035
"test": "vendor/bin/phpspec run",
3136
"test-ci": "vendor/bin/phpspec run -c phpspec.yml.ci"

spec/CachePluginSpec.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace spec\Http\Client\Common\Plugin;
44

5+
use Prophecy\Argument;
6+
use Prophecy\Comparator\Factory as ComparatorFactory;
57
use Http\Message\StreamFactory;
68
use Http\Promise\FulfilledPromise;
79
use PhpSpec\ObjectBehavior;
@@ -39,27 +41,28 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
3941
$request->getUri()->willReturn('/');
4042
$response->getStatusCode()->willReturn(200);
4143
$response->getBody()->willReturn($stream);
42-
$response->getHeader('Cache-Control')->willReturn(array());
43-
$response->getHeader('Expires')->willReturn(array());
44-
$response->getHeader('ETag')->willReturn(array());
44+
$response->getHeader('Cache-Control')->willReturn(array())->shouldBeCalled();
45+
$response->getHeader('Expires')->willReturn(array())->shouldBeCalled();
46+
$response->getHeader('ETag')->willReturn(array())->shouldBeCalled();
4547

4648
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
4749
$item->isHit()->willReturn(false);
48-
$item->set()->willReturn($item)->shouldBeCalled();
4950
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();
50-
$pool->save($item)->shouldBeCalled();
51+
52+
$item->set(Argument::that($this->getCacheItemMatcher([
53+
'response' => $response->getWrappedObject(),
54+
'body' => $httpBody,
55+
'expiresAt' => 0,
56+
'createdAt' => 0,
57+
'etag' => []
58+
])))->willReturn($item)->shouldBeCalled();
59+
$pool->save(Argument::any())->shouldBeCalled();
5160

5261
$next = function (RequestInterface $request) use ($response) {
5362
return new FulfilledPromise($response->getWrappedObject());
5463
};
5564

5665
$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-
6366
}
6467

6568
function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
@@ -107,13 +110,20 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
107110
$response->getHeader('Cache-Control')->willReturn(array('max-age=40'));
108111
$response->getHeader('Age')->willReturn(array('15'));
109112
$response->getHeader('Expires')->willReturn(array());
113+
$response->getHeader('ETag')->willReturn(array());
110114

111115
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
112116
$item->isHit()->willReturn(false);
113117

114-
// 40-15 should be 25
115-
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
116-
$item->expiresAfter(25)->willReturn($item)->shouldBeCalled();
118+
$item->set(Argument::that($this->getCacheItemMatcher([
119+
'response' => $response->getWrappedObject(),
120+
'body' => $httpBody,
121+
'expiresAt' => 0,
122+
'createdAt' => 0,
123+
'etag' => []
124+
])))->willReturn($item)->shouldBeCalled();
125+
// 40-15 should be 25 + the default 1000
126+
$item->expiresAfter(1025)->willReturn($item)->shouldBeCalled();
117127
$pool->save($item)->shouldBeCalled();
118128

119129
$next = function (RequestInterface $request) use ($response) {
@@ -122,4 +132,25 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
122132

123133
$this->handleRequest($request, $next, function () {});
124134
}
135+
136+
private function getCacheItemMatcher(array $expectedData)
137+
{
138+
return function(array $actualData) use ($expectedData) {
139+
foreach ($expectedData as $key => $value) {
140+
if (!isset($actualData[$key])) {
141+
return false;
142+
}
143+
144+
if ($key === 'expiresAt' || $key === 'createdAt') {
145+
// We do not need to validate the value of these fields.
146+
continue;
147+
}
148+
149+
if ($actualData[$key] !== $value) {
150+
return false;
151+
}
152+
}
153+
return true;
154+
};
155+
}
125156
}

0 commit comments

Comments
 (0)