diff --git a/composer.json b/composer.json index a669d43..43ad9e2 100644 --- a/composer.json +++ b/composer.json @@ -8,19 +8,14 @@ } ], "license": "MIT", - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/adamwathan/socialnorm" - } - ], "require": { "php": ">=5.4.0", - "guzzlehttp/guzzle": "~5.0", - "socialnorm/socialnorm": "0.1.*" + "guzzlehttp/guzzle": "^6.0", + "socialnorm/socialnorm": "^0.2" }, "require-dev": { - "mockery/mockery": "~0.8" + "mockery/mockery": "~0.8", + "phpunit/phpunit": "^4.8" }, "autoload": { "psr-4": { diff --git a/src/GitHubProvider.php b/src/GitHubProvider.php index f6edf47..4e108f7 100644 --- a/src/GitHubProvider.php +++ b/src/GitHubProvider.php @@ -66,7 +66,7 @@ protected function getEmailUrl() public function getJson($url, $headers) { $response = $this->httpClient->get($url, ['headers' => $headers]); - return $response->json(); + return json_decode($response->getBody(), true); } protected function getPrimaryEmail($emails) diff --git a/tests/GitHubProviderTest.php b/tests/GitHubProviderTest.php index eea4534..09951e2 100644 --- a/tests/GitHubProviderTest.php +++ b/tests/GitHubProviderTest.php @@ -4,26 +4,38 @@ use SocialNorm\GitHub\GitHubProvider; use SocialNorm\Request; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Client as HttpClient; -use GuzzleHttp\Subscriber\Mock as SubscriberMock; class GitHubProviderTest extends TestCase { - private function getStubbedHttpClient($responses = []) + private function getStubbedHttpClient($fixtures = []) { - $client = new HttpClient; - $mockSubscriber = new SubscriberMock($responses); - $client->getEmitter()->attach($mockSubscriber); - return $client; + $mock = new MockHandler($this->createResponses($fixtures)); + $handler = HandlerStack::create($mock); + return new HttpClient(['handler' => $handler]); + } + + private function createResponses($fixtures) + { + $responses = []; + foreach ($fixtures as $fixture) { + $response = require $fixture; + $responses[] = new Response($response['status'], $response['headers'], $response['body']); + } + + return $responses; } /** @test */ public function it_can_retrieve_a_normalized_user() { $client = $this->getStubbedHttpClient([ - __DIR__ . '/_fixtures/github_accesstoken.txt', - __DIR__ . '/_fixtures/github_user.txt', - __DIR__ . '/_fixtures/github_email.txt', + __DIR__ . '/_fixtures/github_accesstoken.php', + __DIR__ . '/_fixtures/github_user.php', + __DIR__ . '/_fixtures/github_email.php', ]); $provider = new GitHubProvider([ @@ -49,9 +61,9 @@ public function it_can_retrieve_a_normalized_user() public function it_fails_to_retrieve_a_user_when_the_authorization_code_is_omitted() { $client = $this->getStubbedHttpClient([ - __DIR__ . '/_fixtures/github_accesstoken.txt', - __DIR__ . '/_fixtures/github_user.txt', - __DIR__ . '/_fixtures/github_email.txt', + __DIR__ . '/_fixtures/github_accesstoken.php', + __DIR__ . '/_fixtures/github_user.php', + __DIR__ . '/_fixtures/github_email.php', ]); $provider = new GitHubProvider([ diff --git a/tests/_fixtures/github_accesstoken.php b/tests/_fixtures/github_accesstoken.php new file mode 100644 index 0000000..8feade8 --- /dev/null +++ b/tests/_fixtures/github_accesstoken.php @@ -0,0 +1,27 @@ + 200, + 'headers' => [ + 'Server' => 'GitHub.com', + 'Date' => 'Sat, 21 Feb 2015 18:44:34 GMT', + 'Content-Type' => 'application/json; charset=utf-8', + 'Transfer-Encoding' => 'chunked', + 'Status' => '200 OK', + 'Content-Security-Policy' => 'default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src \'self\' \'unsafe-inline\' \'unsafe-eval\' assets-cdn.github.com; img-src \'self\' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src \'none\'; frame-src \'self\' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src \'self\' ghconduit.com:25035 live.github.com wss://live.github.com uploads.github.com www.google-analytics.com s3.amazonaws.com', + 'Cache-Control' => 'no-cache', + 'Vary' => 'X-PJAX, Accept-Encoding', + 'X-UA-Compatible' => 'IE=Edge,chrome=1', + 'Set-Cookie' => 'logged_in=no; domain=.github.com; path=/; expires=Wed, 21-Feb-2035 18:44:34 GMT; secure; HttpOnly, _gh_sess=eyJzZXNzaW9uX2lkIjoiNDZkZDdiMTMzNDIxMjQ5OTNjZjliNmUyMzg4OTM5MWUiLCJsYXN0X3dyaXRlIjoxNDI0NTQ0Mjc0NDgzfQ%3D%3D--4ca192ff94067bcf8922b053b0758d1f580f85a6; path=/; secure; HttpOnly', + 'X-Request-Id' => 'f8898bcf19b20706de5712177bdf9eeb', + 'X-Runtime' => '0.010527', + 'X-Rack-Cache' => 'invalidate, pass', + 'X-GitHub-Request-Id' => 'AE71B20F:0FF4:159043E3:54E8D212', + 'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload', + 'X-Content-Type-Options' => 'nosniff', + 'X-XSS-Protection' => '1; mode=block', + 'X-Frame-Options' => 'deny', + 'X-Served-By' => 'a568c03544f42dddf712bab3bfd562fd' + ], + 'body' => '{"access_token":"abcdefgh12345678","token_type":"bearer","scope":"user:email"}' +]; diff --git a/tests/_fixtures/github_accesstoken.txt b/tests/_fixtures/github_accesstoken.txt deleted file mode 100644 index b500522..0000000 --- a/tests/_fixtures/github_accesstoken.txt +++ /dev/null @@ -1,22 +0,0 @@ -HTTP/1.1 200 OK -Server: GitHub.com -Date: Sat, 21 Feb 2015 18:44:34 GMT -Content-Type: application/json; charset=utf-8 -Transfer-Encoding: chunked -Status: 200 OK -Content-Security-Policy: default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src 'self' 'unsafe-inline' 'unsafe-eval' assets-cdn.github.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src 'none'; frame-src 'self' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src 'self' ghconduit.com:25035 live.github.com wss://live.github.com uploads.github.com www.google-analytics.com s3.amazonaws.com -Cache-Control: no-cache -Vary: X-PJAX, Accept-Encoding -X-UA-Compatible: IE=Edge,chrome=1 -Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Wed, 21-Feb-2035 18:44:34 GMT; secure; HttpOnly, _gh_sess=eyJzZXNzaW9uX2lkIjoiNDZkZDdiMTMzNDIxMjQ5OTNjZjliNmUyMzg4OTM5MWUiLCJsYXN0X3dyaXRlIjoxNDI0NTQ0Mjc0NDgzfQ%3D%3D--4ca192ff94067bcf8922b053b0758d1f580f85a6; path=/; secure; HttpOnly -X-Request-Id: f8898bcf19b20706de5712177bdf9eeb -X-Runtime: 0.010527 -X-Rack-Cache: invalidate, pass -X-GitHub-Request-Id: AE71B20F:0FF4:159043E3:54E8D212 -Strict-Transport-Security: max-age=31536000; includeSubdomains; preload -X-Content-Type-Options: nosniff -X-XSS-Protection: 1; mode=block -X-Frame-Options: deny -X-Served-By: a568c03544f42dddf712bab3bfd562fd - -{"access_token":"abcdefgh12345678","token_type":"bearer","scope":"user:email"} diff --git a/tests/_fixtures/github_email.php b/tests/_fixtures/github_email.php new file mode 100644 index 0000000..a473239 --- /dev/null +++ b/tests/_fixtures/github_email.php @@ -0,0 +1,33 @@ + 200, + 'headers' => [ + 'Server' => 'GitHub.com', + 'Date' => 'Thu, 19 Mar 2015 03:42:05 GMT', + 'Content-Type' => 'application/json; charset=utf-8', + 'Content-Length' => '62', + 'Status' => '200 OK', + 'X-RateLimit-Limit' => '5000', + 'X-RateLimit-Remaining' => '4948', + 'X-RateLimit-Reset' => '1426740125', + 'Cache-Control' => 'private, max-age=60, s-maxage=60', + 'ETag' => '"06ddc2c2d17761bc98b0e6419aed512c"', + 'X-OAuth-Scopes' => 'user:email', + 'X-Accepted-OAuth-Scopes' => 'user, user:email', + 'X-OAuth-Client-Id' => 'b80ba34640eb08f2b3e5', + 'Vary' => 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding', + 'X-GitHub-Media-Type' => 'github.v3', + 'X-XSS-Protection' => '1; mode=block', + 'X-Frame-Options' => 'deny', + 'Content-Security-Policy' => 'default-src \'none\'', + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Expose-Headers' => 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', + 'Access-Control-Allow-Origin' => '*', + 'X-GitHub-Request-Id' => 'AE71B20F:4EF8:83D06C:550A458D', + 'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload', + 'X-Content-Type-Options' => 'nosniff', + 'X-Served-By' => 'b0ef53392caa42315c6206737946d931' + ], + 'body' => '[{"email": "adam@example.com","primary": true,"verified": true}]' +]; diff --git a/tests/_fixtures/github_email.txt b/tests/_fixtures/github_email.txt deleted file mode 100644 index 95ff3f6..0000000 --- a/tests/_fixtures/github_email.txt +++ /dev/null @@ -1,34 +0,0 @@ -HTTP/1.1 200 OK -Server: GitHub.com -Date: Thu, 19 Mar 2015 03:42:05 GMT -Content-Type: application/json; charset=utf-8 -Content-Length: 92 -Status: 200 OK -X-RateLimit-Limit: 5000 -X-RateLimit-Remaining: 4948 -X-RateLimit-Reset: 1426740125 -Cache-Control: private, max-age=60, s-maxage=60 -ETag: "06ddc2c2d17761bc98b0e6419aed512c" -X-OAuth-Scopes: user:email -X-Accepted-OAuth-Scopes: user, user:email -X-OAuth-Client-Id: b80ba34640eb08f2b3e5 -Vary: Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding -X-GitHub-Media-Type: github.v3 -X-XSS-Protection: 1; mode=block -X-Frame-Options: deny -Content-Security-Policy: default-src 'none' -Access-Control-Allow-Credentials: true -Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval -Access-Control-Allow-Origin: * -X-GitHub-Request-Id: AE71B20F:4EF8:83D06C:550A458D -Strict-Transport-Security: max-age=31536000; includeSubdomains; preload -X-Content-Type-Options: nosniff -X-Served-By: b0ef53392caa42315c6206737946d931 - -[ - { - "email": "adam@example.com", - "primary": true, - "verified": true - } -] diff --git a/tests/_fixtures/github_user.php b/tests/_fixtures/github_user.php new file mode 100644 index 0000000..5047e9e --- /dev/null +++ b/tests/_fixtures/github_user.php @@ -0,0 +1,34 @@ + 200, + 'headers' => [ + 'Server' => 'GitHub.com', + 'Date' => 'Sat, 21 Feb 2015 18:43:17 GMT', + 'Content-Type' => 'application/json; charset=utf-8', + 'Content-Length' => '1278', + 'Status' => '200 OK', + 'X-RateLimit-Limit' => '5000', + 'X-RateLimit-Remaining' => '4961', + 'X-RateLimit-Reset' => '1424546049', + 'Cache-Control' => 'private, max-age=60, s-maxage=60', + 'Last-Modified' => 'Sat, 21 Feb 2015 17:06:00 GMT', + 'ETag' => '"7a29c845b431fa302144d2d2da66e7e3"', + 'X-OAuth-Scopes' => 'user:email', + 'X-Accepted-OAuth-Scopes' => '', + 'X-OAuth-Client-Id' => 'b80ba34640eb08f2b3e5', + 'Vary' => 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding', + 'X-GitHub-Media-Type' => 'github.v3', + 'X-XSS-Protection' => '1; mode=block', + 'X-Frame-Options' => 'deny', + 'Content-Security-Policy' => 'default-src \'none\'', + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Expose-Headers' => 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', + 'Access-Control-Allow-Origin' => '*', + 'X-GitHub-Request-Id' => 'AE71B20F:202B:3ABE9811:54E8D1C5', + 'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload', + 'X-Content-Type-Options' => 'nosniff', + 'X-Served-By' => '065b43cd9674091fec48a221b420fbb3' + ], + 'body' => '{"login":"adamwathan","id":4323180,"avatar_url":"https://avatars.githubusercontent.com/u/4323180?v=3","gravatar_id":"","url":"https://api.github.com/users/adamwathan","html_url":"https://github.com/adamwathan","followers_url":"https://api.github.com/users/adamwathan/followers","following_url":"https://api.github.com/users/adamwathan/following{/other_user}","gists_url":"https://api.github.com/users/adamwathan/gists{/gist_id}","starred_url":"https://api.github.com/users/adamwathan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamwathan/subscriptions","organizations_url":"https://api.github.com/users/adamwathan/orgs","repos_url":"https://api.github.com/users/adamwathan/repos","events_url":"https://api.github.com/users/adamwathan/events{/privacy}","received_events_url":"https://api.github.com/users/adamwathan/received_events","type":"User","site_admin":false,"name":"Adam Wathan","company":"Tighten Co","blog":"","location":"Ontario,Canada","email":"","hireable":false,"bio":null,"public_repos":38,"public_gists":12,"followers":54,"following":10,"created_at":"2013-05-02T15:35:48Z","updated_at":"2015-02-21T17:06:00Z"}' +]; diff --git a/tests/_fixtures/github_user.txt b/tests/_fixtures/github_user.txt deleted file mode 100644 index 42e5c73..0000000 --- a/tests/_fixtures/github_user.txt +++ /dev/null @@ -1,60 +0,0 @@ -HTTP/1.1 200 OK -Server: GitHub.com -Date: Sat, 21 Feb 2015 18:43:17 GMT -Content-Type: application/json; charset=utf-8 -Content-Length: 1278 -Status: 200 OK -X-RateLimit-Limit: 5000 -X-RateLimit-Remaining: 4961 -X-RateLimit-Reset: 1424546049 -Cache-Control: private, max-age=60, s-maxage=60 -Last-Modified: Sat, 21 Feb 2015 17:06:00 GMT -ETag: "7a29c845b431fa302144d2d2da66e7e3" -X-OAuth-Scopes: user:email -X-Accepted-OAuth-Scopes: -X-OAuth-Client-Id: b80ba34640eb08f2b3e5 -Vary: Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding -X-GitHub-Media-Type: github.v3 -X-XSS-Protection: 1; mode=block -X-Frame-Options: deny -Content-Security-Policy: default-src 'none' -Access-Control-Allow-Credentials: true -Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval -Access-Control-Allow-Origin: * -X-GitHub-Request-Id: AE71B20F:202B:3ABE9811:54E8D1C5 -Strict-Transport-Security: max-age=31536000; includeSubdomains; preload -X-Content-Type-Options: nosniff -X-Served-By: 065b43cd9674091fec48a221b420fbb3 - -{ - "login": "adamwathan", - "id": 4323180, - "avatar_url": "https://avatars.githubusercontent.com/u/4323180?v=3", - "gravatar_id": "", - "url": "https://api.github.com/users/adamwathan", - "html_url": "https://github.com/adamwathan", - "followers_url": "https://api.github.com/users/adamwathan/followers", - "following_url": "https://api.github.com/users/adamwathan/following{/other_user}", - "gists_url": "https://api.github.com/users/adamwathan/gists{/gist_id}", - "starred_url": "https://api.github.com/users/adamwathan/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/adamwathan/subscriptions", - "organizations_url": "https://api.github.com/users/adamwathan/orgs", - "repos_url": "https://api.github.com/users/adamwathan/repos", - "events_url": "https://api.github.com/users/adamwathan/events{/privacy}", - "received_events_url": "https://api.github.com/users/adamwathan/received_events", - "type": "User", - "site_admin": false, - "name": "Adam Wathan", - "company": "Tighten Co", - "blog": "", - "location": "Ontario, Canada", - "email": "", - "hireable": false, - "bio": null, - "public_repos": 38, - "public_gists": 12, - "followers": 54, - "following": 10, - "created_at": "2013-05-02T15:35:48Z", - "updated_at": "2015-02-21T17:06:00Z" -}