Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2526 from fauvel/guzzle-v6
Browse files Browse the repository at this point in the history
Upgraded to Guzzle v6
  • Loading branch information
fauvel authored Feb 1, 2017
2 parents 6520b0f + 1df4551 commit eda73b4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 44 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"nicmart/tree": "~0.2.0",
"kissmetrics/kissmetrics-php": "~0.1.0",
"elasticsearch/elasticsearch": "~2.0.0",
"guzzlehttp/guzzle": "~5.3.1",
"guzzlehttp/guzzle": "~6.0",
"kickbox/kickbox": "~1.0.3",
"tomaszdurka/codegenerator": "~0.5.0",
"jenssegers/agent": "~2.3.1",
Expand Down
18 changes: 7 additions & 11 deletions library/CM/Janus/HttpApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ protected function _request($method, CM_Janus_Server $server, $path, array $body
$context = CM_Service_Manager::getInstance()->getLogger()->getContext();
$appContext = $this->_contextFormatter->formatAppContext($context);

$url = $server->getHttpAddress() . $path;
$query = ['context' => CM_Util::jsonEncode($appContext)];
$url = $server->getHttpAddress() . $path . '?' . http_build_query($query);
$body = (array) $body;

$options = [
'query' => ['context' => CM_Util::jsonEncode($appContext)],
'body' => $body,
'headers' => ['Server-Key' => $server->getKey()],
];
$request = $this->_httpClient->createRequest($method, $url, $options);
$headers = ['Server-Key' => $server->getKey()];
$request = new \GuzzleHttp\Psr7\Request($method, $url, $headers, http_build_query($body));
try {
$response = $this->_httpClient->send($request);
} catch (GuzzleHttp\Exception\TransferException $e) {
Expand All @@ -67,10 +63,10 @@ protected function _request($method, CM_Janus_Server $server, $path, array $body
'originalExceptionMessage' => $e->getMessage(),
]);
}
$body = $response->getBody();
if (null === $body) {
$contents = $response->getBody()->getContents();
if ('' === $contents) {
throw new CM_Exception_Invalid('Empty response body');
}
return $body->getContents();
return $contents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function _submitRequest(array $parameterList) {
*/
protected function _getGuzzleClient() {
if (!self::$_client) {
self::$_client = new \GuzzleHttp\Client(['base_url' => 'http://www.google-analytics.com']);
self::$_client = new \GuzzleHttp\Client(['base_uri' => 'http://www.google-analytics.com']);
}
return self::$_client;
}
Expand Down
2 changes: 1 addition & 1 deletion library/CMService/KissMetrics/Transport/GuzzleHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CMService_KissMetrics_Transport_GuzzleHttp implements \KISSmetrics\Transpo

public function submitData(array $dataList) {
if (!self::$_client) {
self::$_client = new \GuzzleHttp\Client(['base_url' => 'http://trk.kissmetrics.com']);
self::$_client = new \GuzzleHttp\Client(['base_uri' => 'http://trk.kissmetrics.com']);
}
foreach ($dataList as $data) {
$url = '/' . $data[0] . '?' . http_build_query($data[1], '', '&', PHP_QUERY_RFC3986);
Expand Down
4 changes: 2 additions & 2 deletions library/CMService/XVerify/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ protected function _getDomain() {

/**
* @param string $email
* @return \GuzzleHttp\Message\ResponseInterface
* @return \Psr\Http\Message\ResponseInterface
* @throws Exception
*/
protected function _getResponse($email) {
$email = (string) $email;
$client = new \GuzzleHttp\Client(['base_url' => 'http://www.xverify.com']);
$client = new \GuzzleHttp\Client(['base_uri' => 'http://www.xverify.com']);
$parameterList = array('email' => $email, 'type' => 'json', 'domain' => $this->_getDomain(), 'apikey' => $this->_getCode());
$url = '/services/emails/verify/?' . http_build_query($parameterList, '', '&', PHP_QUERY_RFC3986);
return $client->get($url);
Expand Down
42 changes: 15 additions & 27 deletions tests/library/CM/Janus/HttpApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ public function testStopStream() {
$contextFormatter = $this->mockInterface('CM_Log_ContextFormatter_Interface')->newInstanceWithoutConstructor();
$contextFormatter->mockMethod('formatAppContext')->set(['key' => 'value']);
/** @var CM_Log_ContextFormatter_Interface $contextFormatter */

$httpClient = $this->mockObject('GuzzleHttp\Client');
$sendRequestMethod = $httpClient->mockMethod('send')->set(function (\GuzzleHttp\Message\RequestInterface $request) {
$this->assertSame('http://cm-janus.dev:8080/stopStream?context={"key":"value"}', urldecode($request->getUrl()));
$sendRequestMethod = $httpClient->mockMethod('send')->set(function (\GuzzleHttp\Psr7\Request $request) {
$this->assertSame('http://cm-janus.dev:8080/stopStream?context={"key":"value"}', urldecode($request->getUri()));
$this->assertSame('POST', $request->getMethod());
$this->assertSame('streamId=foo', $request->getBody()->getContents());
$this->assertSame('bar', $request->getHeader('Server-Key'));

$body = $this->mockClass('\GuzzleHttp\Post\PostBody')->newInstanceWithoutConstructor();
$body->mockMethod('getContents')->set('{"success":"Stream stopped"}');
$this->assertSame('bar', $request->getHeaderLine('Server-Key'));

$response = $this->mockClass('\GuzzleHttp\Message\Response')->newInstanceWithoutConstructor();
$response->mockMethod('getBody')->set($body);
return $response;
return new \GuzzleHttp\Psr7\Response(200, [], '{"success":"Stream stopped"}');
});
/** @var GuzzleHttp\Client $httpClient */

$location = $this->mockClass('CM_Geo_Point')->newInstanceWithoutConstructor();
/** @var CM_Geo_Point $location */

$server = new CM_Janus_Server(0, 'bar', 'http://cm-janus.dev:8080', 'ws://cm-janus.dev:8188', [], $location);

$api = new CM_Janus_HttpApiClient($httpClient, $contextFormatter);
$api->stopStream($server, 'foo');
$this->assertSame(1, $sendRequestMethod->getCallCount());
Expand All @@ -39,25 +34,20 @@ public function testFetchStatus() {
/** @var CM_Log_ContextFormatter_Interface $contextFormatter */

$httpClient = $this->mockObject('GuzzleHttp\Client');
$sendRequestMethod = $httpClient->mockMethod('send')->set(function (\GuzzleHttp\Message\RequestInterface $request) {
$this->assertSame('http://cm-janus.dev:8080/status?context={"key":"value"}', urldecode($request->getUrl()));
$sendRequestMethod = $httpClient->mockMethod('send')->set(function (\GuzzleHttp\Psr7\Request $request) {
$this->assertSame('http://cm-janus.dev:8080/status?context={"key":"value"}', urldecode($request->getUri()));
$this->assertSame('GET', $request->getMethod());
$this->assertSame('bar', $request->getHeader('Server-Key'));

$body = $this->mockClass('\GuzzleHttp\Post\PostBody')->newInstanceWithoutConstructor();
$body->mockMethod('getContents')->set('[{"id":"foo", "channelName":"bar"},{"id":"baz", "channelName":"quux"}]');
$this->assertSame('bar', $request->getHeaderLine('Server-Key'));

$response = $this->mockClass('\GuzzleHttp\Message\Response')->newInstanceWithoutConstructor();
$response->mockMethod('getBody')->set($body);
return $response;
return new \GuzzleHttp\Psr7\Response(200, [], '[{"id":"foo", "channelName":"bar"},{"id":"baz", "channelName":"quux"}]');
});
/** @var GuzzleHttp\Client $httpClient */

$location = $this->mockClass('CM_Geo_Point')->newInstanceWithoutConstructor();
/** @var CM_Geo_Point $location */

$server = new CM_Janus_Server(0, 'bar', 'http://cm-janus.dev:8080', 'ws://cm-janus.dev:8188', [], $location);

$api = new CM_Janus_HttpApiClient($httpClient, $contextFormatter);
$result = $api->fetchStatus($server);
$this->assertSame([['id' => 'foo', 'channelName' => 'bar'], ['id' => 'baz', 'channelName' => 'quux']], $result);
Expand All @@ -67,7 +57,7 @@ public function testFetchStatus() {
public function testFail() {
/** @var CM_Log_ContextFormatter_Interface $contextFormatter */
$contextFormatter = $this->mockInterface('CM_Log_ContextFormatter_Interface')->newInstanceWithoutConstructor();

/** @var GuzzleHttp\Client|\Mocka\AbstractClassTrait $httpClient */
$httpClient = $this->mockObject('GuzzleHttp\Client');
/** @var \Mocka\FunctionMock $sendFailMethod */
Expand All @@ -79,7 +69,7 @@ public function testFail() {
/** @var CM_Geo_Point $location */

$server = new CM_Janus_Server(0, 'bar', 'http://cm-janus.dev:8080', 'ws://cm-janus.dev:8188', [], $location);

$api = new CM_Janus_HttpApiClient($httpClient, $contextFormatter);
$exception = $this->catchException(function () use ($api, $server) {
$api->fetchStatus($server);
Expand All @@ -90,9 +80,7 @@ public function testFail() {
$this->assertSame(1, $sendFailMethod->getCallCount());

$httpClient->mockMethod('send')->set(function () {
$response = $this->mockClass('\GuzzleHttp\Message\Response')->newInstanceWithoutConstructor();
$response->mockMethod('getBody')->set(null);
return $response;
return new \GuzzleHttp\Psr7\Response();
});

$exception = $this->catchException(function () use ($api, $server) {
Expand Down
3 changes: 2 additions & 1 deletion tests/library/CMService/XVerify/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ protected function _getXVerifyMock($responseBody, $statusCode = null, $headerLis
$mockBuilder = $this->getMockBuilder('CMService_XVerify_Client');
$mockBuilder->setMethods(['_getResponse', '_handleException']);
$mockBuilder->setConstructorArgs(['', '']);
/** @var PHPUnit_Framework_MockObject_MockObject|CMService_XVerify_Client $xVerifyMock */
$xVerifyMock = $mockBuilder->getMock();
$responseMock = new \GuzzleHttp\Message\Response($statusCode, $headerList, \GuzzleHttp\Stream\Stream::factory($responseBody));
$responseMock = new \GuzzleHttp\Psr7\Response($statusCode, $headerList, \GuzzleHttp\Stream\Stream::factory($responseBody));
$xVerifyMock->expects($this->once())->method('_getResponse')->will($this->returnValue($responseMock));
if ($exceptionExpected) {
$xVerifyMock->expects($this->once())->method('_handleException')->with($exceptionExpected);
Expand Down

0 comments on commit eda73b4

Please sign in to comment.