Permalink
Browse files

Modify v1 codebase to support Guzzle 6 under the hood.

  • Loading branch information...
1 parent a68285f commit 19de185c22689fe93fb372e6853870a2b4ab358b @bencorlett bencorlett committed Jul 21, 2016
Showing with 96 additions and 86 deletions.
  1. +4 −4 composer.json
  2. +19 −13 src/Client/Server/Server.php
  3. +17 −22 src/Client/Signature/HmacSha1Signature.php
  4. +23 −17 tests/ServerTest.php
  5. +17 −15 tests/TrelloServerTest.php
  6. +16 −15 tests/XingServerTest.php
View
@@ -4,12 +4,12 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
- "guzzlehttp/guzzle": "3.*"
+ "guzzlehttp/guzzle": "^6.0"
},
"require-dev": {
- "phpunit/phpunit": "~4.0",
- "mockery/mockery": "~0.9",
- "squizlabs/php_codesniffer": "~2.0"
+ "phpunit/phpunit": "^4.0",
+ "mockery/mockery": "^0.9",
+ "squizlabs/php_codesniffer": "^2.0"
},
"keywords": [
"oauth",
@@ -2,8 +2,8 @@
namespace League\OAuth1\Client\Server;
-use Guzzle\Service\Client as GuzzleClient;
-use Guzzle\Http\Exception\BadResponseException;
+use GuzzleHttp\Client as GuzzleHttpClient;
+use GuzzleHttp\Exception\BadResponseException;
use League\OAuth1\Client\Credentials\ClientCredentialsInterface;
use League\OAuth1\Client\Credentials\ClientCredentials;
use League\OAuth1\Client\Credentials\CredentialsInterface;
@@ -86,12 +86,14 @@ public function getTemporaryCredentials()
$headers = $this->buildHttpClientHeaders($authorizationHeader);
try {
- $response = $client->post($uri, $headers)->send();
+ $response = $client->post($uri, [
+ 'headers' => $headers,
+ ]);
} catch (BadResponseException $e) {
return $this->handleTemporaryCredentialsBadResponse($e);
}
- return $this->createTemporaryCredentials($response->getBody());
+ return $this->createTemporaryCredentials((string) $response->getBody());
}
/**
@@ -160,12 +162,15 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials,
$headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters);
try {
- $response = $client->post($uri, $headers, $bodyParameters)->send();
+ $response = $client->post($uri, [
+ 'headers' => $headers,
+ 'form_params' => $bodyParameters,
+ ]);
} catch (BadResponseException $e) {
return $this->handleTokenCredentialsBadResponse($e);
}
- return $this->createTokenCredentials($response->getBody());
+ return $this->createTokenCredentials((string) $response->getBody());
}
/**
@@ -246,7 +251,9 @@ protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force =
$headers = $this->getHeaders($tokenCredentials, 'GET', $url);
try {
- $response = $client->get($url, $headers)->send();
+ $response = $client->get($url, [
+ 'headers' => $headers,
+ ]);
} catch (BadResponseException $e) {
$response = $e->getResponse();
$body = $response->getBody();
@@ -256,18 +263,17 @@ protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force =
"Received error [$body] with status code [$statusCode] when retrieving token credentials."
);
}
-
switch ($this->responseType) {
case 'json':
- $this->cachedUserDetailsResponse = $response->json();
+ $this->cachedUserDetailsResponse = json_decode((string) $response->getBody(), true);
break;
case 'xml':
- $this->cachedUserDetailsResponse = $response->xml();
+ $this->cachedUserDetailsResponse = simplexml_load_string((string) $response->getBody());
break;
case 'string':
- parse_str($response->getBody(), $this->cachedUserDetailsResponse);
+ parse_str((string) $response->getBody(), $this->cachedUserDetailsResponse);
break;
default:
@@ -301,11 +307,11 @@ public function getSignature()
/**
* Creates a Guzzle HTTP client for the given URL.
*
- * @return GuzzleClient
+ * @return GuzzleHttpClient
*/
public function createHttpClient()
{
- return new GuzzleClient();
+ return new GuzzleHttpClient();
}
/**
@@ -19,26 +19,12 @@ public function method()
*/
public function sign($uri, array $parameters = array(), $method = 'POST')
{
- $url = $this->createUrl($uri);
-
- $baseString = $this->baseString($url, $method, $parameters);
+ $baseString = $this->baseString($uri, $method, $parameters);
return base64_encode($this->hash($baseString));
}
/**
- * Create a Guzzle url for the given URI.
- *
- * @param string $uri
- *
- * @return Url
- */
- protected function createUrl($uri)
- {
- return Url::factory($uri);
- }
-
- /**
* Generate a base string for a HMAC-SHA1 signature
* based on the given a url, method, and any parameters.
*
@@ -48,20 +34,29 @@ protected function createUrl($uri)
*
* @return string
*/
- protected function baseString(Url $url, $method = 'POST', array $parameters = array())
+ protected function baseString($url, $method = 'POST', array $parameters = array())
{
$baseString = rawurlencode($method).'&';
- $schemeHostPath = Url::buildUrl(array(
- 'scheme' => $url->getScheme(),
- 'host' => $url->getHost(),
- 'path' => $url->getPath(),
- ));
+ $parsedUrl = parse_url($url);
+ if (!isset($parsedUrl['query'])) {
+ $parsedUrl['query'] = '';
+ }
+
+ $schemeHostPath = sprintf(
+ '%s://%s',
+ $parsedUrl['scheme'],
+ $parsedUrl['host']
+ );
+
+ if ($parsedUrl['path']) {
+ $schemeHostPath .= '/'.ltrim($parsedUrl['path'], '/');
+ }
$baseString .= rawurlencode($schemeHostPath).'&';
$data = array();
- parse_str($url->getQuery(), $query);
+ parse_str($parsedUrl['query'], $query);
$data = array_merge($query, $parameters);
// normalize data key/values
View
@@ -82,7 +82,9 @@ public function testGettingTemporaryCredentials()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('post')->with('http://www.example.com/temporary', m::on(function($headers) use ($me) {
+ $client->shouldReceive('post')->with('http://www.example.com/temporary', m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
@@ -94,9 +96,7 @@ public function testGettingTemporaryCredentials()
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
- }))->once()->andReturn($request = m::mock('stdClass'));
-
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ }))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
$credentials = $server->getTemporaryCredentials();
@@ -142,7 +142,10 @@ public function testGettingTokenCredentials()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me) {
+ $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+ $body = $options['form_params'];
+
$me->assertTrue(isset($headers['Authorization']));
$me->assertFalse(isset($headers['User-Agent']));
@@ -154,10 +157,10 @@ public function testGettingTokenCredentials()
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
- return true;
- }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
+ $me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ return true;
+ }))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
@@ -178,7 +181,10 @@ public function testGettingTokenCredentialsWithUserAgent()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me, $userAgent) {
+ $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me, $userAgent) {
+ $headers = $options['headers'];
+ $body = $options['form_params'];
+
$me->assertTrue(isset($headers['Authorization']));
$me->assertTrue(isset($headers['User-Agent']));
$me->assertEquals($userAgent, $headers['User-Agent']);
@@ -191,10 +197,10 @@ public function testGettingTokenCredentialsWithUserAgent()
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
- return true;
- }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
+ $me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ return true;
+ }))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->setUserAgent($userAgent)->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
@@ -215,7 +221,9 @@ public function testGettingUserDetails()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('get')->with('http://www.example.com/user', m::on(function($headers) use ($me) {
+ $client->shouldReceive('get')->with('http://www.example.com/user', m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
@@ -227,10 +235,8 @@ public function testGettingUserDetails()
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
- }))->once()->andReturn($request = m::mock('stdClass'));
-
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
- $response->shouldReceive('json')->once()->andReturn(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred'));
+ }))->once()->andReturn($response = m::mock('stdClass'));
+ $response->shouldReceive('getBody')->once()->andReturn(json_encode(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred')));
$user = $server->getUserDetails($temporaryCredentials);
$this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
@@ -47,7 +47,9 @@ public function testGettingTemporaryCredentials()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetRequestToken', m::on(function($headers) use ($me) {
+ $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetRequestToken', m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
@@ -59,9 +61,7 @@ public function testGettingTemporaryCredentials()
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
- }))->once()->andReturn($request = m::mock('stdClass'));
-
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ }))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
$credentials = $server->getTemporaryCredentials();
@@ -200,7 +200,10 @@ public function testGettingTokenCredentials()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetAccessToken', m::on(function($headers) use ($me) {
+ $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetAccessToken', m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+ $body = $options['form_params'];
+
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
@@ -211,10 +214,10 @@ public function testGettingTokenCredentials()
$matches = preg_match($pattern, $headers['Authorization']);
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
- return true;
- }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
+ $me->assertSame($body, array('oauth_verifier' => 'myverifiercode'));
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ return true;
+ }))->once()->andReturn($response = m::mock('stdClass'));
$response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
$credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
@@ -234,7 +237,9 @@ public function testGettingUserDetails()
$server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
$me = $this;
- $client->shouldReceive('get')->with('https://trello.com/1/members/me?key='.$this->getApplicationKey().'&token='.$this->getAccessToken(), m::on(function($headers) use ($me) {
+ $client->shouldReceive('get')->with('https://trello.com/1/members/me?key='.$this->getApplicationKey().'&token='.$this->getAccessToken(), m::on(function($options) use ($me) {
+ $headers = $options['headers'];
+
$me->assertTrue(isset($headers['Authorization']));
// OAuth protocol specifies a strict number of
@@ -246,10 +251,8 @@ public function testGettingUserDetails()
$me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
return true;
- }))->once()->andReturn($request = m::mock('stdClass'));
-
- $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
- $response->shouldReceive('json')->once()->andReturn($this->getUserPayload());
+ }))->once()->andReturn($response = m::mock('stdClass'));
+ $response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload());
$user = $server
->setAccessToken($this->getAccessToken())
@@ -297,7 +300,7 @@ protected function getApplicationScope($readonly = true)
private function getUserPayload()
{
- $user = '{
+ return '{
"id": "545df696e29c0dddaed31967",
"avatarHash": null,
"bio": "I have magical powers",
@@ -342,6 +345,5 @@ private function getUserPayload()
"premiumFeatures": [],
"idBoardsPinned": null
}';
- return json_decode($user, true);
}
}
Oops, something went wrong.

0 comments on commit 19de185

Please sign in to comment.