| 1 |  |  | <?php namespace Flipbox\OAuth2\Client\Test\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Flipbox\OAuth2\Client\Provider\Guardian; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Mockery as m; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class GuardianTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var Guardian */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $this->provider = new Guardian([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             'clientId' => 'mock_client_id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             'clientSecret' => 'mock_secret', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             'redirectUri' => 'none', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         m::close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function testAuthorizationUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $url = $this->provider->getAuthorizationUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $uri = parse_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         parse_str($uri['query'], $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertArrayHasKey('client_id', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertArrayHasKey('redirect_uri', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertArrayHasKey('state', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->assertArrayHasKey('scope', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertArrayHasKey('response_type', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertArrayHasKey('approval_prompt', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertNotNull($this->provider->getState()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function testScopes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $options = ['scope' => [uniqid(), uniqid()]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $url = $this->provider->getAuthorizationUrl($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $scopeString = implode(' ', $options['scope']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $scopeQuery = http_build_query(['scope' => $scopeString], null, '&', \PHP_QUERY_RFC3986); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertContains($scopeQuery, $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |     /** @test */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     public function testGetAccessToken() | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $response = m::mock('Psr\Http\Message\ResponseInterface'); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}'); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $response->shouldReceive('getStatusCode')->andReturn(200); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $client = m::mock('GuzzleHttp\ClientInterface'); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $client->shouldReceive('send')->times(1)->andReturn($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->provider->setHttpClient($client); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $this->assertEquals('mock_access_token', $token->getToken()); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertNull($token->getExpires()); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertNull($token->getRefreshToken()); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertNull($token->getResourceOwnerId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** @test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function testUserData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'token' => uniqid() . uniqid(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'email' => uniqid() . '@' . uniqid() . '.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'domain' => uniqid() . '.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'scopes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 'contacts', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'content', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 'oauth' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'expires_in' => rand(6, 10), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'id' => rand(6, 10), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'token_type' => 'access' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $postResponse->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token","expires":3600,"refresh_token":"mock_refresh_token","otherKey":"1234"}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/json']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $postResponse->shouldReceive('getStatusCode')->andReturn(200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $userResponse = m::mock('Psr\Http\Message\ResponseInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $userResponse->shouldReceive('getBody')->andReturn(json_encode($params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/json']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $userResponse->shouldReceive('getStatusCode')->andReturn(200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $client = m::mock('GuzzleHttp\ClientInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $client->shouldReceive('send') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->times(2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->andReturn($postResponse, $userResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->provider->setHttpClient($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $user = $this->provider->getResourceOwner($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertEquals($params['id'], $user->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertEquals($params['id'], $user->toArray()['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->assertEquals($params['domain'], $user->getDomain()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertEquals($params['domain'], $user->toArray()['domain']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->assertEquals($params['email'], $user->getEmail()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertEquals($params['email'], $user->toArray()['email']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function testExceptionThrownWhenErrorObjectReceived() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $status = rand(400, 600); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $postResponse->shouldReceive('getBody')->andReturn('{"message": "Validation Failed","errors": [{"resource": "Issue","field": "title","code": "missing_field"}]}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $postResponse->shouldReceive('getStatusCode')->andReturn($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $client = m::mock('GuzzleHttp\ClientInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $client->shouldReceive('send') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ->times(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ->andReturn($postResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->provider->setHttpClient($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function testExceptionThrownWhenOAuthErrorReceived() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $status = 200; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $postResponse->shouldReceive('getBody')->andReturn('{"error": "bad_verification_code","error_description": "The code passed is incorrect or expired.","error_uri": "https://developer.github.com/v3/oauth/#bad-verification-code"}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $postResponse->shouldReceive('getStatusCode')->andReturn($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $client = m::mock('GuzzleHttp\ClientInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $client->shouldReceive('send') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             ->times(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             ->andReturn($postResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->provider->setHttpClient($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.