From 4a843af71100b410bf9f1ffdb176d3296286a134 Mon Sep 17 00:00:00 2001 From: Tom Anderson Date: Fri, 25 Apr 2014 10:13:33 -0700 Subject: [PATCH] working on ms --- src/Provider/Microsoft.php | 9 ++- test/src/Provider/MicrosoftTest.php | 95 +++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 test/src/Provider/MicrosoftTest.php diff --git a/src/Provider/Microsoft.php b/src/Provider/Microsoft.php index 9b0c3ad..a87fc2d 100644 --- a/src/Provider/Microsoft.php +++ b/src/Provider/Microsoft.php @@ -24,7 +24,14 @@ class Microsoft extends IdentityProvider public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token) { - $imageHeaders = get_headers('https://apis.live.net/v5.0/'.$response->id.'/picture', 1); + $client = $this->getHttpClient(); + $client->setBaseUrl('https://apis.live.net/v5.0/' . $response->id . '/picture'); + $request = $client->get()->send(); + $info = $request->getInfo(); + + print_r($info);die(); + + $imageUrl = $info['url']; $user = new User; diff --git a/test/src/Provider/MicrosoftTest.php b/test/src/Provider/MicrosoftTest.php new file mode 100644 index 0000000..b2b6129 --- /dev/null +++ b/test/src/Provider/MicrosoftTest.php @@ -0,0 +1,95 @@ +provider = new \League\OAuth2\Client\Provider\Microsoft(array( + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + )); + } + + protected function tearDown() + { +# m::close(); + } + + public function testAuthorizationUrl() + { + $url = $this->provider->getAuthorizationUrl(); + $uri = parse_url($url); + parse_str($uri['query'], $query); + + $this->assertArrayHasKey('client_id', $query); + $this->assertArrayHasKey('redirect_uri', $query); + $this->assertArrayHasKey('state', $query); + $this->assertArrayHasKey('scope', $query); + $this->assertArrayHasKey('response_type', $query); + $this->assertArrayHasKey('approval_prompt', $query); + } + + public function testUrlAccessToken() + { + $url = $this->provider->urlAccessToken(); + $uri = parse_url($url); + + $this->assertEquals('/token', $uri['path']); + } + + public function testGetAccessToken() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', array('code' => 'mock_authorization_code')); + +# print_r($token);die(); + + $this->assertEquals('mock_access_token', $token->accessToken); + $this->assertLessThanOrEqual(time() + 3600, $token->expires); + $this->assertGreaterThanOrEqual(time(), $token->expires); + $this->assertEquals('mock_refresh_token', $token->refreshToken); + $this->assertEquals('1', $token->uid); + } + + public function testScopes() + { + $this->assertEquals(array('wl.basic', 'wl.emails'), $this->provider->getScopes()); + } + + public function testUserData() + { + $postResponse = m::mock('Guzzle\Http\Message\Response'); + $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $getResponse = m::mock('Guzzle\Http\Message\Response'); + $getResponse->shouldReceive('getBody')->times(1)->andReturn('{"id": 12345, "name": "mock_name", "first_name": "mock_first_name", "last_name": "mock_last_name", "emails": {"preferred": "mock_email"}, "link": "mock_link"}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(1); + $client->shouldReceive('post->send')->times(1)->andReturn($postResponse); + $client->shouldReceive('get->send')->times(1)->andReturn($getResponse); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', array('code' => 'mock_authorization_code')); + $user = $this->provider->getUserDetails($token); + + $this->assertEquals(12345, $this->provider->getUserUid($token)); + $this->assertEquals(array('mock_first_name', 'mock_last_name'), $this->provider->getUserScreenName($token)); + $this->assertEquals('mock_email', $this->provider->getUserEmail($token)); + $this->assertEquals('mock_email', $user->email); + } +}