OAuthTestUnitOAuth2ServiceGitHubTest

The Gravity Forms Salesforce Add-On OAuthTest Unit OAuth2 Service GitHubTest class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/tests/Unit/OAuth2/Service/GitHubTest.php  
  1. class GitHubTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  4. */ 
  5. public function testConstructCorrectInterfaceWithoutCustomUri() 
  6. $service = new GitHub( 
  7. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  8. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  9. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  10. ); 
  11.  
  12. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); 
  13.  
  14. /** 
  15. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  16. */ 
  17. public function testConstructCorrectInstanceWithoutCustomUri() 
  18. $service = new GitHub( 
  19. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  20. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  21. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  22. ); 
  23.  
  24. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); 
  25.  
  26. /** 
  27. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  28. */ 
  29. public function testConstructCorrectInstanceWithCustomUri() 
  30. $service = new GitHub( 
  31. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  32. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  33. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  34. array(),  
  35. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') 
  36. ); 
  37.  
  38. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); 
  39.  
  40. /** 
  41. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  42. * @covers OAuth\OAuth2\Service\GitHub::getAuthorizationEndpoint 
  43. */ 
  44. public function testGetAuthorizationEndpoint() 
  45. $service = new GitHub( 
  46. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  47. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  48. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  49. ); 
  50.  
  51. $this->assertSame('https://github.com/login/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri()); 
  52.  
  53. /** 
  54. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  55. * @covers OAuth\OAuth2\Service\GitHub::getAccessTokenEndpoint 
  56. */ 
  57. public function testGetAccessTokenEndpoint() 
  58. $service = new GitHub( 
  59. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  60. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  61. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  62. ); 
  63.  
  64. $this->assertSame('https://github.com/login/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); 
  65.  
  66. /** 
  67. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  68. * @covers OAuth\OAuth2\Service\GitHub::getAuthorizationMethod 
  69. */ 
  70. public function testGetAuthorizationMethod() 
  71. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  72. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0)); 
  73.  
  74. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  75. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  76. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  77.  
  78. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  79. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  80.  
  81. $service = new GitHub( 
  82. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  83. $client,  
  84. $storage 
  85. ); 
  86.  
  87. $uri = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  88. $absoluteUri = parse_url($uri->getAbsoluteUri()); 
  89.  
  90. $this->assertSame('access_token=foo', $absoluteUri['query']); 
  91.  
  92. /** 
  93. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  94. * @covers OAuth\OAuth2\Service\GitHub::parseAccessTokenResponse 
  95. */ 
  96. public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 
  97. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  98. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null)); 
  99.  
  100. $service = new GitHub( 
  101. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  102. $client,  
  103. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  104. ); 
  105.  
  106. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  107.  
  108. $service->requestAccessToken('foo'); 
  109.  
  110. /** 
  111. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  112. * @covers OAuth\OAuth2\Service\GitHub::parseAccessTokenResponse 
  113. */ 
  114. public function testParseAccessTokenResponseThrowsExceptionOnError() 
  115. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  116. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}')); 
  117.  
  118. $service = new GitHub( 
  119. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  120. $client,  
  121. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  122. ); 
  123.  
  124. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  125.  
  126. $service->requestAccessToken('foo'); 
  127.  
  128. /** 
  129. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  130. * @covers OAuth\OAuth2\Service\GitHub::parseAccessTokenResponse 
  131. */ 
  132. public function testParseAccessTokenResponseValidWithoutRefreshToken() 
  133. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  134. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo", "expires_in":"bar"}')); 
  135.  
  136. $service = new GitHub( 
  137. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  138. $client,  
  139. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  140. ); 
  141.  
  142. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  143.  
  144. /** 
  145. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  146. * @covers OAuth\OAuth2\Service\GitHub::getExtraOAuthHeaders 
  147. */ 
  148. public function testGetExtraOAuthHeaders() 
  149. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  150. $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($uri, $params, $extraHeaders) { 
  151. \PHPUnit_Framework_Assert::assertTrue(array_key_exists('Accept', $extraHeaders)); 
  152. \PHPUnit_Framework_Assert::assertTrue(in_array('application/json', $extraHeaders, true)); 
  153.  
  154. return '{"access_token":"foo", "expires_in":"bar"}'; 
  155. })); 
  156.  
  157. $service = new GitHub( 
  158. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  159. $client,  
  160. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  161. ); 
  162.  
  163. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  164.  
  165. /** 
  166. * @covers OAuth\OAuth2\Service\GitHub::__construct 
  167. * @covers OAuth\OAuth2\Service\GitHub::getExtraApiHeaders 
  168. */ 
  169. public function testGetExtraApiHeaders() 
  170. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  171. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  172.  
  173. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  174. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  175. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  176.  
  177. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  178. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  179.  
  180. $service = new GitHub( 
  181. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  182. $client,  
  183. $storage 
  184. ); 
  185.  
  186. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  187.  
  188. $this->assertTrue(array_key_exists('Accept', $headers)); 
  189. $this->assertSame('application/vnd.github.beta+json', $headers['Accept']);