Skip to content

Commit e085db4

Browse files
committed
CachedClient: delegate onRequest() only, apply onResponse() by self
1 parent 5779ee0 commit e085db4

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Github/Http/CachedClient.php

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

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

2326
public function __construct(Storages\ICache $cache, IClient $client = NULL)
2427
{
@@ -75,6 +78,8 @@ public function request(Request $request)
7578
$response = $cached->setPrevious($response);
7679
}
7780

81+
$this->onResponse && call_user_func($this->onResponse, $response);
82+
7883
return $response;
7984
}
8085

@@ -96,7 +101,8 @@ public function onRequest($callback)
96101
*/
97102
public function onResponse($callback)
98103
{
99-
$this->client->onResponse($callback);
104+
$this->client->onResponse(NULL);
105+
$this->onResponse = $callback;
100106
return $this;
101107
}
102108

tests/Github/Http/CachedClient.phpt

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class MockClient implements Milo\Github\Http\IClient
2020
return call_user_func($this->onRequest, $request);
2121
}
2222

23-
public function onRequest($foo) { trigger_error("Inner onRequest called: $foo", E_USER_NOTICE); }
24-
public function onResponse($foo) { trigger_error("Inner onResponse called: $foo", E_USER_NOTICE); }
23+
public function onRequest($foo) { trigger_error('Inner onRequest called: ' . var_export($foo, TRUE), E_USER_NOTICE); }
24+
public function onResponse($foo) { trigger_error('Inner onResponse called: ' . var_export($foo, TRUE), E_USER_NOTICE); }
2525
}
2626

2727

@@ -40,34 +40,46 @@ class CachingTestCase extends Tester\TestCase
4040
private $client;
4141

4242
/** @var MockClient */
43-
private $mockClient;
43+
private $innerClient;
4444

4545

4646
public function setup()
4747
{
4848
$cache = new MockCache;
49-
$this->mockClient = new MockClient;
50-
$this->client = new \Milo\Github\Http\CachedClient($cache, $this->mockClient);
49+
$this->innerClient = new MockClient;
50+
$this->client = new \Milo\Github\Http\CachedClient($cache, $this->innerClient);
5151
}
5252

5353

5454
public function testBasics()
5555
{
56-
Assert::same($this->mockClient, $this->client->getInnerClient());
56+
Assert::same($this->innerClient, $this->client->getInnerClient());
5757

5858
Assert::error(function() {
5959
Assert::same($this->client, $this->client->onRequest('callback-1'));
6060
Assert::same($this->client, $this->client->onResponse('callback-2'));
6161
}, [
62-
[E_USER_NOTICE, 'Inner onRequest called: callback-1'],
63-
[E_USER_NOTICE, 'Inner onResponse called: callback-2'],
62+
[E_USER_NOTICE, "Inner onRequest called: 'callback-1'"],
63+
[E_USER_NOTICE, 'Inner onResponse called: NULL'],
6464
]);
65+
66+
$onResponseCalled = FALSE;
67+
$this->innerClient->onRequest = function () {
68+
return new Milo\Github\Http\Response(200, [], '');
69+
};
70+
Assert::error(function() use (& $onResponseCalled) {
71+
$this->client->onResponse(function() use (& $onResponseCalled) { $onResponseCalled = TRUE; });
72+
}, E_USER_NOTICE, 'Inner onResponse called: NULL');
73+
$this->client->request(
74+
new Milo\Github\Http\Request('', '')
75+
);
76+
Assert::true($onResponseCalled);
6577
}
6678

6779

6880
public function testNoCaching()
6981
{
70-
$this->mockClient->onRequest = function (Milo\Github\Http\Request $request) {
82+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) {
7183
Assert::false($request->hasHeader('ETag'));
7284
Assert::false($request->hasHeader('If-Modified-Since'));
7385

@@ -88,7 +100,7 @@ class CachingTestCase extends Tester\TestCase
88100

89101
public function testETagCaching()
90102
{
91-
$this->mockClient->onRequest = function (Milo\Github\Http\Request $request) {
103+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) {
92104
Assert::false($request->hasHeader('If-None-Match'));
93105
Assert::false($request->hasHeader('If-Modified-Since'));
94106

@@ -100,7 +112,7 @@ class CachingTestCase extends Tester\TestCase
100112
);
101113
Assert::same('response-1', $response->getContent());
102114

103-
$this->mockClient->onRequest = function (Milo\Github\Http\Request $request) {
115+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) {
104116
Assert::same('e-tag', $request->getHeader('If-None-Match'));
105117
Assert::false($request->hasHeader('If-Modified-Since'));
106118

@@ -119,7 +131,7 @@ class CachingTestCase extends Tester\TestCase
119131

120132
public function testIfModifiedCaching()
121133
{
122-
$this->mockClient->onRequest = function (Milo\Github\Http\Request $request) {
134+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) {
123135
Assert::false($request->hasHeader('If-None-Match'));
124136
Assert::false($request->hasHeader('If-Modified-Since'));
125137

@@ -131,7 +143,7 @@ class CachingTestCase extends Tester\TestCase
131143
);
132144
Assert::same('response-1', $response->getContent());
133145

134-
$this->mockClient->onRequest = function (Milo\Github\Http\Request $request) {
146+
$this->innerClient->onRequest = function (Milo\Github\Http\Request $request) {
135147
Assert::false($request->hasHeader('ETag'));
136148
Assert::same('today', $request->getHeader('If-Modified-Since'));
137149

0 commit comments

Comments
 (0)