diff --git a/composer.json b/composer.json index 90f574c15..443847af1 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/library/CM/Janus/HttpApiClient.php b/library/CM/Janus/HttpApiClient.php index dc2802670..9dae11ad9 100644 --- a/library/CM/Janus/HttpApiClient.php +++ b/library/CM/Janus/HttpApiClient.php @@ -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) { @@ -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; } } diff --git a/library/CMService/GoogleAnalytics/MeasurementProtocol/Client.php b/library/CMService/GoogleAnalytics/MeasurementProtocol/Client.php index d083a89dd..266eab5e8 100644 --- a/library/CMService/GoogleAnalytics/MeasurementProtocol/Client.php +++ b/library/CMService/GoogleAnalytics/MeasurementProtocol/Client.php @@ -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; } diff --git a/library/CMService/KissMetrics/Transport/GuzzleHttp.php b/library/CMService/KissMetrics/Transport/GuzzleHttp.php index 333ab40b6..a03f0680f 100644 --- a/library/CMService/KissMetrics/Transport/GuzzleHttp.php +++ b/library/CMService/KissMetrics/Transport/GuzzleHttp.php @@ -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); diff --git a/library/CMService/XVerify/Client.php b/library/CMService/XVerify/Client.php index f270bb438..9e5a939bd 100644 --- a/library/CMService/XVerify/Client.php +++ b/library/CMService/XVerify/Client.php @@ -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); diff --git a/tests/library/CM/Janus/HttpApiClientTest.php b/tests/library/CM/Janus/HttpApiClientTest.php index 10bef145a..a1c8be4da 100644 --- a/tests/library/CM/Janus/HttpApiClientTest.php +++ b/tests/library/CM/Janus/HttpApiClientTest.php @@ -6,20 +6,15 @@ 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 */ @@ -27,7 +22,7 @@ public function testStopStream() { /** @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()); @@ -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); @@ -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 */ @@ -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); @@ -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) { diff --git a/tests/library/CMService/XVerify/ClientTest.php b/tests/library/CMService/XVerify/ClientTest.php index 045f9c032..0786653b9 100644 --- a/tests/library/CMService/XVerify/ClientTest.php +++ b/tests/library/CMService/XVerify/ClientTest.php @@ -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);