Skip to content

Commit b4c9de8

Browse files
committed
CachedClient: added option to forbid Github rechecking [Closes #6]
When enabled and response exists in cache, it is returned as is without rechecking Github for actual data. So there is no HTTP request and HTTP response. This is handy during development.
1 parent 3ef562b commit b4c9de8

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/Github/Http/CachedClient.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@ class CachedClient extends Github\Sanity implements IClient
1919
/** @var IClient */
2020
private $client;
2121

22+
/** @var bool */
23+
private $forbidRecheck;
24+
2225
/** @var callable|NULL */
2326
private $onResponse;
2427

2528

26-
public function __construct(Storages\ICache $cache, IClient $client = NULL)
29+
/**
30+
* @param Storages\ICache
31+
* @param IClient
32+
* @param bool forbid checking Github for new data; more or less development purpose only
33+
*/
34+
public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE)
2735
{
2836
$this->cache = $cache;
2937
$this->client = $client ?: Github\Helpers::createDefaultClient();
38+
$this->forbidRecheck = (bool) $forbidRecheck;
3039
}
3140

3241

@@ -59,6 +68,12 @@ public function request(Request $request)
5968
]);
6069

6170
if ($cached = $this->cache->load($cacheKey)) {
71+
if ($this->forbidRecheck) {
72+
$cached = clone $cached;
73+
$this->onResponse && call_user_func($this->onResponse, $cached);
74+
return $cached;
75+
}
76+
6277
/** @var $cached Response */
6378
if ($cached->hasHeader('ETag')) {
6479
$request->addHeader('If-None-Match', $cached->getHeader('ETag'));

tests/Github/Http/CachedClient.phpt

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CachingTestCase extends Tester\TestCase
4747
{
4848
$cache = new MockCache;
4949
$this->innerClient = new MockClient;
50-
$this->client = new \Milo\Github\Http\CachedClient($cache, $this->innerClient);
50+
$this->client = new Milo\Github\Http\CachedClient($cache, $this->innerClient);
5151
}
5252

5353

@@ -190,6 +190,70 @@ class CachingTestCase extends Tester\TestCase
190190
Assert::same('inner-304', $response->getPrevious()->getContent());
191191
}
192192

193+
194+
public function testForbidRecheckDisabled()
195+
{
196+
$client = new Milo\Github\Http\CachedClient(new MockCache, $this->innerClient);
197+
198+
$count = 0;
199+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) use (& $count) {
200+
$count++;
201+
return $request->hasHeader('If-None-Match')
202+
? new Milo\Github\Http\Response(304, [], 'inner-304')
203+
: new Milo\Github\Http\Response(200, ['ETag' => '"test"'], 'inner-200');
204+
};
205+
206+
$request = new Milo\Github\Http\Request('', '');
207+
208+
$response = $client->request($request);
209+
Assert::same(1, $count);
210+
Assert::same('inner-200', $response->getContent());
211+
Assert::null($response->getPrevious());
212+
213+
$response = $client->request($request);
214+
Assert::same(2, $count);
215+
Assert::same('inner-200', $response->getContent());
216+
Assert::type('Milo\Github\Http\Response', $response->getPrevious());
217+
Assert::same('inner-304', $response->getPrevious()->getContent());
218+
219+
$response = $client->request($request);
220+
Assert::same(3, $count);
221+
Assert::same('inner-200', $response->getContent());
222+
Assert::type('Milo\Github\Http\Response', $response->getPrevious());
223+
Assert::same('inner-304', $response->getPrevious()->getContent());
224+
}
225+
226+
227+
public function testForbidRecheckEnabled()
228+
{
229+
$client = new Milo\Github\Http\CachedClient(new MockCache, $this->innerClient, TRUE);
230+
231+
$count = 0;
232+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) use (& $count) {
233+
$count++;
234+
return $request->hasHeader('If-None-Match')
235+
? new Milo\Github\Http\Response(304, [], 'inner-304')
236+
: new Milo\Github\Http\Response(200, ['ETag' => '"test"'], 'inner-200');
237+
};
238+
239+
$request = new Milo\Github\Http\Request('', '');
240+
241+
$response = $client->request($request);
242+
Assert::same(1, $count);
243+
Assert::same('inner-200', $response->getContent());
244+
Assert::null($response->getPrevious());
245+
246+
$response = $client->request($request);
247+
Assert::same(1, $count);
248+
Assert::same('inner-200', $response->getContent());
249+
Assert::null($response->getPrevious());
250+
251+
$response = $client->request($request);
252+
Assert::same(1, $count);
253+
Assert::same('inner-200', $response->getContent());
254+
Assert::null($response->getPrevious());
255+
}
256+
193257
}
194258

195259
(new CachingTestCase)->run();

0 commit comments

Comments
 (0)