Skip to content

Commit 9b55adb

Browse files
committed
Fix CachedHttpKernel tests, fix events
1 parent c90c84c commit 9b55adb

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6161
*
6262
* {@inheritdoc}
6363
*/
64-
protected function createRequest($httpMethod, $url)
64+
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
6565
{
66-
$request = parent::createRequest($httpMethod, $url);
66+
$request = parent::createRequest($httpMethod, $path, $requestBody, $headers = array());
6767

68-
if ($modifiedAt = $this->getCache()->getModifiedSince($url)) {
68+
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
6969
$modifiedAt = new \DateTime('@'.$modifiedAt);
7070
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));
7171

lib/Github/HttpClient/HttpClient.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,11 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
131131
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
132132
;
133133

134-
$request = $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
134+
$request = $this->createRequest($httpMethod, $path, $requestBody, $headers);
135135
$request->addHeaders($headers);
136136

137137
try {
138-
$response = Response::fromMessage(
139-
$this->client->send($request)
140-
);
138+
$response = $this->createResponse($this->client->send($request));
141139
} catch (\LogicException $e) {
142140
throw new ErrorException($e->getMessage());
143141
} catch (\RuntimeException $e) {
@@ -175,4 +173,14 @@ public function getLastResponse()
175173
{
176174
return $this->lastResponse;
177175
}
176+
177+
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
178+
{
179+
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
180+
}
181+
182+
protected function createResponse($response)
183+
{
184+
return Response::fromMessage($response);
185+
}
178186
}

test/Github/Tests/HttpClient/CachedHttpClientTest.php

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,30 @@
88
class CachedHttpClientTest extends HttpClientTest
99
{
1010
/**
11-
* test
11+
* @test
1212
*/
1313
public function shouldCacheResponseAtFirstTime()
1414
{
1515
$cache = $this->getCacheMock();
16+
$response = new Response(200);
1617

17-
$httpClient = new TestCachedHttpClient(
18-
array('base_url' => ''),
19-
$this->getMock('Guzzle\Http\ClientInterface', array('send'))
20-
);
18+
$httpClient = $this->getHttpClientMock($response);
2119
$httpClient->setCache($cache);
2220

23-
$cache->expects($this->once())->method('set')->with('test', new Response(200));
21+
$cache->expects($this->once())->method('set')->with('test', $response);
2422

2523
$httpClient->get('test');
2624
}
2725

2826
/**
29-
* test
27+
* @test
3028
*/
3129
public function shouldGetCachedResponseWhileResourceNotModified()
3230
{
33-
$client = $this->getMock('Guzzle\Http\ClientInterface', array('send'));
34-
$client->expects($this->once())->method('send');
35-
3631
$cache = $this->getCacheMock();
37-
3832
$response = new Response(304);
3933

40-
$httpClient = new TestCachedHttpClient(
41-
array('base_url' => ''),
42-
$client
43-
);
34+
$httpClient = $this->getHttpClientMock($response);
4435
$httpClient->setCache($cache);
4536
$httpClient->fakeResponse = $response;
4637

@@ -50,23 +41,15 @@ public function shouldGetCachedResponseWhileResourceNotModified()
5041
}
5142

5243
/**
53-
* test
44+
* @test
5445
*/
5546
public function shouldRenewCacheWhenResourceHasChanged()
5647
{
57-
$client = $this->getMock('Guzzle\Http\ClientInterface', array('send'));
58-
$client->expects($this->once())->method('send');
59-
6048
$cache = $this->getCacheMock();
61-
6249
$response = new Response(200);
6350

64-
$httpClient = new TestCachedHttpClient(
65-
array('base_url' => ''),
66-
$client
67-
);
51+
$httpClient = $this->getHttpClientMock($response);
6852
$httpClient->setCache($cache);
69-
$httpClient->fakeResponse = $response;
7053

7154
$cache->expects($this->once())->method('set')->with('test', $response);
7255
$cache->expects($this->once())->method('getModifiedSince')->with('test')->will($this->returnValue(1256953732));
@@ -78,14 +61,19 @@ public function getCacheMock()
7861
{
7962
return $this->getMock('Github\HttpClient\Cache\CacheInterface');
8063
}
81-
}
82-
83-
class TestCachedHttpClient extends CachedHttpClient
84-
{
85-
public $fakeResponse;
8664

87-
protected function createResponse()
65+
private function getHttpClientMock($response)
8866
{
89-
return $this->fakeResponse ?: new Response(200);
67+
$mock = $this
68+
->getMockBuilder('Github\HttpClient\CachedHttpClient')
69+
->setConstructorArgs(array(array('base_url' => ''), $this->getBrowserMock()))
70+
->setMethods(array('createResponse'))
71+
->getMock();
72+
73+
$mock->expects($this->any())
74+
->method('createResponse')
75+
->will($this->returnValue($response));
76+
77+
return $mock;
9078
}
9179
}

test/Github/Tests/HttpClient/HttpClientTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ public function shouldBeAbleToSetOption()
4141
public function shouldAuthenticateUsingAllGivenParameters($login, $password, $method)
4242
{
4343
$client = new GuzzleClient();
44+
$listeners = $client->getEventDispatcher()->getListeners('request.before_send');
45+
$this->assertCount(1, $listeners);
4446

4547
$httpClient = new TestHttpClient(array(), $client);
4648
$httpClient->authenticate($login, $password, $method);
4749

48-
$this->assertCount(1, $client->getEventDispatcher()->getListeners('request.create'));
49-
$this->assertInstanceOf(
50-
'Github\HttpClient\Listener\AuthListener',
51-
current(current($client->getEventDispatcher()->getListeners('request.create')))
52-
);
50+
$listeners = $client->getEventDispatcher()->getListeners('request.before_send');
51+
$this->assertCount(2, $listeners);
52+
53+
$authListener = $listeners[1][0];
54+
$this->assertInstanceOf('Github\HttpClient\Listener\AuthListener', $authListener);
5355
}
5456

5557
public function getAuthenticationFullData()
@@ -88,8 +90,8 @@ public function shouldDoPOSTRequest()
8890

8991
$client = $this->getBrowserMock();
9092
$client->expects($this->once())
91-
->method('send')
92-
->with($this->anything(), $this->isType('array'), '{"a":"b"}');
93+
->method('createRequest')
94+
->with('POST', $path, $this->isType('array'), '{"a":"b"}');
9395

9496
$httpClient = new HttpClient(array(), $client);
9597
$httpClient->post($path, $parameters, $headers);
@@ -103,10 +105,9 @@ public function shouldDoPOSTRequestWithoutContent()
103105
$path = '/some/path';
104106

105107
$client = $this->getBrowserMock();
106-
107108
$client->expects($this->once())
108-
->method('send')
109-
->with($this->anything(), $this->isType('array'), $this->isEmpty());
109+
->method('createRequest')
110+
->with('POST', $path, $this->isType('array'));
110111

111112
$httpClient = new HttpClient(array(), $client);
112113
$httpClient->post($path);
@@ -242,13 +243,19 @@ public function shouldThrowExceptionWhenApiIsExceeded()
242243

243244
protected function getBrowserMock(array $methods = array())
244245
{
245-
return $this->getMock(
246+
$mock = $this->getMock(
246247
'Guzzle\Http\Client',
247248
array_merge(
248-
array('send'),
249+
array('send', 'createRequest'),
249250
$methods
250251
)
251252
);
253+
254+
$mock->expects($this->any())
255+
->method('createRequest')
256+
->will($this->returnValue($this->getMock('Guzzle\Http\Message\Request', array(), array('GET', 'some'))));
257+
258+
return $mock;
252259
}
253260
}
254261

@@ -261,7 +268,7 @@ public function getOption($name, $default = null)
261268

262269
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
263270
{
264-
$request = $this->createRequest($httpMethod, $path);
271+
$request = $this->client->createRequest($httpMethod, $path);
265272

266273
return $this->client->send($request, $headers, $parameters);
267274
}

0 commit comments

Comments
 (0)